【问题标题】:ASP.NET + GridView + EmptyDataTemplateASP.NET + GridView + EmptyDataTemplate
【发布时间】:2012-06-08 14:10:50
【问题描述】:

我有一个使用 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”时,我想从 TextBoxes 中获取值并将新记录插入到我的数据源中。我的问题是,当有人点击“saveLinkBut​​ton”时,我如何获取这些文本字段的值?

谢谢!

【问题讨论】:

    标签: asp.net gridview


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      试试这个以获取网格的行命令中的名称和年龄的值.. 在执行此操作之前,请将 saveLinkbutton 的命令名称设置为 MyInsert 并删除 onlcik 事件,因为您可能不需要它。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-06
        • 1970-01-01
        • 1970-01-01
        • 2011-02-27
        • 2016-06-14
        • 1970-01-01
        • 2023-03-13
        • 1970-01-01
        相关资源
        最近更新 更多