【问题标题】:How to reference textbox from gridview in my codebehind C#如何在我的代码隐藏 C# 中从 gridview 引用文本框
【发布时间】:2014-09-12 14:50:24
【问题描述】:

我发现的所有示例都没有帮助。我的网格视图的页脚中有一个名为“txtMyCoverage”的文本框。在我的代码隐藏中,我只想引用该文本框来说

a = txtMyCoverage.Text;

我试过了

gvLimit.FindControl("txtMyCoverage").ToString();

但它返回空引用。感谢您的帮助!

这是我的网格

<asp:GridView Width="100%" ID="gvLimits"
CssClass="gridView no_min_width"
runat="server"
AutoGenerateColumns="False"
CellPadding="2" ShowFooter="true"
DataKeyNames="PolicyLimitID, LimitID"
HorizontalAlign="Center"
OnRowDataBound="gvLimits_RowDataBound">
<RowStyle HorizontalAlign="Center" />
<Columns>
    <asp:TemplateField HeaderText="Coverage">
        <ItemTemplate>
            <asp:Label ID="txtLimitLetter" runat="server" Width="20px" CssClass="center" Text='<%# Bind("Limit.LimitLetter") %>' Enabled="false" ></asp:Label>
        </ItemTemplate>
        <FooterTemplate>
            <asp:TextBox ID="txtMyCoverage" runat="server" Width="50px" CssClass="center" ></asp:TextBox>
        </FooterTemplate>

</Columns>
</asp:GridView>

【问题讨论】:

  • 你为什么要把控件转换成字符串?
  • 你能展示你如何在标记中定义 txtMyCoverage 你有 runat="" 并且你有分配一个 ID..?请提供/显示更多信息
  • 向我们展示您的 HTML 代码 (DataGrid)。你添加 runat="Server" 属性了吗?
  • 我已经添加了网格代码..取出了其他列,所以它不会那么长。 @MelanciaUK 我只是想从那个文本框中获取文本。

标签: c# asp.net gridview


【解决方案1】:

如果你像这样使用 TextBox:

 <asp:TemplateField HeaderText="Coverage">
        <ItemTemplate>
            <asp:Label ID="txtLimitLetter" runat="server" Width="20px" CssClass="center" Text='<%# Bind("Limit.LimitLetter") %>' Enabled="false" ></asp:Label>
        </ItemTemplate>
        <FooterTemplate>
            <asp:TextBox ID="txtMyCoverage" runat="server" Width="50px" CssClass="center" ></asp:TextBox>
        </FooterTemplate>

然后它你可以像这样在 rowbound 上找到:

关于Grid的RowBoundEvent

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
TextBox txtLimitLetter=(TextBox)e.Row.FindControl("txtLimitLetter");
}

}

或 您可以通过 GridView 行查看

foreach(GridViewRow row in GridView2.Rows)
{
    if (e.Row.RowType == DataControlRowType.Footer)
    {
    TextBox txtLimitLetter=(TextBox)e.Row.FindControl("txtLimitLetter");
    }

}

【讨论】:

  • 感谢@Ganesh_Devlekar 解决了它!
猜你喜欢
  • 1970-01-01
  • 2014-09-09
  • 1970-01-01
  • 1970-01-01
  • 2012-06-25
  • 2011-08-26
  • 2012-08-11
  • 1970-01-01
  • 2010-12-07
相关资源
最近更新 更多