【问题标题】:chechbox .checked error: Object reference not set to an instance of an object复选框 .checked 错误:对象引用未设置为对象的实例
【发布时间】:2014-04-15 20:42:43
【问题描述】:

我有一个列表视图,我在其中使用了更新面板,所有代码都在更新面板中。我使用了一个复选框和一个事件来检查是否选中了复选框,文本框的可见性应该为真,下拉列表的可见性应该为假。但是在选中复选框后,软件会中断并显示对象引用未设置为对象的实例。错误。我的 asp.net 代码:

<EditItemTemplate>
    <asp:UpdatePanel ID="upchk" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            ///
            <td><asp:Label ID="label17" CssClass="fasele" runat="server" Text="رشته : ">
                </asp:Label></td>
            <td><asp:DropDownList ID="DropDownList2" DataSourceID="SqlDataSource1" 
                DataTextField="reshte" DataValueField="reshteId" AutoPostBack="True" 
                runat="server" ></asp:DropDownList></td>
            <td>

            <asp:TextBox ID="TextBox8" runat="server"></asp:TextBox>
            <asp:CheckBox ID="CheckBox1" runat="server" 
                oncheckedchanged="CheckBox1_CheckedChanged" AutoPostBack="true" 
                EnableViewState="true" /> : افزودن رشته جدید</td>
            /////
        </ContentTemplate>  
        <Triggers>
            <asp:AsyncPostBackTrigger  EventName="checkedchanged" ControlID="CheckBox1" />
        </Triggers> 
    </asp:UpdatePanel>
</EditItemTemplate>

我的 .cs 代码:

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
    CheckBox chk = (CheckBox)ListView1.FindControl("CheckBox1");
    TextBox txtrshte = (TextBox)ListView1.FindControl("TextBox8");
    DropDownList drpreshte = (DropDownList)ListView1.FindControl("DropDownList2");

    if (chk.Checked)
    {
        txtrshte.Visible = true;
        drpreshte.Visible = false;

    }
}

【问题讨论】:

  • 您可能想查看this 问题及其答案

标签: c# asp.net checkbox


【解决方案1】:

在您的情况下,您可以找到复选框的父 UpdatePanel 并在其中找到控件,如下所示:

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
    CheckBox chk = (CheckBox)sender;
    TextBox txtrshte = (TextBox)chk.Parent.FindControl("TextBox8");
    DropDownList drpreshte = (DropDownList)chk.Parent.FindControl("DropDownList2");

    if (chk.Checked)
    {
        txtrshte.Visible = true;
        drpreshte.Visible = false;

    }
}

编辑:

我可以看到您的表结构存在严重问题。您不能以这种方式通过UpdatePanel 扩展表。您必须在父表&lt;td&gt; 内移动更新面板,并在UpdatePanel 内创建一个新表。否则,您最终会弄乱呈现的 html,使用双倍控件等。您更正的标记应如下所示:

<EditItemTemplate>
    <tr><td colspan="3">
        <asp:UpdatePanel ID="upchk" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                ///
                <table><tr>
                    <td><asp:Label ID="label17" CssClass="fasele" runat="server" Text="رشته : ">
                        </asp:Label></td>
                    <td><asp:DropDownList ID="DropDownList2" DataSourceID="SqlDataSource1" 
                        DataTextField="reshte" DataValueField="reshteId" AutoPostBack="True" 
                        runat="server" ></asp:DropDownList></td>
                    <td>

                    <asp:TextBox ID="TextBox8" runat="server"></asp:TextBox>
                    <asp:CheckBox ID="CheckBox1" runat="server" 
                        oncheckedchanged="CheckBox1_CheckedChanged" AutoPostBack="true" 
                        EnableViewState="true" /> : افزودن رشته جدید</td>
                </tr></table>
                /////
            </ContentTemplate>  
            <Triggers>
                <asp:AsyncPostBackTrigger  EventName="checkedchanged" ControlID="CheckBox1" />
            </Triggers> 
        </asp:UpdatePanel>    
        </td></tr>
</EditItemTemplate>

【讨论】:

  • 我这样做了,没有错误。但是当我运行网站并选中复选框时,会在原始编辑模板之上创建一个编辑副本。您可以在此链接中看到我的意思:rogatech.somee.com/manage-darkhast-maghale.aspx 并且您可以在顺序检查复选框中看到,最后一个文本框的内容重复!
  • 那是因为错误的标记。您不能使用UpdatePanel 包装表格的一部分。查看我的编辑。
  • 太棒了!你说得对。现在一切都好了。所以谢谢
【解决方案2】:

事实证明,FindControl 仅在父控件级别进行搜索。

你需要的是一个递归的 findcontrol 方法来找到你的子控件:

public static Control FindControlRecursive(this Control control, string id)
{
  if (control == null)
  {
      throw new ArgumentNullException("control")
  }

  foreach (Control childControl in control.Controls)
  {
      if (childControl.ID == id)
      {
         return childControl;
      }

      Control child = FindControlRecursive(ctl, id);
      if (child != null)
      {
          return child;
      }
}

return null;

希望有帮助!

【讨论】:

  • 页面加载时元素不会被初始化吗? (不是我的东西 asp ... :) (ולמה אתה ער??)
  • 谢谢,但我很困惑!我真的能做什么!?不需要在哪里调用这个函数?
猜你喜欢
  • 2015-01-04
  • 2023-01-30
  • 2011-07-09
  • 2012-08-31
  • 2011-11-21
  • 2012-10-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多