hi guys i have a weird prob here..
i have this dropdownlist where different values will hide/show some of the textbox in the page
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
<asp:ListItem>Multiple Choice Question</asp:ListItem>
<asp:ListItem>Free text answer</asp:ListItem>
</asp:DropDownList>
C# code behind, inside Page_Load:
if (Page.IsPostBack)
{
if (DropDownList1.SelectedValue == "Multiple Choice Question")
{
tb_ans.Visible = true;
tb_ans2.Visible = true;
}
else
{
tb_ans2.Visible = false;
tb_ans.Visible = false;
}
}
if (!Page.IsPostBack) //the code within this statement will only load
{
Session["no"] = null;
this.opt3.Attributes["style"] = "display: none;";
.....
opt3.Visible = false;
....
}
Tried to debug and the result was : (i set breakpoint in page load)
when i select different value from the dropdownlist.
3rd time on changing the value, it looks like the dropdownlist keep the same value as the 2nd value.
the loop always go to the if(ddl.selectedvalue==”multiple…”)
. As a result, it will not hide the textbox that i want if the value is change to “free text…”
example :
-
default selection ‘multiple choice..’
-
change to ‘free ans..’ will hide the textbox but other button is not firing.
-
change back to ‘multiple choice…’ it will turn back to ‘free ans..’
-
ddl.selected value is still multiple choice.
what is the problem here..
Because of this:
if (!Page.IsPostBack)Remove that.
That means it only works for the first time of the page load.
If you remove that, that function will run for every page load.
But why don’t you use the
dropdownlist selected index change event??That’s better.
Don’t forget to add
AutoPostBack="true"in your aspxdropdownlistOk. I want to write like this.
in aspx
in cs