0

我正在尝试使用下面的代码禁用特定的 grdview 行,但它并没有禁用该行。我100%确定绑定到gridview的列中有一个名为“Total Expenses”的值。更奇怪的是,我删除了 if 条件(accountTextBox.Text == "Total Expenses") 以查看是否所有行都被禁用但只有第一行被禁用。对此有什么想法吗?

更多信息:我正在使用带有模板字段(ASP.NET、C#)的 gridview。我还运行了调试器,发现accountTextBox显示为 NULL。我很困惑!

我很感激你可能有的任何想法。谢谢

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
        if (!tableCopied)
        {
            originalDataTable = ((System.Data.DataRowView)e.Row.DataItem).Row.Table.Copy();
            ViewState["originalValuesDataTable"] = originalDataTable;
            tableCopied = true;
            TextBox accountTextBox = (TextBox)e.Row.FindControl("AccountTextBox");

                if (accountTextBox.Text == "Total Expenses")
                {
                    e.Row.Enabled = false;
                }

        }
}
4

3 回答 3

1

我假设您的标记看起来像这样:

<asp:TemplateField HeaderText="SomeField" SortExpression="SomeField">
    <EditItemTemplate>
        <asp:TextBox ID="AccountTextBox" runat="server" Text='<%# Bind("SomeField") %>'></asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Bind("SomeField") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

在这种情况下AccountTextBox,只有当 e.Row.State 处于编辑模式时,RowDataBound 才可用。根据您的数据源中提供的更新功能,它可能可用也可能不可用……但那是另一回事。

通常,您不使用 TextBox 来显示数据,但可以说尝试填充“只读”文本框,然后您需要做的就是将 AccountTextBox 从 EditTemplate 移动到 ItemTemplate,您应该很高兴。

于 2013-06-04T22:40:09.640 回答
0

tableCopied变量可能是问题所在。它将在第一行设置为 true,这将限制其余行进入 if 条件并实际上被禁用。

First row:   
  //tableCopied is false
  if(!tableCopied) // pass
      tableCopied = true

Other rows:
  //tableCopied is true
  if(!tableCopied) // wont pass

请注意,GridView1_RowDataBound对于同一回发的每一行,该事件都会发生一次,并且每个类成员都会在回发期间持久化。

于 2013-06-04T21:48:42.747 回答
0

使用以下代码隐藏:

public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            gvTest.DataSource = new List<string>() { "test1", "test2" };
            gvTest.DataBind();
        }

        private bool tableCopied = false;
        private System.Data.DataTable originalDataTable;

        protected void gvTest_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
                if (!tableCopied)
                {
                    //originalDataTable = ((System.Data.DataRowView)e.Row.DataItem).Row.Table.Copy();
                    //ViewState["originalValuesDataTable"] = originalDataTable;
                    //tableCopied = true;
                    TextBox accountTextBox = (TextBox)e.Row.FindControl("AccountTextBox");

                    if (accountTextBox.Text == "Total Expenses")
                    {
                        e.Row.Enabled = false;
                    }

                }
        }
    }

这个标记:

<asp:GridView runat="server" ID="gvTest" OnRowDataBound="gvTest_RowDataBound">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:TextBox ID="AccountTextBox" runat="server">Total Expenses</asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

我没有重现错误。两行都对我禁用。

于 2013-06-04T21:53:16.727 回答