devan
devan

Reputation: 1653

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.

Upvotes: 0

Views: 1107

Answers (3)

Mayur Borad
Mayur Borad

Reputation: 1295

Use this

    protected void Page_Load(object sender, EventArgs e)
    {
       if(!IsPostback)
       {
         this.xx.Visible = CheckBox1.Checked;
       }
    }

Upvotes: 0

Ray Cheng
Ray Cheng

Reputation: 12576

try this. you need to AutoPostBack=True in DropDownList.

aspx:

<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" 
                AutoPostBack="true" onselectedindexchanged="DropDownList1_SelectedIndexChanged">
                <asp:ListItem Value="city1" Text="city1"></asp:ListItem>
                <asp:ListItem Value="city2" Text="city2"></asp:ListItem>
            </asp:DropDownList>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Label ID="Label3" runat="server" Text="Select Student name :"></asp:Label>
        </td>
        <td>
            <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
        </td>
    </tr>
</table>

code behind:

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

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    this.Label2.Text = this.DropDownList1.Text;
}

Upvotes: 0

Kapil Khandelwal
Kapil Khandelwal

Reputation: 16144

Do this on your Page_Load Event:

this.xx.Visible = CheckBox1.Checked;

Upvotes: 0

Related Questions