Selected row to textbox when clicked on row not firing - asp.net

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>

Related

Gridview Not Triggering Events

I'm trying to use the events on my Gridview control. But its not firing the events when expected. i.e, for OnSelectedIndexChanged, when changing the selected cell/row nothing happens. I've tried with OnSelectedIndexChanged as well as OnRowUpdating. The breakpoint is never hit in gvQ15_RowUpdating. I've even tried using the OnTextChanged event for the child textboxes or the gridview and they are not firing.
Markup
<asp:GridView AutoPostBack="true" ID="gvQ15" runat="server" AllowPaging="false" AutoGenerateColumns="false" >
<Columns>
<asp:TemplateField HeaderText="Prescription Medication Name" ItemStyle-Width="25%">
<ItemTemplate>
<asp:TextBox ID="txtPrescriptionMedicationName" runat="server" Width="100%" OnTextChanged="txtPrescriptionMedicationName_TextChanged" BordewWidth="0" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date Originally Prescribed" ItemStyle-Width="10%">
<ItemTemplate>
<asp:TextBox ID="txtDateOriginallyPrescribed" runat="server" Style="min-width: 110px; width: 100%;" BorderWidth="0" OnTextChanged="txtDateOriginallyPrescribed_TextChanged" />
<asp:MaskedEditExtender ID="meetxtDateOriginallyPrescribed" runat="server" MaskType="date" UserDateFormat="MonthDayYear" Mask="99/99/9999" TargetControlID="txtDateOriginallyPrescribed" ClearMaskOnLostFocus="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Frequency and Dosage" ItemStyle-Width="25%">
<ItemTemplate>
<asp:TextBox ID="txtFrequencyAndDosage" runat="server" Width="100%" BorderWidth="0" OnTextChanged="txtFrequencyAndDosage_TextChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Condition" ItemStyle-Width="25%">
<ItemTemplate>
<asp:TextBox ID="txtCondition" runat="server" Width="100%" BorderWidth="0" OnTextChanged="txtCondition_TextChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Onset Date" ItemStyle-Width="10%">
<ItemTemplate>
<asp:TextBox ID="txtConditionOnsetDate" runat="server" style="min-width:110px;width:100%" BorderWidth="0" OnTextChanged="txtConditionOnsetDate_TextChanged" />
<asp:MaskedEditExtender ID="meeConditionOnsetDate" runat="server" MaskType="date" UserDateFormat="MonthDayYear" Mask="99/99/9999" TargetControlID="txtConditionOnsetDate" ClearMaskOnLostFocus="true" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code Behind
Protected Sub gvQ15_RowUpdating(sender As Object, e As GridViewUpdateEventArgs)
Session("Prescriptions") = gvQ15.DataSource
End Sub
Add the CausesValidation="False" attribute to your CommandField.
<asp:commandfield ShowEditButton="True" CausesValidation="False" HeaderText="Edit"/>
Or,
Add the EnableViewState="True" attribute to your GridView.
For TemplateField use this:
<asp:TemplateField HeaderText="Command">
<ItemTemplate>
<asp:Button CommandName="Edit" Text="Edit" ID="btnEdit" Runat="server"></asp:Button>
<asp:Button CommandName="Delete" Text="Delete" ID="btnDel" Runat="server"></asp:Button>
</ItemTemplate>
<EditItemTemplate>
<asp:Button CommandName="Update" Text="Update" ID="btnUpdate" Runat="server"></asp:Button>
<asp:Button CommandName="Cancel" Text="Delete" ID="btnCancel" Runat="server"></asp:Button>
</EditItemTemplate>
</asp:TemplateField>

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

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....

Gridview edit not working

I have the following gridview:
<asp:GridView ID="GrdAll" runat="server" AutoGenerateColumns="false" ShowHeader="false"
Width="40%" GridLines="None" CellPadding="2" CellSpacing="2">
<AlternatingRowStyle CssClass="AltColor22" />
<RowStyle CssClass="AltColor21" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table width="100%" class="TableBorder">
<tr>
<td valign="top" align="left">
<asp:Label ID="lblName" runat="server" />
<hr />
</td>
</tr>
<tr>
<td valign="top" align="left" class="TableBorder">
<asp:GridView ID="GrdContent" runat="server" AutoGenerateColumns="False"
Width="100%" GridLines="None" CellPadding="2" CellSpacing="2"
OnRowDataBound="GrdContent_RowDataBound" OnRowEditing="GrdContent_RowEditing"><%----%>
<Columns>
<asp:TemplateField HeaderText="Kiosk ID" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblKiosID" runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtKiosk" runat="server" Text ='<%#Eval("HBEmailID") %>'></asp:TextBox>
</EditItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Email ID" HeaderStyle-HorizontalAlign="Left">
<HeaderStyle HorizontalAlign="Left" />
<ItemTemplate>
<asp:Label ID="lblEmail" runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmail" runat="server" Text ='<%#Eval("HBEmailID") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit"
Text="Edit" ></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton3" runat="server" CommandName="Update"
Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In this, edit functionality is not working.
In clicking of edit button, it shows me nothing. (It should show me textbox to edit)
Following is code:
Protected Sub GrdContent_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs)
Dim row As GridViewRow = DirectCast(TryCast(sender, Control).Parent.Parent, GridViewRow)
Dim index As Integer = row.RowIndex
CType(row.FindControl("GrdContent"), GridView).EditIndex = e.NewEditIndex
DBName = CType(row.FindControl("lblName"), Label).Text
bindContentGrid(CType(row.FindControl("GrdContent"), GridView), DBName)
End Sub
Please help me, it does not gives me error, but it also not showing me textbox to edit.
The issue could be on the following code
bindContentGrid(CType(row.FindControl("GrdContent"), GridView), DBName)
If that code does not return any records, then nothing will be displayed. Make sure it is returning some rows.

ASP.NET Dynamically Adding a Gridview

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.

Resources