ASP.NET + GridView + EmptyDataTemplate
I have an ASP.NET GridView that uses an EmptyDataTemplate. This template is used to collect data in the event that no records exist in my data source. My GridView source looks like this:
<asp:GridView ID="myGridView" runat="server"
DataKeyNames="ID" OnRowEditing="myGridView_RowEditing"
OnRowCancelingEdit="myGridView_RowCancelingEdit"
OnRowUpdating="myGridView_RowUpdating"
ShowFooter="True" EnableModelValidation="True">
<Columns>
<asp:BoundField DataField="ID" Visible="false" />
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="nameFooterTextBox" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Age">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Age") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Age") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ageTextBox" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Options" HeaderStyle-HorizontalAlign="Left"
ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true" >
</asp:CommandField>
</Columns>
<EmptyDataTemplate>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>Name</td>
开发者_高级运维 <td>Age</td>
<td>Options</td>
</tr>
<tr>
<td><asp:TextBox ID="nameTextBox" runat="server" /></td>
<td><asp:TextBox ID="ageTextBox" runat="server" /></td>
<td><asp:LinkButton ID="saveLinkButton" runat="server" Text="save" OnClick="saveLinkButton_Click" /></td>
</tr>
</table>
</EmptyDataTemplate>
When a user clicks the "saveLinkButton" in the EmptyDataTemplate, I want to get the values from the TextBoxes and insert a new record into my data source. My question is, how do I get the values of those text fields when somebody clicks the "saveLinkButton"?
Thank you!
This thread over at asp.net proposes a solution to this problem (Note: I haven't tried it out)
http://forums.asp.net/p/1436652/3240106.aspx
You need to handle the RowCommand event, get the parent naming container of the control that raise the event (your linkbutton) and then search for the textboxes using FindControl within that.
Try this to get the values of the name and age in row command of the grid.. Before doing this set the command name of the saveLinkbutton to MyInsert and remove the onlcik event as u may not need it.
protected void yourGridview_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("MyInsert"))
{
TextBox nameTextBox= gvEligibility.Controls[0].Controls[0].FindControl("nameTextBox") as TextBox;
TextBox ageTextBox= gvEligibility.Controls[0].Controls[0].FindControl("ageTextBox") as TextBox;
string name=nameTextBox.Text();
string age=ageTextBox.Text();
//save code here
}
}
精彩评论