Search code examples
c#asp.netautopostback

AutoPostBack="true" will do unexpected table invisible in ASP page


I have a Check Box in my asp page. Once I clicked on it, the page display (visible = true) a table id='xx'. This table has two rows. A Dropdown and a Lable.

   <table>
        <tr>
            <td colspan='2'>
                <asp:CheckBox runat="server" ID="CheckBox1" Text="check" Checked="true" AutoPostBack="true"
                    OnCheckedChanged="CheckBox1_CheckedChanged" />
                <table id='xx' runat="server">
                    <tr>
                        <td colspan='2'>
                            Student Information :
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Label ID="Label1" runat="server" Text="Select Student name :"></asp:Label>
                        </td>
                        <td>
                            <asp:DropDownList ID="DropDownList1" runat="server" Width="200px">
                            </asp:DropDownList>
                        </td>
                    </tr>

                    <tr>
                        <td>
                            <asp:Label ID="Label1" runat="server" Text="Select Student name :"></asp:Label>
                        </td>
                        <td>
                            <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>

    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        this.xx.Visible = CheckBox1.Checked;
    }

Once user change the selected drop Down value The label value should change. ex: If user select 'City' in Drop down the Label2.text = Dropdown.selectedvalue.

I have used AutoPostBack="true" for all Check Box control and Drop down while postback.

Issue scenario:

  1. User tick the Check Box
  2. Page visible the Table id='xx'
  3. User change the Dropdown selected value.
  4. The page called the postback and Refreash the page.
  5. The visibled table again non-visible.

Please help me to display change value On label2 once user has changed the value on Drop down without non-visibling the table.

Thank you.


Solution

  • Do this on your Page_Load Event:

    this.xx.Visible = CheckBox1.Checked;