ASP.NET Dynamically Adding a Gridview - asp.net

I have a gridview as such
<asp:GridView runat="server" ID="gvShoppingCart" AutoGenerateColumns="false" EmptyDataText="There is nothing in your shopping cart." AlternatingRowStyle-CssClass="tr_dark" HeaderStyle-CssClass="header_req" BorderWidth="0px" GridLines="None" AllowPaging="true" PageSize="25" AllowSorting="false" Width="100%" ShowFooter="true" DataKeyNames="ProductId" OnRowDataBound="gvShoppingCart_RowDataBound" OnRowCommand="gvShoppingCart_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Product Name" SortExpression="productName" HeaderStyle-CssClass="product" >
<ItemTemplate>
<asp:Label ID="ProductNameField" runat="server" Text='<%# Eval("description").ToString() %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField HeaderText="Pack Size" SortExpression="packSize" HeaderStyle-CssClass="packsize" >
<ItemTemplate>
<asp:Label ID="PackSizeField" runat="server" Text='<%# Eval("packSize").ToString()%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField HeaderText="Stock" SortExpression="address" HeaderStyle-CssClass="stock">
<ItemTemplate>
<asp:Label ID="StockField" runat="server" Text='<%# DisplayStockLevel(Eval("StockIndicator").ToString()) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField HeaderText="Quantity" HeaderStyle-CssClass="quantity" >
<ItemTemplate>
<asp:TextBox runat="server" Width="30" ID="txtQuantity" Text='<%# Eval("Quantity") %>'></asp:TextBox>
<asp:TextBox runat="server" Visible="false" Width="30" ID="txtProductCode" Text='<%# Eval("ProductCode") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField HeaderText="Actual Price" HeaderStyle-CssClass="actual" SortExpression="address">
<ItemTemplate>
<asp:Label ID="TradePriceField" runat="server" Text='<%# DisplayMoney(Eval("UnitPrice").ToString())%>'></asp:Label>
<asp:Label ID="TradePriceFieldHidden" runat="server" Text='<%# Eval("UnitPrice").ToString()%>' Visible="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:BoundField DataField="TotalPrice" HeaderText="Total" HeaderStyle-CssClass="total" HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:C}" />
</Columns>
<Columns>
<asp:TemplateField HeaderText="" HeaderStyle-CssClass="remove">
<ItemTemplate>
<asp:ImageButton ImageUrl="~/img/icons/cross.gif" width="10" height="10" alt="Cancel" runat="server" ID="btnRemove" CommandName="Remove" CommandArgument='<%# Eval("ProductId") %>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
And now trying to add a footer rows dynamically ( because I need multiple footer rows)
Protected Sub gvShoppingCart_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles gvShoppingCart.RowDataBound
' If we are binding the footer row, let's add in our total
If e.Row.RowType = DataControlRowType.Footer Then
e.Row.Cells(5).Text = "<strong>Total Cost:</strong>"
e.Row.Cells(6).Text = ShoppingCart.Instance.GetSubTotal().ToString("C")
End If
Dim grid As GridView = CType(sender, GridView)
''gets the current footer row to clone
Dim footer As GridViewRow = grid.FooterRow
Dim numCells = footer.Cells.Count
Dim newRow As New GridViewRow(footer.RowIndex + 1, -1, footer.RowType, footer.RowState)
''have to add in the right number of cells
''this also copies any styles over from the original footer
For i As Integer = 0 To numCells - 1
Dim emptyCell As New TableCell
emptyCell.ApplyStyle(grid.Columns(i).ItemStyle)
newRow.Cells.Add(emptyCell)
Next
newRow.Cells(5).Text = "Total Discount:"
newRow.Cells(6).Text = "55.00"
''add new row to the gridview table, at the very bottom
CType(grid.Controls(0), Table).Rows.Add(newRow)
End Sub
But getting the error
Object reference not set to an instance of an object.
Dim numCells = footer.Cells.Count
Any idea whats going wrong ?

The GridView FooterRow property is only set after the RowDataBound method has been called. You can access it in the DataBound method of the GridView.
Protected Sub gvShoppingCart_DataBound(ByVal sender As Object, ByVal e As EventArgs)
Dim footerRow as GridViewRow = gvShoppingCart.FooterRow
End Sub

First of all your GridView Markup is incorrect. Define Columns just once like this.
<asp:GridView ID="gvShoppingCart" runat="server"
AlternatingRowStyle-CssClass="tr_dark"
AllowPaging="true"
AllowSorting="false"
AutoGenerateColumns="false"
BorderWidth="0px"
DataKeyNames="ProductId"
EmptyDataText="There is nothing in your shopping cart."
GridLines="None"
HeaderStyle-CssClass="header_req"
PageSize="25"
ShowFooter="true"
Width="100%"
OnRowCommand="gvShoppingCart_RowCommand"
OnRowDataBound="gvShoppingCart_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Product Name" SortExpression="productName" HeaderStyle-CssClass="product">
<ItemTemplate>
<asp:Label ID="ProductNameField" runat="server"
Text='<%# Eval("description").ToString() %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Pack Size" SortExpression="packSize" HeaderStyle-CssClass="packsize">
<ItemTemplate>
<asp:Label ID="PackSizeField" runat="server"
Text='<%# Eval("packSize").ToString()%>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Stock" SortExpression="address" HeaderStyle-CssClass="stock">
<ItemTemplate>
<asp:Label ID="StockField" runat="server"
Text='<%# DisplayStockLevel(Eval("StockIndicator").ToString()) %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity" HeaderStyle-CssClass="quantity">
<ItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server"
Width="30"
Text='<%# Eval("Quantity") %>'>
</asp:TextBox>
<asp:TextBox ID="txtProductCode" runat="server"
Visible="false"
Width="30"
Text='<%# Eval("ProductCode") %>'>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Actual Price" HeaderStyle-CssClass="actual" SortExpression="address">
<ItemTemplate>
<asp:Label ID="TradePriceField" runat="server"
Text='<%# DisplayMoney(Eval("UnitPrice").ToString())%>'>
</asp:Label>
<asp:Label ID="TradePriceFieldHidden" runat="server"
Text='<%# Eval("UnitPrice").ToString()%>'
Visible="false">
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="TotalPrice" HeaderText="Total"
HeaderStyle-CssClass="total" HeaderStyle-HorizontalAlign="Right"
DataFormatString="{0:C}" />
<asp:TemplateField HeaderText="" HeaderStyle-CssClass="remove">
<ItemTemplate>
<asp:ImageButton ID="btnRemove" runat="server"
ImageUrl="~/img/icons/cross.gif"
Width="10"
Height="10"
AlternateText="Cancel"
CommandName="Remove"
CommandArgument='<%# Eval("ProductId") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Secondly, you are getting error because you have not defined your FooterTemplate anywhere.

Related

How to Enable a DropDownList in Gridview When Selected the First DropDownList

I'm working with my GridView table in ASP.Net.
First when I click the Edit link button in my GridView I will be able to edit it with Textbox and dropdownlist.
Here's the image of my GridView.
So the Business Unit was enabled but the Division, Sub-Division, Classification and Sub-Classification are not.
My problem is that when I select the Business Unit, the dropdownlist Division should be enabled and be able to select the value according to the Business Unit that I selected and so on. So the next dropdownlist will be dependent on the first dropdownlist that you selected. I searched about this on the internet already and tried different solutions but I'm still confused and don't know how it will work.
Here's my code in ASP.Net
<asp:GridView ID="gvGroup" runat="server" AllowPaging="false"
AllowSorting="true" AutoGenerateColumns="False" BorderColor="Silver"
BorderWidth="1px" Height="104px" ShowFooter="true" ShowHeader="true"
style="margin-right: 0px" Width="77%">
<RowStyle Font-Names="Arial" Font-Size="9pt" HorizontalAlign="Center" />
<Columns>
<asp:TemplateField HeaderStyle-BackColor="#666666"
HeaderStyle-ForeColor="White" HeaderStyle-Width="10px" HeaderText=""
ItemStyle-Width="10px">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
<HeaderStyle BackColor="#666666" ForeColor="White" />
<ItemStyle Width="10px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="" ItemStyle-Width="140px" Visible="false">
<ItemTemplate>
<asp:Label ID="lblMasterID" runat="server" Text='<%#Bind("MASTERID") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="" ItemStyle-Width="140px" Visible="false">
<ItemTemplate>
<asp:Label ID="lblSEQID" runat="server" Text='<%#Bind("SEQID") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt"
HeaderStyle-ForeColor="White" HeaderText="Job Title" ItemStyle-Font-Size="9pt"
ItemStyle-Width="140px">
<ItemTemplate>
<asp:Label ID="lblJobTitle" runat="server" Text='<%#Bind("JOBTITLE") %>'
Width="140px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtJT" runat="server" CssClass="ehr_textbox"
Text='<%#BIND("JOBTITLE") %>' Width="140px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtJobTitleAdd" runat="server" Width="110px">
</asp:TextBox>
</FooterTemplate>
<HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
<ItemStyle Font-Size="9pt" Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt"
HeaderStyle-ForeColor="White" HeaderText="Business Unit"
ItemStyle-Font-Size="9pt" ItemStyle-Width="140px">
<ItemTemplate>
<asp:Label ID="lblBusinessUnit" runat="server"
Text='<%#Bind("BUSINESS_UNIT") %>' Width="140px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_gvBusinessUnit" runat="server" CssClass="ehr_dropdown"
Width="140px">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlBusinessUnitAdd" runat="server" Width="110px">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
<ItemStyle Font-Size="9pt" Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt"
HeaderStyle-ForeColor="White" HeaderText="Division" ItemStyle-Font-Size="9pt"
ItemStyle-Width="140px">
<ItemTemplate>
<asp:Label ID="lblDivision" runat="server" Text='<%#Bind("DIVISION") %>'
Width="140px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_gvDivision" Enabled="false" runat="server" CssClass="ehr_dropdown"
Width="140px">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlDivisionAdd" runat="server" Width="110px">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
<ItemStyle Font-Size="9pt" Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt"
HeaderStyle-ForeColor="White" HeaderText="Sub-Division"
ItemStyle-Font-Size="9pt" ItemStyle-Width="140px">
<ItemTemplate>
<asp:Label ID="lblSubDivision" runat="server" Text='<%#Bind("SUB_DIVISION") %>'
Width="140px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_gvSubDivision" Enabled="false" runat="server" CssClass="ehr_dropdown"
Width="140px">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlSubDivisionAdd" runat="server" Width="110px">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
<ItemStyle Font-Size="9pt" Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt"
HeaderStyle-ForeColor="White" HeaderText="Classification"
ItemStyle-Font-Size="9pt" ItemStyle-Width="140px">
<ItemTemplate>
<asp:Label ID="lblClassification" runat="server"
Text='<%#Bind("CLASSIFICATION") %>' Width="140px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_gvClassification" Enabled="false" runat="server"
CssClass="ehr_dropdown" Width="140px">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlClassification" runat="server" Width="110px">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
<ItemStyle Font-Size="9pt" Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt"
HeaderStyle-ForeColor="White" HeaderText="Sub-Classification"
ItemStyle-Font-Size="9pt" ItemStyle-Width="140px">
<ItemTemplate>
<asp:Label ID="lblSubClassification" runat="server"
Text='<%#Bind("SUB_CLASSIFICATION") %>' Width="140px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_gvSubClassification" Enabled="false" runat="server"
CssClass="ehr_dropdown" Width="140px">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlSubClassification" runat="server" Width="110px">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
<ItemStyle Font-Size="9pt" Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" ItemStyle-Font-Size="9pt">
<ItemTemplate>
<asp:LinkButton ID="lnkRemove" runat="server" CommandName="Delete"
OnClientClick="return confirm('Do you want to delete?')" Text="Delete"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="btnAdd" runat="server" CommandName="Add"
OnClick="AddgvGroup" Text="Add"></asp:LinkButton>
</FooterTemplate>
<HeaderStyle BackColor="#666666" />
<ItemStyle Font-Size="9pt" />
</asp:TemplateField>
<asp:CommandField HeaderStyle-BackColor="#666666" HeaderStyle-Width="10px"
ItemStyle-Font-Size="9pt" ItemStyle-Width="10px" ShowEditButton="True">
<HeaderStyle BackColor="#666666" />
<ItemStyle Font-Size="9pt" />
</asp:CommandField>
</Columns>
<PagerStyle Font-Size="9pt" HorizontalAlign="Right" />
<EmptyDataTemplate>
<div style="width: 100%; font-size: 10pt; text-align: center; color: Red;">
No record found.
</div>
</EmptyDataTemplate>
<HeaderStyle BackColor="DarkGray" Font-Bold="True" Font-Names="Arial"
Font-Size="9pt" ForeColor="White" HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="Gainsboro" />
</asp:GridView>
And here's my code in VB.Net
Protected Sub gvGroup_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvGroup.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
If (e.Row.RowState And DataControlRowState.Edit) > 0 Then
Dim ddlBusinessUnit As DropDownList = DirectCast(e.Row.FindControl("ddl_gvBusinessUnit"), DropDownList)
'bind dropdown-list
Dim sqlstr As String
Dim dt As DataTable = New DataTable()
sqlstr = "Select BUSINESS_UNIT from BUSINESS_UNIT_TBL"
dt = ehr_utils.DataTable(sqlstr)
ddlBusinessUnit.DataSource = dt
ddlBusinessUnit.DataTextField = "BUSINESS_UNIT"
ddlBusinessUnit.DataValueField = "BUSINESS_UNIT"
ddlBusinessUnit.DataBind()
Dim dr As DataRowView = TryCast(e.Row.DataItem, DataRowView)
ddlBusinessUnit.SelectedValue = dr("BUSINESS_UNIT").ToString()
End If
End If
End Sub
Any idea of how can I possibly do that? And I tried also the OnSelectedIndexChanged
First you need to add an OnSelectedIndexChanged to ddl_gvBusinessUnit and set AutoPostBack to true
<asp:DropDownList ID="ddl_gvBusinessUnit" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_gvBusinessUnit_SelectedIndexChanged">
And then in code behind
protected void ddl_gvBusinessUnit_SelectedIndexChanged(object sender, EventArgs e)
{
//cast the sender back to a dropdownlist
DropDownList dropDownList1 = sender as DropDownList;
//get the selectedvalue
string value = dropDownList1.SelectedValue;
//find the other dropdownlist in the correct row by using the editindex
DropDownList dropDownList2 = gvBusinessUnit.Rows[gvBusinessUnit.EditIndex].FindControl("DropDownList2") as DropDownList;
//do stuff with the other dropdownlist, like give it a color or add database values based on the first dropdownlist
dropDownList2.BackColor = Color.Red;
}
VB
Protected Sub ddl_gvBusinessUnit_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
'cast the sender back to a dropdownlist
Dim dropDownList1 As DropDownList = CType(sender,DropDownList)
'get the selectedvalue
Dim value As String = dropDownList1.SelectedValue
'find the other dropdownlist in the correct row by using the editindex
Dim dropDownList2 As DropDownList = CType(gvBusinessUnit.Rows(gvBusinessUnit.EditIndex).FindControl("DropDownList2"),DropDownList)
'do stuff with the other dropdownlist, like give it a color or add database values based on the first dropdownlist
dropDownList2.BackColor = Color.Red
End Sub

export gridview to excel is not working with Ajax and Update panel

I have a grid-view in Update panel. there is a link button inside grid-view which fire export command in row command event. There is an another grid-view which get exported on this command code. I debug, the pointers goes in but the export not happening. I know i am missing something here, but not able to get it after spending lot of hours. The Script manager is on master page.
Can any one help please. Below is the code.
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:GridView ID="grd_Report" runat="server" ShowFooter="true" CssClass="ui-jqgrid-btable table-hover" AutoGenerateColumns="false" AllowPaging ="true" PageSize ="12" >
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lbl_CampaignName" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="100px" />
<HeaderStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="CreateDate">
<ItemTemplate>
<asp:Label ID="lbl_CreateDate" runat="server" Text='<%#Eval("CreateDate") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="130px" />
<HeaderStyle Width="130px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="sender">
<ItemTemplate>
<asp:Label ID="lbl_senderID" runat="server" Text='<%#Eval("sender") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="85px" />
<HeaderStyle Width="85px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Text">
<ItemTemplate>
<asp:Label ID="lbl_text" runat="server" Text='<%#Eval("Text") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList class="ui-pg-selbox" ID="drp_itemCount" runat="server" AutoPostBack="true"
onselectedindexchanged="drp_itemCount_SelectedIndexChanged" >
<asp:listItem>10</asp:listItem>
<asp:listItem>15</asp:listItem>
<asp:listItem>50</asp:listItem>
<asp:listItem>100</asp:listItem>
<asp:listItem>200</asp:listItem>
</asp:DropDownList>
</FooterTemplate>
<ItemStyle Width="375px" />
<HeaderStyle Width="375px" />
<FooterStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Count">
<ItemTemplate>
<asp:Label ID="lbl_SMSCount" runat="server" Text='<%#Eval("Count") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" width="80px" />
<HeaderStyle Width="80px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Delivery Count">
<ItemTemplate>
<table class="deliveryCount">
<tr>
<td style="padding:5px;width:180px" colspan="2">Total: <%#Eval("Total")%></td>
</tr>
<table style="font-size:11px;width:180px;">
<tr>
<td style="padding:5px;font-size:11px;width:80px">A: <%#Eval("A")%></td>
<td style="padding:5px;font-size:11px;width:80px">B: <%#Eval("B")%></td>
</tr
<tr>
<td style="padding:5px;font-size:11px;width:80px">2: <%#Eval("2")%></td>
<td style="padding:5px;font-size:11px;width:80px">1: <%#Eval("1")%></td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Download">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lnk_download" style="font-size:11px;" CommandName="download" CommandArgument='<%#Eval("CampaignId") %>' text="Download" class="ui-icon-document"></asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" width="100px" />
<HeaderStyle Width="100px" />
</asp:TemplateField>
</Columns>
<HeaderStyle HorizontalAlign="Center" Font-Size="13" Font-Bold="true" />
<RowStyle Font-Names="Helvetica" Font-Size="10" />
<PagerStyle CssClass ="gridviewPager" Height="30px" />
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="grd_export" EventName="RowCommand" />
</Triggers>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel6" runat="Server">
<ContentTemplate>
<asp:GridView ID="grd_export" runat="server" Visible="false" AutoGenerateColumns ="false" >
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lbl_Name" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Number">
<ItemTemplate>
<asp:Label ID="lbl_Number" runat="server" Text='<%#Eval("Number") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lbl_Id" runat="server" Text='<%#Eval("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Text">
<ItemTemplate>
<asp:Label ID="lbl_text" runat="server" Text='<%#Eval("Text") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="text Id">
<ItemTemplate>
<asp:Label ID="lbl_MessageId" runat="server" Text='<%#Eval("textId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<asp:Label ID="lbl_Date" runat="server" Text='<%#Eval("Date") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="lbl_Status" runat="server" Text='<%#Eval("Status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Credits">
<ItemTemplate>
<asp:Label ID="lbl_Credits" runat="server" Text='<%#Eval("Credits") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Below is the code on Row Command event
If e.CommandName = "download" Then
Dim str As String
str = " select * FROM [Box] where MsgId='" + e.CommandArgument + "'"
Dim com As New SqlCommand(str, con)
Dim da As New SqlDataAdapter(com)
Dim dt As New DataTable
da.Fill(dt)
If Not dt Is Nothing Then
If dt.Rows.Count > 0 Then
grd_export.DataSource = dt
grd_export.DataBind()
grd_export.Visible = True
Response.Clear()
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.AddHeader("content-disposition", "attachment;filename=Message.xls")
Response.Charset = ""
Response.ContentType = "application/vnd.xls"
Me.EnableViewState = False
Dim stringwrite As System.IO.StringWriter = New System.IO.StringWriter
Dim htmlwrite As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(stringwrite)
grd_export.RenderControl(htmlwrite)
Response.Write(stringwrite.ToString())
grd_export.Visible = False
Response.End()
End If
End If
End If
You need to add a postback trigger to your update panel for respose.write to work.

Accessing Linkbutton click event in footer of Gridview

I am working on an asp.net project where I have a gridview on the default.aspx page databound to my sql database. When I click on a specific row it navigates to a details.aspx page where you can see and edit the details of the row clicked.
On that details.aspx page is another gridview where I exposed the footer and added and "Insert" linkbutton and the appropriate controls for each column to add a new record into the database. I have the entire project coded, but unfortunately nothing happens when I click on "Insert" linkbutton (Except the text in the controls all clear). No exceptions are thrown, but no record is added to the database. Here is the gridview code:
<asp:GridView ID="grid1" runat="server" AutoGenerateColumns="False" DataKeyNames="ClientId" DataSourceID="SqlDataSource1" ShowFooter="True" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal">
<AlternatingRowStyle BackColor="#F7F7F7" />
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField HeaderText="ClientId" InsertVisible="False" SortExpression="ClientId">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ClientId") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ClientId") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lbInsert" runat="server" CommandName="insert" CausesValidation="false">Insert</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MiddleName" SortExpression="MiddleName">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("MiddleName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("MiddleName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtMiddleName" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LastName" SortExpression="LastName">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gender" SortExpression="Gender">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Gender") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("Gender") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddGender" runat="server">
<asp:ListItem>M</asp:ListItem>
<asp:ListItem>F</asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DateOfBirth" SortExpression="DateOfBirth">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("DateOfBirth") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("DateOfBirth") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtDOB" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ZipCode" SortExpression="ZipCode">
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("ZipCode") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("ZipCode") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtZIP" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<SortedAscendingCellStyle BackColor="#F4F4FD" />
<SortedAscendingHeaderStyle BackColor="#5A4C9D" />
<SortedDescendingCellStyle BackColor="#D8D8F0" />
<SortedDescendingHeaderStyle BackColor="#3E3277" />
</asp:GridView>
and here is the codebehind:
Private Sub grid1_RowCommand1(sender As Object, e As GridViewCommandEventArgs) Handles grid1.RowCommand
If e.CommandName = "insert" Then
SqlDataSource1.InsertParameters("FirstName").DefaultValue = DirectCast(grid1.FooterRow.FindControl("txtFirstName"), TextBox).Text
SqlDataSource1.InsertParameters("MiddleName").DefaultValue = DirectCast(grid1.FooterRow.FindControl("txtMiddleName"), TextBox).Text
SqlDataSource1.InsertParameters("LastName").DefaultValue = DirectCast(grid1.FooterRow.FindControl("txtLastName"), TextBox).Text
SqlDataSource1.InsertParameters("Gender").DefaultValue = DirectCast(grid1.FooterRow.FindControl("ddGender"), DropDownList).SelectedValue
SqlDataSource1.InsertParameters("DateOfBirth").DefaultValue = DirectCast(grid1.FooterRow.FindControl("txtDOB"), TextBox).Text
SqlDataSource1.InsertParameters("ZipCode").DefaultValue = DirectCast(grid1.FooterRow.FindControl("txtZIP"), TextBox).Text
SqlDataSource1.Insert()
End If
End Sub
Thanks for any assistance in sorting this out!
John
DefaultValue doesn't set the value of what should be inserted, but rather provides resolution for null values for types that don't allow them (Integer, Long, etc). See this link for more information on what that Property does.
What you'll want to do is use Control Parameters in your SqlDataSource to bind the values of your TextBoxes to the parameters used by the InsertCommand. Use the following as an example:
<asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="<%$ ConnectionStrings:YourConnection%>"
insertcommand="INSERT INTO SomeTable (FirstName, MiddleName) VALUES (#FirstName, #MiddleName)">
<insertparameters>
<asp:controlparameter name="FirstName" controlid="txtFirstName" propertyname="Text"/>
<asp:controlparameter name="MiddleName" controlid="txtLastName" propertyname="Text"/>
</insertparameters>
</asp:sqldatasource>
Then your grid1_RowCommand1 method becomes much cleaner:
Private Sub grid1_RowCommand1(sender As Object, e As GridViewCommandEventArgs) Handles grid1.RowCommand
If e.CommandName = "insert" Then
SqlDataSource1.Insert()
End If
End Sub

Selected row to textbox when clicked on row not firing

I can click on row to selected them but when i need them to textbox it's not firing i tried to use Autopostback on textbox but it's doesn't work i think it's because of for each that not make row.cells(0) work
i write a code like this
VB.NET
Protected Sub OnSelectedIndexChanged(sender As Object, e As EventArgs)
For Each row As GridViewRow In GridView1.Rows
If row.RowIndex = GridView1.SelectedIndex Then
row.BackColor = ColorTranslator.FromHtml("#E294FF")
txtrepname.Text = row.Cells(0).Text
row.ToolTip = String.Empty
Else
row.BackColor = ColorTranslator.FromHtml("#FFFFFF")
row.ToolTip = "Click to select this row."
End If
Next
End Sub
Gridview
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" ShowFooter="True" Width="1280px" OnRowDataBound = "OnRowDataBound" OnSelectedIndexChanged = "OnSelectedIndexChanged"
onrowupdating="GridView1_RowUpdating" DataKeyNames="REP_NAME" PageSize="9999">
<Columns>
<asp:TemplateField HeaderText="" >
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" Text="" CommandName="Edit" ToolTip="Edit"
CommandArgument=''><img src="Images/ad_edit.png" BORDER="0" /></asp:LinkButton>
<asp:LinkButton ID="lnkDelete" runat="server" Text="Delete" CommandName="Delete"
ToolTip="Delete" OnClientClick='return confirm("Are you sure you want to delete this entry?");'
CommandArgument='' BorderColor="#CC3300"><img src="Images/ad_delete.png" BORDER="0" /></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lnkInsert" runat="server" Text="" ValidationGroup="editGrp" CommandName="Update" ToolTip="Save"
CommandArgument=''><img src="Images/ad_save.png" BORDER="0" /></asp:LinkButton>
<asp:LinkButton ID="lnkCancel" runat="server" Text="" CommandName="Cancel" ToolTip="Cancel"
CommandArgument=''><img src="Images/ad_reload.png" BORDER="0" /></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkInsert" runat="server" Text="" ValidationGroup="newGrp" CommandName="InsertNew" ToolTip="Add New Entry"
CommandArgument=''><img src="Images/ad_add.png" BORDER="0"/></asp:LinkButton>
<asp:LinkButton ID="lnkCancel" runat="server" Text="" CommandName="CancelNew" ToolTip="Cancel"
CommandArgument=''><img src="Images/ad_reload.png" BORDER="0"/></asp:LinkButton>
</FooterTemplate>
<HeaderStyle Width="60px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Report Name" SortExpression="REP_NAME" >
<ItemTemplate>
<div style="overflow:hidden; text-overflow:ellipsis; white-space:nowrap; min-width:150px; ">
<asp:Label ID="Label1" runat="server" Text='<%# Bind("REP_NAME") %>'></asp:Label>
</div>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("REP_NAME") %>'></asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtREP_NAMENew" runat="server" CssClass="" Width="60px" ></asp:TextBox>
<asp:RequiredFieldValidator ID="valREP_FILEID" runat="server" ControlToValidate="txtREP_NAMENew"
Display="Dynamic" ErrorMessage="Province is required." ForeColor="Red" SetFocusOnError="True"
ValidationGroup="newGrp">*</asp:RequiredFieldValidator>
</FooterTemplate>
<HeaderStyle Width="80px" />
</asp:TemplateField>
</Columns>
</asp:GridView>

Get current GridView

I've got a page with 3 GridViews.
<div id="Export" runat="server" class="headeropt noprint">
<asp:LinkButton runat="server" ID="buttonexport" OnClick="buttonexport_Click" OnClientClick="Javascript:Noshow();" CssClass="btn btn-default">
<asp:Image runat="server" ImageUrl="/images/Excel-16.gif" />
<asp:Label runat="server" Text="Export" />
</asp:LinkButton>
</div>
<div class="col-md-12">
<asp:GridView ID="grid1" runat="server" CellPadding="0" CssClass="table" CellSpacing="0" OnRowDataBound="grid1_RowDataBound" AutoGenerateColumns="false" GridLines="Vertical" BorderStyle="Solid" ShowFooter="true" OnRowEditing="grid1_RowEditing" datakeynames="grid1">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:LinkButton ID="lnkname" runat="server" CausesValidation="false" CommandName="Edit" CommandArgument='<%# Eval("name") %>' Text='<%# Eval("name") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Company">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("company") %>'></asp:Label>
</ItemTemplate>
<ItemStyle CssClass="alinha-direita" />
<FooterStyle CssClass="alinha-direita" />
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:GridView ID="grid2" runat="server" CellPadding="0" CssClass="table" CellSpacing="0" OnRowDataBound="grid2_RowDataBound" AutoGenerateColumns="false" GridLines="Vertical" BorderStyle="Solid" ShowFooter="true" OnRowEditing="grid2_RowEditing" datakeynames="grid2">
<Columns>
<asp:TemplateField HeaderText="Product Family">
<ItemTemplate>
<asp:LinkButton ID="lnkprdfamily" runat="server" CausesValidation="false" CommandName="Edit" CommandArgument='<%# Eval("prdfamily") %>' Text='<%# Eval("prdfamily") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("quantity") %>'></asp:Label>
</ItemTemplate>
<ItemStyle CssClass="alinha-direita" />
<FooterStyle CssClass="alinha-direita" />
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:GridView ID="grid3" runat="server" CellPadding="0" CssClass="table" CellSpacing="0" OnRowDataBound="grid3_RowDataBound" AutoGenerateColumns="false" GridLines="Vertical" BorderStyle="Solid" ShowFooter="true" datakeynames="grid3">
<Columns >
<asp:TemplateField HeaderText="Product">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("product") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("price") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
as you can see, the first grid links to the second, and the second to the third.
What I want to know, is at any given time to get the grid I'm viewing at the moment.
Just edited adding the linkButton that exports the grid to excel. This is why I need the gridview that I'm currently on, so that the export command works at any time.
Here's the export code:
protected void buttonexport_Click(object sender, EventArgs c)
{
Master.Page.Form.Attributes.Remove("onsubmit");
CB.ChangeControlsToValue(grid1);
string attachment = "attachment; filename=File.xls";
//Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
grid1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
Thanks.
Nevemind, got a solution.
I've just checked the one that is visible at the moment and work with it.
bool list = grid1.Visible;
if (list)
{ CB.ChangeControlsToValue(grid1); }
and so on....

Resources