从模板字段访问 GridView 数据

发布于 2024-11-27 22:23:28 字数 1846 浏览 1 评论 0原文

    <asp:GridView ID="gvGrid" runat="server" AutoGenerateColumns="False" 
        DataSourceID="dsDataSource" AllowPaging="True" PageSize="20" >
        <Columns>
            <asp:BoundField DataField="Field1" HeaderText="Field1" 
                SortExpression="Field1" />
            <asp:BoundField DataField="Field2" HeaderText="Field2" 
                SortExpression="Field2" />
            <asp:TemplateField HeaderText="TemplateField1">
                <ItemTemplate>
                    <asp:Label id="lblComments" runat="server" Text=" i use a function to compute"></asp:Label>
                </ItemTemplate> 
            </asp:TemplateField>

          <asp:CommandField ShowSelectButton="True" SelectText="Complete" HeaderText ="Status" />

            <asp:TemplateField HeaderText="Action">
                <ItemTemplate>
                       <asp:Button ID="btnComplete" runat="server" Text="Complete" onclick="btnComplete_Click"/>
                    <asp:Button ID="btnAddComment" runat="server" Text="Add Comment" onclick="btnAddComment_Click" />    
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

protected void btnComplete_Click(object sender, EventArgs e) 
{

    String Field1 = gvGrid.SelectedRow.Cells[1].Text; // throws an error at runtime

   //I want to be able to access the row data do some computation and then be able to insert it into the database 
// Reason why I am trying to use it as a template field instead of a commandfield is because I want to make it not visible when it meets certain condition. 
}

另外,如果您能让我知道更好的方法(也许使用命令字段)以及是否有办法切换其可见性,那就太好了。我也不介意使用 LinkBut​​ton。

    <asp:GridView ID="gvGrid" runat="server" AutoGenerateColumns="False" 
        DataSourceID="dsDataSource" AllowPaging="True" PageSize="20" >
        <Columns>
            <asp:BoundField DataField="Field1" HeaderText="Field1" 
                SortExpression="Field1" />
            <asp:BoundField DataField="Field2" HeaderText="Field2" 
                SortExpression="Field2" />
            <asp:TemplateField HeaderText="TemplateField1">
                <ItemTemplate>
                    <asp:Label id="lblComments" runat="server" Text=" i use a function to compute"></asp:Label>
                </ItemTemplate> 
            </asp:TemplateField>

          <asp:CommandField ShowSelectButton="True" SelectText="Complete" HeaderText ="Status" />

            <asp:TemplateField HeaderText="Action">
                <ItemTemplate>
                       <asp:Button ID="btnComplete" runat="server" Text="Complete" onclick="btnComplete_Click"/>
                    <asp:Button ID="btnAddComment" runat="server" Text="Add Comment" onclick="btnAddComment_Click" />    
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

protected void btnComplete_Click(object sender, EventArgs e) 
{

    String Field1 = gvGrid.SelectedRow.Cells[1].Text; // throws an error at runtime

   //I want to be able to access the row data do some computation and then be able to insert it into the database 
// Reason why I am trying to use it as a template field instead of a commandfield is because I want to make it not visible when it meets certain condition. 
}

Also, it would be great if you could let me know a better way to do it, maybe using the command field and if there is a way to toggle its visibility. I don't mind using LinkButton either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

开始看清了 2024-12-04 22:23:28

你可以这样做:

protected void btnComplete_Click(object sender, EventArgs e)    
{  
     Button btn = (Button)sender;
     GridViewRow gvRow = (GridViewRow)btn.Parent.Parent;

     //Alternatively you could use NamingContainer
     //GridViewRow gvRow = (GridViewRow)btn.NamingContainer;

     Label lblComments = (Label)gvRow.FindControl("lblComments");

     // lblComments.Text ...whatever you wanted to do
}

You can do it like this:

protected void btnComplete_Click(object sender, EventArgs e)    
{  
     Button btn = (Button)sender;
     GridViewRow gvRow = (GridViewRow)btn.Parent.Parent;

     //Alternatively you could use NamingContainer
     //GridViewRow gvRow = (GridViewRow)btn.NamingContainer;

     Label lblComments = (Label)gvRow.FindControl("lblComments");

     // lblComments.Text ...whatever you wanted to do
}
〗斷ホ乔殘χμё〖 2024-12-04 22:23:28

以下是访问 gridview 行的方法:

protected void btnComplete_Click(object sender, EventArgs e)    
{  
     foreach (GridViewRow row in gvGrid.Rows)
      {
            Label lblComments = row.FindControl("lblComments") as Label;
            ....//you can do rest of the templatefiled....
      }
}

here is how you can access to the gridview row:

protected void btnComplete_Click(object sender, EventArgs e)    
{  
     foreach (GridViewRow row in gvGrid.Rows)
      {
            Label lblComments = row.FindControl("lblComments") as Label;
            ....//you can do rest of the templatefiled....
      }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文