store details in gidview on button click - javascript

In my application I first need to have a button on clicking the button user should see a pop up with labels and text boxes and on clicking the add button which is in modal pop up the data should be added to grid view every thing is done, but if for the first time i enter the details of a person and i click on add button then they are added to gridview, again if i enter details of another person then the details that are in gridview for first person are disappered and in gridview the second person details are displayed.But it should display both the records.Also if i again run the application after some time then the added records are not displayed in gridview that means when i run the application after some time if i have added the records previously thay should be displayed in gridview along with button but only the button is displayed but not gridview how can i display the added records even i run the application any no of times.
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.Background
{
background-color: Black;
filter: alpha(opacity=90);
opacity: 0.8;
}
.Popup
{
background-color: #FFFFFF;
border-width: 3px;
border-style: solid;
border-color: black;
padding-top: 10px;
padding-left: 10px;
width: 400px;
height: 350px;
}
.lbl
{
font-size:16px;
font-style:italic;
font-weight:bold;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Button ID="Button1" runat="server" Text="Fill Form in Popup" />
<div style="margin-left:10px;margin-top:10px">
<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" CellPadding="2"
ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField HeaderStyle-Width="120px" HeaderText="FirstName" DataField="FirstName" />
<asp:BoundField HeaderStyle-Width="120px" HeaderText=" MiddleName" DataField="MiddleName" />
<asp:BoundField HeaderStyle-Width="120px" HeaderText=" LastName" DataField="LastName" />
<asp:BoundField HeaderStyle-Width="120px" HeaderText="Gender" DataField="Gender" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
</div>
<!-- ModalPopupExtender -->
<cc1:ModalPopupExtender ID="mp1" runat="server" PopupControlID="Panl1" TargetControlID="Button1"
CancelControlID="Button2" BackgroundCssClass="Background">
</cc1:ModalPopupExtender>
<asp:Panel ID="Panl1" runat="server" CssClass="Popup" align="center" style = "display:none">
<div id="irm1" style=" width: 300px; height: 250px;" runat="server" >
<table>
<tr>
<td>
<asp:Label ID="Label1" runat="server" CssClass="lbl" Text="First Name"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" Font-Size="14px" ></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" CssClass="lbl" Text="Middle Name"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server" Font-Size="14px" ></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" CssClass="lbl" Text="Last Name"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox3" runat="server" Font-Size="14px" ></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label4" runat="server" CssClass="lbl" Text="Gender"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox4" runat="server" Font-Size="14px" ></asp:TextBox>
</td>
<td>
<asp:Button ID="Button3" runat="server" Text="Add" OnClick="Button3_Click" />
</td>
</tr>
</table>
</div>
<br />
<asp:Button ID="Button2" runat="server" Text="Close" />
</asp:Panel>
<!-- ModalPopupExtender -->
</form>
</body>
In my code behind-
protected void Page_Load(object sender, EventArgs e)
{
dt = new DataTable();
DataColumn dc1 = new DataColumn("FirstName");
DataColumn dc2 = new DataColumn("MiddleName");
DataColumn dc3 = new DataColumn("LastName");
DataColumn dc4 = new DataColumn("Gender");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
dt.Columns.Add(dc4);
DataRow dr1 = dt.NewRow();
GridView1.DataSource = dt;
GridView1.DataBind();
}
DataTable dt;
protected void Button3_Click(object sender, EventArgs e)
{
DataRow dr1 = dt.NewRow();
dr1[0] = TextBox1.Text;
dr1[1] = TextBox2.Text;
dr1[2] = TextBox3.Text;
dr1[3] = TextBox4.Text;
dt.Rows.Add(dr1);
GridView1.DataSource = dt;
GridView1.DataBind();
ClearFields();
}
protected void ClearFields()
{
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
}

carry your gridview inside Panl1 and all your code behind to webform1.cs, delete webform2, you dont need it.

Related

Call javascript in aspx.cs with div<%# Eval("variable") %>' as parameter in aspx

I have a collapsible and expandable gridview. When i change the dropdown in my gridview the gridview collapses due to the autopostback that is caused. I need to do a autopostback since I want to populate a second dropdown based on the first dropdown. Is there a way I can expanded it after it is collapsed. I am thinking of calling the javascript but i dont now how to call it since I have little knowledge in javascript. Thank you for your help.
Here is my javascript.
function divexpandcollapse(divname) {
var div = document.getElementById(divname);
var img = document.getElementById('img' + divname);
if (div.style.display == "none") {
div.style.display = "inline";
img.src = "../images/minus.gif";
} else {
div.style.display = "none";
img.src = "../images/plus.gif";
}
}
Here is the code for my indexChanged how can I add the javascript to expand the grid.
protected void OnClosetIndexChanged(object sender, EventArgs e)
{
//Call javascript to reopen gridview
}
And here is my front end
<asp:TemplateField ItemStyle-Width="20px" >
<ItemTemplate>
<a href="JavaScript:divexpandcollapse('div<%# Eval("componente_id") %>');" >
<img id="imgdiv<%# Eval("componente_id") %>" width="9px" border="0" src="../images/plus.gif"
alt="" /></a>
<asp:ImageButton ImageUrl="~/images/select.png" runat="server" CommandName="Select" OnRowDataBound = "OnRowDataBound" Width="10px" Height="10px"/>
</ItemTemplate>
<ItemStyle Width="20px" VerticalAlign="Middle"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Purchase Order ID">
<ItemTemplate>
<asp:Label ID="lblPurchaseOrderID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "componente_id") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<tr>
<td colspan="100%" style="background:#F5F5F5" >
<div id="div<%# Eval("componente_id") %>" style="overflow:auto; display:none; position: relative; left: 15px; overflow: auto">
<div class="header">
Cambiar la cantidad
</div>
<div class="body">
<asp:Label ID="lblType" runat="server" Text=""></asp:Label>
<br/>
<label for="validationOfTypeID">Armario:</label>
<asp:DropDownList ID="drCloset2" runat="server" Width="40%" Height="30px" AutoPostBack="True" OnSelectedIndexChanged = "OnClosetIndexChanged"></asp:DropDownList>
<br/>
<label for="validationOfTypeID">cajón:</label>
<asp:DropDownList ID = "drDrawer2" runat="server" Width="40%" Height="30px" >
</asp:DropDownList>
<br />
<asp:Label ID="lblQuantity" runat="server" Text=""></asp:Label>
<asp:DropDownList Height="30px" ID="drOperation" runat="server">
<asp:ListItem>+</asp:ListItem>
<asp:ListItem>-</asp:ListItem>
</asp:DropDownList>
<asp:TextBox width="50px" ID="txtChangeQuantity" runat="server" TextMode="Number" min="0" step="1" Value="0"></asp:TextBox>
<br/>
<asp:Button ID="btnChangeQuantity" runat="server" Text="Validar" OnClick="change_quantity_Click" />
<asp:Button ID="btnHide" runat="server" Text="Anular" AutoPostBack="True" />
</div>
<asp:DetailsView id="DetailsView1" DataKeyNames="componente_id" Runat="server" Width="300px" Font-Names="Calibri"/>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
Here is a image of what it looks like :

call jquery function inside gridview

call jquery function inside gridview .what i want to happen when i click on the link button open the div . i have problem here with this case didn't work .
JavaScript
function toggleDiv(divId) {
$("#shoow").toggle('slow');
}
HTML Markup
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
CellPadding="4"
ForeColor="#333333"
GridLines="None"
Width="507px"
Height="294px">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("CategoryName")%>'></asp:Label>
<br />
<asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="toggleDiv('Shoow'); return false;">
LinkButton</asp:LinkButton>
<div id="Shoow" style="background-color: blue; width: 150px; height: 150px; display: none;">
</div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Description")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
Make these changes
Markup
Pass the clicked element as itself like toggleDiv(this)
Remove the id of the div
Sample Code
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("CategoryName")%>'>
</asp:Label>
<br />
<asp:LinkButton ID="LinkButton1" runat="server"
OnClientClick="javascript:toggleDiv(this);return false;">
LinkButton</asp:LinkButton>
<div style="background-color: blue; width: 150px; height: 150px; display: none;">
</div>
</ItemTemplate>
JavaScript (Toggle the visibility of the element next to the element that's clicked)
function toggleDiv(elm) {
$(elm).next().toggle('slow');
}
Whenever I need to execute JS from a Gridview I try to avoid using asp controls like LinkButton as they tend to cause a postback which you then need to prevent, which can be done, but I find it far easier to just embed a basic html button or img tag with an onclick set for your JS. No postback and no extra code to prevent a postback.
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("CategoryName")%>'></asp:Label>
<br />
<button ID="LinkButton1" runat="server">Button Text</button>
<div id="Shoow" runat=server style="background-color: blue; width: 150px; height: 150px; display: none;">
</div>
</ItemTemplate>
</asp:TemplateField>
Then in the OnRowDatabound event you do the following because runat="server" has been set:
dim btn as HtmlButton = e.Row.FindControl("LinkButton1")
dim div as HtmlContainer = e.Row.FindControl("Shoow")
dim js as String = String.Format("toggleDiv('#{0}')", div.UniqueID)
btn.Attributes.Add("onclick", js)
and the JS:
function toggleDiv(divId) {
$(divId).toggle('slow');
}

Maintian Scrollbar Of GridView Under <div> Asp.netnot working?

i have content page in which i am selecting value from drop down on which gridview populate i am here showing my aspx page,
<div>
<table>
<tr>
<td>
<asp:DropDownList ID="ddlConsultant" runat="server"
AutoPostBack="True"
onselectedindexchanged="ddlConsultant_SelectedIndexChanged">
</asp:DropDownList>
</td>
<td>
</td>
<td>
<asp:TextBox ID="txtActivatedOn" runat="server">
</asp:TextBox>
<a href="javascript:show_calendar('<%=txtActivatedOn.ClientID%>','<%=txtActivatedOn.Text%>');">
<img border="0" src="./Images/calendar.gif" alt="calendar" /></a>
</td>
<td>
</td>
<td>
<asp:Button ID="btnUpdateActivated" runat="server" Text="Update Date"
class="button" />
</td>
</tr>
</table>
<div id="grdWithScroll" style="OVERFLOW: auto" onscroll="SetDivPosition()">
<asp:GridView ID="gvProjects" runat="server" AutoGenerateColumns="False" Height="150px"
Width="225px" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDeleting="gvProjects_RowDeleting"
DataKeyNames="ID" ViewStateMode="Inherit">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chk_All" runat="server" AutoPostBack="true" OnCheckedChanged="chk_All_CheckedChanged"/>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chk_selector" runat="server" AutoPostBack="true" OnCheckedChanged="chk_selector_CheckedChanged" Checked='<%#bool.Parse(Eval("Proj_Flag").ToString() == "True" ? "True": "False") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="lbl_ID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ProjectID" Visible="true">
<ItemTemplate>
<asp:Label ID="lblProjectID" runat="server" Text='<%# Bind("ProjectID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<%--<asp:TemplateField HeaderText="Order #">
<ItemTemplate>
<!-- <asp:Label ID="lblOrderNumber" runat="server" Text=''></asp:Label>-->
</ItemTemplate>
</asp:TemplateField>--%>
<asp:TemplateField HeaderText="Title">
<ItemTemplate>
<a href="javascript:openPopup('DsProjectDetail.aspx?mode=1&cid=<%# Eval("ID") %>&prj=<%#Eval("Title")%>' )">
<%--<%#Eval("Title")%>--%>
<asp:Label ID="lblTitle" runat="server" Text='<%# Bind("Title") %>'></asp:Label></a>
</ItemTemplate>
<ControlStyle Width="400px" />
</asp:TemplateField>
<%-- <asp:TemplateField HeaderText="CompletionDate">
<ItemTemplate>
<asp:Label ID="lblCompletionDate" runat="server" Text='<%# Bind("CompletionDate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>--%>
<asp:TemplateField HeaderText="ConsultantID" Visible="false">
<ItemTemplate>
<asp:Label ID="lblConsultantID" runat="server" Text='<%# Bind("CompanyID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField DeleteImageUrl="~/Images/Delete.gif" ButtonType="Image" ShowDeleteButton="True" />
</Columns>
</asp:GridView>
</div>
<input type="hidden" id="XPos" runat="server" />
<input type="hidden" id="YPos" runat="server" />
<table>
<tr>
<td>
<asp:Button ID="btnAddProject" runat="server" CssClass="button" Text="New Project"
onclick="btnAddProject_Click" />
</td>
</tr>
</table>
</div>
Javascript i am using are,
<script type="text/javascript">
window.onload = function(){
var strCook = document.cookie;
if(strCook.indexOf("!~")!=0){
var intS = strCook.indexOf("!~");
var intE = strCook.indexOf("~!");
var strPos = strCook.substring(intS+2,intE);
document.getElementById("grdWithScroll").scrollTop = strPos;
}
}
function SetDivPosition(){
var intY = document.getElementById("grdWithScroll").scrollTop;
document.title = intY;
document.cookie = "yPos=!~" + intY + "~!";
}
</script>
<script type="text/javascript">
window.scrollBy(100, 100);
function foo() {
alert("ddd");
if (grdWithScroll != null) alert(grdWithScroll.scrollTop);
}
</script>
and at the end of the page load event i am using,
Page.ClientScript.RegisterStartupScript(GetType(), "MyKey", "SetDivPosition();", true);
i have added this line in
<%# Page Language="C#" MaintainScrollPositionOnPostback="true" ...
and also added in webconfig,
<system.web>
<pages maintainScrollPositionOnPostBack="true" >
</pages>
</system.web>
getting help from this link,
http://stackoverflow.com/questions/12092150/maintain-scroll-bar-position-of-a-div-within-a-gridview-after-a-postback
One think i wanna mention that "onscroll="SetDivPosition()" in div tag showing me warning that on scroll is not valid attribute for div tag
But after doing all this its not working for me firstly i populate gridview with dropdown when grid populate it shows check box column too in all rows that on checked save check box value in database and post back occurs if i am at bottom of grid it scroll to the top after post back
Hopes for your suggestion
Thanks

Attaching click event to a table seems not working

I have a table with some results in 2 columns, and one checkbox. When the checkboxes are clicked I should put the clicked or selected ones in a div in the same page.
Tht html generated is like this:
<table cellspacing="0" cellpadding="4" id="ctl00_PlaceHolderMain_myGrid" style="color:#333333;width:100%;border-collapse:collapse;">
<tr style="color:White;background-color:#5D7B9D;font-weight:bold;">
<th scope="col"> </th>
<th scope="col">JobCode</th>
<th scope="col">JobName</th>
<th scope="col">JobPartner</th>
<th scope="col">JobManager</th>
<th scope="col">ClientName</th>
</tr>
<tr style="color:#333333;background-color:#F7F6F3;">
<td>
<input id="ctl00_PlaceHolderMain_myGrid_ctl02_CheckBox1" type="checkbox" name="ctl00$PlaceHolderMain$myGrid$ctl02$CheckBox1" />
</td>
<td>Column1</td>
<td>Column2</td>
<td>Column3</td>
<td>Column4</td>
<td>Column5</td>
</tr>
</table>
And in the js file I have this":
/// <reference path="jquery-1.9.1.min.js" />
$(document).ready(function () {
//On every checkbow that is clicked
$("#myGrid INPUT").click(function () {
var clientCode = $(this).parent().parent().parent().find("td:eq(2)").text()
var clientName = $(this).parent().parent().parent().find("td:eq(1)").text()
var displayvalue = clientCode.toUpperCase() + " - " + clientName.toUpperCase();
var removeDiv = $("#" + clientCode);
removeDiv.remove();
if ($(this).prop("checked") == true) {
AddSelectedJob(clientCode, displayvalue);
// $("[id$=ResultsDiv]").append('<div class="selectedjobs" id=' + clientCode + '>' + displayvalue + '<i class="icon-trash"></i></div>');
//Add to selectedjobs
FillSelectedJobs();
}
});
}
When I use developer tools and attach the js debugger, and I click on a checkbox, with a breakpoint inside the function, nothing happens.
Update
This is the server aspx code
<div style="width: 100%">
<div style="float: left">
<asp:Label ID="Label1" runat="server" Text="Search :"></asp:Label>
</div>
<div style="float: left">
<asp:TextBox ID="txtSearch" runat="server" Width="250px"></asp:TextBox>
</div>
<div style="float: left">
<asp:Button ID="btnSearch" runat="server" Text="Search" />
</div>
<div style="float: left; margin-left: 20px">
<asp:Label ID="lblClientCode" runat="server" Text=""></asp:Label>
</div>
<div style="clear: both"></div>
<div>
<div style="height: 300px; overflow: auto; float: left">
<asp:GridView ID="myGrid"
AutoGenerateColumns="true"
runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Width="100%">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:CheckBox ID="chkSelected" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<div style="margin-top: 0px; margin-left: 10px; float: left">
<asp:Label Text="Selected :" runat="server"></asp:Label>
<div id="ResultsDiv" style="margin-top: 0px">
</div>
</div>
<div style="clear: both"></div>
</div>
<div style="margin-top: 20px; margin-left: 550px">
<asp:Button ID="btnClose" runat="server" Text="Close" />
</div>
<div>
<asp:Label ID="lblError" runat="server" Text=""></asp:Label>
</div>
</div>
try with .on()
$(document).on('click','#myGrid INPUT',function () {
have a look here: http://jsfiddle.net/kb55u/
use
$("#ctl00_PlaceHolderMain_myGrid input").change(function () {
instead of
$("#myGrid INPUT").click(function () {
then it should work
Your problem is that the table id is assigned by ASP so when you look for it in your javascript code by #myGrid, nothing is found.
A quick solution would be to wrap the asp gridView in a Div with an id, and use that id
example:
<div id="myGridContainer">
<asp:gridview id="myGrid" ...>
</div>
$("#myGridContainer INPUT").click(function () {...});
An other solution is to select the element that the id ends with myGrid:
$("[id$=myGrid] INPUT").click(function () {...});
Try doing like this..
$('#myGrid INPUT').live('click', function(){
Try this:
$("[id$='CheckBox1']").click(function(){
alert("CheckBox1 clicked");
});

Passing ASp.net ClientId to javascript function

I have an ASP.NET page that contains a JavaScript function as follows:
function PrintGridView(Title,Address,Terms,GridViewName) {
if (document.getElementById != null) {
var html = '<html xmlns="http://www.w3.org/1999/xhtml" dir="rtl" xml:lang="en"><HTML>\n\n';
html += '<head> <title>' + Title + '</title> <link href="Styles/Print.css" rel="stylesheet" type="text/css" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
html += "";
html += '</head>';
html += '<body> <div id="page-wrap">';
html += '<textarea id="header">'+Title+'</textarea>';
html += '<div id="identity">';
html += '<textarea id="address">'+Address+'</textarea>';
html += '<div id="logo">';
html += '<div id="logohelp">';
html +='<input id="imageloc" type="text" size="50" value="" /><br />';
html +='(max width: 540px, max height: 100px)';
html +='</div>';
html += '<img id="image" src="images/smallLogo.png" alt="logo" />';
html +='</div>';
html += '</div>';
html += '<div style="clear:both"></div>';
var tablerow = GridViewName.getElementsByTagName("tr");
html += '<table align="center" id="items">';
var td = tablerow.item(0).getElementsByTagName("th");
html += '<tr>'; // Get Table Header
for (var i = 1; i < td.length ; i++) {
html +='<th width=auto>'+td.item(i).innerText+'</th>';
}
html += '</tr>';
//Get Table Rows Data
if (tablerow.length > 10)
var Rowlen = 11;
else
var Rowlen = tablerow.length -1;
for (var j = 1; j < Rowlen; j++) {
html += '<tr class="item-row">';
var tRow= tablerow.item(j).getElementsByTagName("td");
for (var i = 1; i < 3; i++) {
html += '<td >' + tRow.item(i).innerText + '</td>';
}
html += '</tr>';
}
html += '</table>';
html += '<div id="terms"> <h5>notice</h5> <textarea>'+Terms+'</textarea></div>';
html +='</div></body></html>';
}
var printWin = window.open("", "PrintGridView");
printWin.document.open();
printWin.document.write(html);
printWin.document.close();
//printWin.print();
}
The ASP.NET page is as follows:
<%# Page Title="Damascus University-Institutes Management System" Language="C#" MasterPageFile="~/University.master" AutoEventWireup="true" CodeBehind="UniPunishmentRewardInfo.aspx.cs" Inherits="InstitutesManagementSystem.RewardPunishmentInfo" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContainerHolder" runat="server">
<script type="text/javascript">
// PrintGridView(Title,Address,Terms,GridViewName) code is here
</script>
<asp:TabContainer ID="TabContainer" runat="server" Width="700px"
ActiveTabIndex="0" OnDemand="true" AutoPostBack="false" TabStripPlacement="Top"
CssClass="fancy fancy-green" ScrollBars="None"
UseVerticalStripPlacement="false" VerticalStripWidth="120px"
onactivetabchanged="TabContainer_ActiveTabChanged" ViewStateMode="Enabled">
<asp:TabPanel ID="TabPanelSendingDownType" runat="server" HeaderText="Typs" Enabled="true" ScrollBars="Auto" OnDemandMode="Once" ViewStateMode="Enabled">
<ContentTemplate>
<asp:UpdatePanel ID="UpdatePanelSendingDownType" runat="server"
UpdateMode="Conditional">
<ContentTemplate>
<table align="center" class="tabContainerTable" >
<tr class="tabContainerTableRow">
<td align="center" >
<table align="center">
<tr>
<td>
<asp:ImageButton ID="SendingDownTypeRefreshImageButton" runat="server" ImageUrl="~/images/refresh.png" ToolTip="Refresh" onclick="SendingDownTypeRefreshImageButton_Click" />
</td>
<td>
<asp:ImageButton ID="SendingDownTypePrintImageButton" runat="server" ImageUrl="~/images/printer.png" ToolTip="Print" onclick="SendingDownTypePrintImageButton_Click" OnClientClick="PrintGridView('university',' address','Terms','"<%=SendingDownTypeGridView.ClientID>"')"/>
</td>
</tr>
</table>
</td>
</tr>
<tr class="tabContainerTableRow" >
<td align="center" class="tabContainerTableCell" ><asp:Label ID="SendingDownResultsLabel" runat="server"></asp:Label></td>
</tr>
<tr class="tabContainerTableRow">
<td align="center" class="tabContainerTableCell" >
<asp:Panel ID="PanelSendingDownType" runat="server" ScrollBars="Auto" Width="650px">
<asp:GridView ID="SendingDownTypeGridView" runat="server"
DataKeyNames="SendingDownTypeID" HeaderStyle-BackColor="#61A6F8"
ShowFooter="True" HeaderStyle-Font-Bold="true"
HeaderStyle-ForeColor="White" AutoGenerateColumns="False" HorizontalAlign="Center"
onrowcommand="SendingDownTypeGridView_RowCommand"
DataSourceID="SendingDownTypeSqlDataSource" AllowPaging="True"
AllowSorting="True">
<Columns>
<asp:TemplateField HeaderText="???????" HeaderStyle-HorizontalAlign="center">
<EditItemTemplate>
<asp:ImageButton ID="SendingDownTypeImgBtnUpdate" CommandName="Update" runat="server" ImageUrl="~/images/save.png" ToolTip="Update" Height="20px" Width="20px" />
<asp:ImageButton ID="SendingDownTypeImgBtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/images/exit.png" ToolTip="Cancel" Height="20px" Width="20px" />
</EditItemTemplate>
<ItemTemplate>
<asp:ImageButton ID="SendingDownTypeImgBtnEdit" CommandName="Edit" runat="server" ImageUrl="~/images/edit.png" ToolTip="Edit" Height="20px" Width="20px" />
<asp:ImageButton ID="SendingDownTypeImgBtnDelete" CommandName="Delete" Text="Remove" runat="server" ImageUrl="~/images/-.png" ToolTip="Remove" Height="20px" Width="20px" />
</ItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="SendingDownTypeImgBtnAppend" runat="server" ImageUrl="~/images/+.png" CommandName="Insert" Width="30px" Height="30px" ToolTip="Append" ValidationGroup="Tab1" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="??? ??? ?????" HeaderStyle-HorizontalAlign="center">
<EditItemTemplate>
<asp:Label ID="EditSendingDownTypeIDLable" runat="server" CssClass="input" Text='<%#Eval("SendingDownTypeID") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="ItemSendingDownTypeIDLable" runat="server" CssClass="input" Text='<%#Eval("SendingDownTypeID") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="SendingDownTypeIDTextBox" runat="server" MaxLength="3" CssClass="input" />
<asp:RequiredFieldValidator ID="SendingDownTypeIDTextBoxVali" runat="server" ControlToValidate="SendingDownTypeIDTextBox" Text="*" ValidationGroup="Tab1"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="??? ?????" HeaderStyle-HorizontalAlign="center">
<EditItemTemplate>
<asp:TextBox ID="EditSendingDownTypeNameTextBox" runat="server" CssClass="input" Text='<%#Eval("SendingDownTypeName") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="ItemSendingDownTypeNameLable" runat="server" CssClass="input" Text='<%#Eval("SendingDownTypeName") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="SendingDownTypeNameTextBox" runat="server" MaxLength="50" CssClass="input"/>
<asp:RequiredFieldValidator ID="SendingDownTypeNameTextBoxVali" runat="server" ControlToValidate="SendingDownTypeNameTextBox" Text="*" ValidationGroup="Tab1"/>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#61A6F8" Font-Bold="True" ForeColor="White" />
</asp:GridView>
<asp:SqlDataSource ID="SendingDownTypeSqlDataSource" runat="server" ConflictDetection="OverwriteChanges" ConnectionString="<%$ ConnectionStrings:SqlConnectionString %>"
DeleteCommand="DELETE FROM [SendingDownType_Table] WHERE [SendingDownTypeID] =#SendingDownTypeID"
InsertCommand="IF NOT EXISTS (SELECT * FROM [SendingDownType_Table] WHERE [SendingDownTypeID] = #SendingDownTypeID ) INSERT INTO [SendingDownType_Table] ([SendingDownTypeID], [SendingDownTypeName]) VALUES (#SendingDownTypeID, #SendingDownTypeName) ELSE PRINT N'This Type ID Is Exist'"
SelectCommand="SELECT [SendingDownTypeID], [SendingDownTypeName] FROM [SendingDownType_Table] ORDER BY [SendingDownTypeID], [SendingDownTypeName]"
UpdateCommand="UPDATE [SendingDownType_Table] SET [SendingDownTypeName] = #SendingDownTypeName WHERE [SendingDownTypeID] = #SendingDownTypeID">
<DeleteParameters>
<asp:Parameter Name="SendingDownTypeID" Type="Byte" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="SendingDownTypeID" Type="Byte" />
<asp:Parameter Name="SendingDownTypeName" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="SendingDownTypeID" Type="Byte" />
<asp:Parameter Name="SendingDownTypeName" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
</asp:Panel>
</td>
</tr>
<tr class="tabContainerTableRow">
<td><div class="clear"></div></td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</ContentTemplate>
</asp:TabPanel>
</asp:TabContainer>
I try to pass a GridView ID to my JavaScript function that declared as follows:
'PrintGridView(Title,Address,Terms,GridViewName)'
to use it in my ASP.NET page for ImageButton on OnClientClick Event as follows:
<asp:ImageButton ID="SendingDownTypePrintImageButton" runat="server" ImageUrl="~/images/printer.png" ToolTip="Print" OnClientClick="PrintGridView('university',' address','Terms','<%=SendingDownTypeGridView.ClientID%>')"/>
but I always get an error. What should I do to solve this problem?
Change
'"<%=SendingDownTypeGridView.ClientID>"'
To
'<%=SendingDownTypeGridView.ClientID %>'

Categories