ASP.NET + GridView + EmptyDataTemplate

I have an ASP.NET GridView that uses an EmptyDataTemplate. This template is used to collect data when there are no records 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 the user clicks the "saveLinkButton" button on the EmptyDataTemplate, I want to get the values ​​from the text fields and insert a new record into my data source. My question is, how do I get the values ​​of these textboxes when someone clicks the "saveLinkButton"?

Thank!

+2


source to share


2 answers


This thread on asp.net offers a solution to this problem (Note: I haven't tried it)

http://forums.asp.net/p/1436652/3240106.aspx



You need to handle the RowCommand event, get the parent naming container of the control that raises the event (your link), and then search the text boxes using FindControl.

+3


source


Try this to get the name and age values ​​in the grid command line. Before doing that, set the command name saveLinkbutton to MyInsert and remove the onlcik event as u may not be needed.



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

      

+1


source







All Articles