ASP.NET + GridView + EmptyDataTemplate

只愿长相守 提交于 2019-12-22 11:23:28

问题


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!


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/1584913/asp-net-gridview-emptydatatemplate

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!