0

I have a listview that I used an update panel in it and all codes is in update panel. I used a check box and an event for it that checks if checkbox is checked the textbox visibility should be true and dropdown list visibility should be false. but after checking checkbox the software break and show Object reference not set to an instance of an object. error. my asp.net codes:

<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>

my .cs codes:

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;

    }
}
Noctis
  • 11,507
  • 3
  • 43
  • 82
  • 2
    You might want to check out [this](http://stackoverflow.com/q/14903094/2175939) question and its answers – Sam Apr 15 '14 at 21:03

2 Answers2

1

In your case, you can find the parent UpdatePanel of the check box and find controls inside it like below:

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;

    }
}

EDIT:

I can see that your table structure has serious issue. You can't extend a table through anUpdatePanel this way. You have to move the update panel inside parent tables <td> and create a new table inside the UpdatePanel. Otherwise you will end up messing the rendered html, with doubled controls etc. Your corrected markup should look like this:

<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>
afzalulh
  • 7,925
  • 2
  • 26
  • 37
  • I do this, and no error. but when I run the website and check the checkbox, a copy of edit create on top of orginal edit template. you can see my mean in this link :http://rogatech.somee.com/manage-darkhast-maghale.aspx and you can see on sequential checking checkbox, the contet of last textboxs repeat! – Curious Developer Apr 16 '14 at 05:46
  • 1
    That is because of wrong markup. You can't wrap part of a table with an `UpdatePanel`. See my edit. – afzalulh Apr 16 '14 at 06:54
  • great ! you right. now all the thing is ok. so thanks – Curious Developer Apr 16 '14 at 07:26
0

As it turns out, FindControl only searches at the parent control level.

What you need is a recursive findcontrol method that will find your child control:

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;

Hope that helps!

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321