I have a Dropdown with two values "P" and "Q" with
AutoPostBack= true. I have another Custom WebControl called BDTextBox, this custom textbox have a Boolean property called LabelMode , on setting it to true would render the textbox as label.This LabelMode Property is set using ViewState and not using private variable.

If(Dropdown.selectedvalue="P") 
 {
    BDTexTbox.LabelMode = false;
 }
 else
 {
    BdTextBox.LabelMode = true;
 }

my need is that if Dropdown has value "P" the labelmode = false and on "Q" labelMode = true; my code looks somewhat like this.

I have tried writting this code in PanelCreation , OnPrerender but there seems to be a issue with it.

As i hit this code and suppose Dropdown value is "P" , it goes in if loop and as per code it sets value as false, that means it is not rendered as label.
But after the page is loaded completely it is rendered as label still that it has value of true in it.

Now if i change the Dropdown value to "Q" , it again hits code it goes in else loop and sets value of "true", but after page loads it is not rendered as label.

What is noticed that the value set is reflected in next postback. If if i set value as true and previously it was false, so once page is loaded it picks up older value and not which is set at runtime.

How the value set at runtime can be made to refelect in page in same postback itself ? why it reflects in next postback ? I guesss its somehting related to viewstate.

Dani AI

Generated

Brief diagnosis based on 's description and 's typo note: this is almost always a page-lifecycle / child-control-creation ordering problem, not a ViewState bug. ViewState is restored before postback events, and if BDTextBox builds its child controls (Label vs TextBox) earlier in the lifecycle using the old ViewState value, changing LabelMode in the SelectedIndexChanged handler won't rebuild those children — so the UI keeps showing the previous state until the next request.

Two reliable fixes:

  • Rebuild the BDTextBox children immediately after changing the property. Add a public helper on the control that clears and recreates children, then call it from the dropdown event:
public void RecreateChildControls()
{
    Controls.Clear();
    ChildControlsCreated = false;
    EnsureChildControls(); // calls CreateChildControls()
}
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
    bdTextBox.LabelMode = (ddl.SelectedValue == "Q");
    bdTextBox.RecreateChildControls();
}
  • Simpler and safer: create both a Label and a TextBox in CreateChildControls and toggle their Visible flags when LabelMode changes. That keeps the control tree stable (so ViewState/postback data still binds) and just flips presentation:
protected override void CreateChildControls()
{
    Controls.Clear();
    _label = new Label { ID = "lbl" };
    _textBox = new TextBox { ID = "txt" };
    Controls.Add(_label);
    Controls.Add(_textBox);
    UpdateVisualMode();
}

private void UpdateVisualMode()
{
    _label.Visible = LabelMode;
    _textBox.Visible = !LabelMode;
}

Troubleshooting tips: put breakpoints in the property setter, CreateChildControls and the dropdown event to watch ordering; check whether BDTextBox implements IPostBackDataHandler (it might overwrite state during LoadPostData); and ensure comparisons use the correct operator for the language (as flagged). If preserving inner control values across a rebuild is required, capture those values before clearing controls and restore them after recreation.

Recommended Answers

All 2 Replies

Use like this and let me know,

If(Dropdown.selectedvalue=="P")

Use like this and let me know,

If(Dropdown.selectedvalue=="P")

sorry , that's what it is. it was typo error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.