ASP.NET + GridView + 空数据模板

标签 asp.net gridview

我有一个使用 EmptyDataTemplate 的 ASP.NET GridView。该模板用于在我的数据源中不存在记录的情况下收集数据。我的 GridView 源代码如下所示:

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

当用户单击 EmptyDataTemplate 中的“saveLinkBut​​ton”时,我想从文本框中获取值并将新记录插入到我的数据源中。我的问题是,当有人单击“saveLinkBut​​ton”时,如何获取这些文本字段的值?

谢谢!

最佳答案

asp.net 上的这个线程提出了这个问题的解决方案(注意:我还没有尝试过)

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

您需要处理 RowCommand 事件,获取引发该事件的控件(您的链接按钮)的父命名容器,然后在其中使用 FindControl 搜索文本框。

关于ASP.NET + GridView + 空数据模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1584913/

相关文章:

ASP.NET @Register 与 @Reference

javascript - 在 asp.net 中单击按钮时调用 jQuery 函数

c# - Ajax.ActionLink 不调用 Controller Action

Android从gridview布局索引转换成行和列

asp.net - 如何突出显示 Gridview 中的最大值

c# - 如何在 TemplateField 中更改 ItemTemplate 的命令文本和 ImageButton

c# - 如何获取发起重定向的服务器IP

asp.net - 带有 jquery 的 IE 复选框无法正常工作

ios - 在uigridview中的行和列设置图像

asp.net - 编辑 GridView 文本框时捕获 Enter 键