views:

728

answers:

1

Hello,

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!

+2  A: 

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.

Adam Pope