【问题标题】:How to find Null values from Gridview Textbox [C#]如何从 Gridview 文本框 [C#] 中查找 Null 值
【发布时间】:2015-07-14 03:23:46
【问题描述】:

如果文本框值为空,我正在尝试禁用文本框,但我不知道我在哪里做错了以下不起作用,请检查并分享您的建议。

网络表单:

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            onrowdatabound="GridView1_RowDataBound">
            <Columns>
                <asp:TemplateField HeaderText="GROUP">
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox2" commandargument="Now_cmd" runat="server" 
            value='<%# Eval("now") %>'></asp:TextBox>
<cc1:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="Textbox2"></cc1:CalendarExtender>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="ACTIVITY">
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox3" commandargument="after_cmd" value='<%# Eval("afteronehour") %>' runat="server" 
            ></asp:TextBox>
            <cc1:CalendarExtender ID="CalendarExtender2" runat="server" TargetControlID="Textbox3"></cc1:CalendarExtender>

                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

代码隐藏:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        for (int i = 0; i < e.Row.Cells.Count; i++)
        {
            string value = e.Row.Cells[i].Text.ToString();

            if (e.Row.Cells[i].Text.ToString() == string.Empty)
            {

                e.Row.Cells[i].Enabled = false;
            }
        }
    }
}

我的意见:

2015-07-03 09:44:02.380     2015-07-04 09:44:02.380
2015-07-03 09:4`4:53.360    2015-07-04 09:44:53.360
2015-07-03 09:47:00.580     NULL
2015-07-03 09:47:00.580     2015-07-04 09:44:53.360

所以我需要禁用 NULL 值的复选框

【问题讨论】:

  • 你有看到NullPointerException吗?
  • 如果e.Row.Cells[i].Text 为空,那么e.Row.Cells[i].Text.ToString() 必须抛出异常
  • 或e.Row.Cells[i].Text 可能包含"null" 作为字符串
  • M.NasserJavaid,我没有任何 nullpointerException

标签: c# asp.net gridview textbox


【解决方案1】:

您将TextBox 与DataSource 列绑定而不是单元格,因此您应该在TextBox 中而不是单元格中查找文本。您也可以使用FindControl 而不是循环通过GridView 行。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{    
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
      TextBox tb1= (TextBox)e.Row.FindControl("TextBox2");

    if(String.IsNullOrEmpty(Convert.ToString(DataBinder.Eval(e.Row.DataItem, "now"))))
         tb1.Enabled = false; 

      TextBox tb2= (TextBox)e.Row.FindControl("TextBox3");
      if(String.IsNullOrEmpty(Convert.ToString(DataBinder.Eval(e.Row.DataItem, "afteronehour"))))
         tb2.Enabled = false; 
   }
}

【讨论】:

  • 阿迪尔,NullreferenceException 来了
  • 如果 FindControl 返回 null,这意味着您在当前行中没有任何具有给定 id 的控件。确保提供正确的 id
  • Adil,请检查我的 gridview 编码,我有这个控件,如果我尝试使用 if (e.Row.RowType == DataControlRowType.DataRow) 没有错误,但整个列被禁用,如果文本框值为空,我只需要禁用特定单元格
  • 阿迪尔,我也试过了,但整个列都被禁用了,但我需要禁用特定的空单元格
  • 这是我的错误,因为在 RowDataBound 上尚未分配值,所有 TextBoxes 都将为空,因此我们需要在网格中查找或 DataItem 而不是 TextBox 以获取 null 或空值..跨度>
【解决方案2】:

您应该使用IsNullOrEmpty() 方法检查null 和empty 字符串,该方法将检查字符串是null 还是empty。

if (string.IsNullOrEmpty(e.Row.Cells[i].Text))
{
  e.Row.Cells[i].Enabled = false;
}

【讨论】:

    【解决方案3】:
    for (int r = 0; r < GridView1.Rows.Count; r++)
    {
        TextBox tb1 = (TextBox)GridView1.Rows[r].FindControl("TextBox2");
    
        TextBox tb2 = (TextBox)GridView1.Rows[r].FindControl("TextBox3");
    
    if(tb1.Text==null)
    {
    tb1.Enabled=false;
    }
    else
    {
    tb1.Enabled=true;
    }
    if(tb2.Text==null)
    {
    tb2.Enabled=false;
    }
    else
    {
    tb2.Enabled=true;
    }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多