2

Using: .NET 3.5SP1, VS2008

I was editting someone else asp.net script, he did the Data retreving at the Page_Load while Page is not postback.

I could see the data was populated into the DropDownList properly even after I refresh, navigates, postback in the page.

I added couples more DropDownList and some CheckBoxes into the script, only the DropDownList I added got populated properly. But not the CheckBox.

So I do a test in a new project, which is similar to its script structure:

ASPX:

<form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
            <asp:ListItem>Item1</asp:ListItem>
            <asp:ListItem>Item2</asp:ListItem>
        </asp:DropDownList>
        <% 
            if (DropDownList1.SelectedValue == "Item2")
            {                
        %>
        <asp:CheckBox ID="CheckBox1" runat="server" Text="CheckBox 1" />
        <asp:TextBox ID="TextBox1" runat="server" Text="" />
        <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true">
            <asp:ListItem>Item1</asp:ListItem>
            <asp:ListItem>Item2</asp:ListItem>
        </asp:DropDownList>
        <%
            }
        %>
    </div>
    </form>

Code-Behind:

 public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.CheckBox1.Checked = true;
                this.CheckBox1.Text = "Hello CheckBox";
                this.TextBox1.Text = "Hello TextBox";
                this.DropDownList2.SelectedValue = "Item2";
            }
        }
    }

So as you see the code, when the page first load, the CheckBox1's text will change, Checked will be true, so as other TextBox and DropDownList2

After I select DropDownList1's item to Item2, when the CheckBox1, TextBox1, DropDownList2 nothing got setted, except the CheckBox1.Text.

Why is this happen?

EDIT:

I tried to put them into Panel, in this way it work. But the problem is the program I am editting is using the format above.. So I am not allow to change them all to Panel.

<form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
            <asp:ListItem>Item1</asp:ListItem>
            <asp:ListItem>Item2</asp:ListItem>
        </asp:DropDownList>
        <% 
            if (DropDownList1.SelectedValue == "Item2")
            {
                this.MyPanel.Visible = true;
            }
            else
            {
                this.MyPanel.Visible = false;
            }
        %>
        <asp:Panel ID="MyPanel" runat="server" Visible="false" >
            <asp:CheckBox ID="CheckBox1" runat="server" Text="CheckBox 1" />
            <asp:TextBox ID="TextBox1" runat="server" Text="" />
            <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true">
                <asp:ListItem>Item1</asp:ListItem>
                <asp:ListItem>Item2</asp:ListItem>
            </asp:DropDownList>
        </asp:Panel>

    </div>
    </form>
King Chan
  • 4,212
  • 10
  • 46
  • 78
  • Why are you putting code in you aspx form? – IrishChieftain Mar 15 '12 at 19:54
  • @IrishChieftain is not me, is my previous developer. I have no choice but to continue to modify his code – King Chan Mar 15 '12 at 19:59
  • Re-create the problem in a new, test page. This markup is horrendous. Put your code in the code-behind file and try debugging it again. – IrishChieftain Mar 15 '12 at 20:21
  • Why? OP is showing right problem already. – Pankaj Mar 15 '12 at 20:24
  • Code does not belong in the markup, period. – IrishChieftain Mar 15 '12 at 20:47
  • @IrishChieftain this is already in a new, test page as I stated in the OP. – King Chan Mar 15 '12 at 20:53
  • Ok. Did you try overloading the SaveViewState and LoadViewState methods to persist state of the controls in question? I frequently have to do this for check boxes and radio buttons selections - known issue. – IrishChieftain Mar 15 '12 at 20:58
  • @IrishChieftain I found a post of your solution in SO. I dont' know if you mean override the Page's SaveViewState and LoadViewState, then retrieve the ViewState at LoadViewState and store it back to the CheckBox. I have tried, but it doesn't work. The CheckBox.Checked got resetted after Page_Load (which is after LoadViewState). After couples hours, I figures out workaround using what `sll` suggested in his comment, hidden field + CheckedOnChanged. – King Chan Mar 16 '12 at 02:12
  • Glad sll's solution worked for you, be sure to give him the points :) CheckBoxes and RadioButtons do not save their state on postback and I often have to override those two page methods in the code-behind... just an FYI – IrishChieftain Mar 16 '12 at 02:22
  • @IrishChieftain Of course. Thanks for your suggest and help :) – King Chan Mar 16 '12 at 02:32

1 Answers1

0

Try out setting EnableViewState and ViewStateMode, perhaps some of the parent controls has disabled it so default Inherit value has been applied.

MSDN:

The default value of the ViewStateMode property for a Web server control in a page is Inherit. As a result, if you do not set this property at either the page or the control level, the value of the EnableViewState property determines view-state behavior.

<asp:CheckBox ID="CheckBox1" runat="server" Text="CheckBox 1" 
              EnableViewState="true"
              ViewStateMode="Enabled" />
sll
  • 61,540
  • 22
  • 104
  • 156
  • try out adding `ViewStateMode` as well – sll Mar 15 '12 at 19:49
  • It doesn't work too. I even copy and paste your code. And I am testing on the new project. So the Page basically nothing except the code I posted above. But if I put all those controls into a `Panel` and set it Visable to false, then Visable it when I select the drop down item and it works... – King Chan Mar 15 '12 at 19:51
  • 1
    Looks like you've faced following issue, see workarounds here: [ASP.Net Checkbox value at postback is wrong?](http://stackoverflow.com/questions/1523606/asp-net-checkbox-value-at-postback-is-wrong) – sll Mar 15 '12 at 19:53
  • So what was the issue, how you fixed it? – sll Mar 16 '12 at 09:58
  • 1
    I think this issues happen when the CheckBox created within a If Statement like above. The behavior is very strange. I tried to override every event on the page, I notice that the value will be reset before reaching `OnPreLoad`. The way I fixed is using your suggested workaround, store values in hidden fields. Then populate the values only when the issues happen. But why the issues happen.... I have no idea. I tried to do the same thing in ASP.NET 4.0 and is the same... – King Chan Mar 16 '12 at 14:32
  • Thanks for elaborating this, it would be helpful for others as well. – sll Mar 16 '12 at 21:39