1.11M Members

Update an UpdatePanel from dropdownlists

 
0
 

Hi guys,

I've been trying to update an updatepanel with the selected values of two dropdownlists. The updatepanel is supposed to be updated on selectedindex changed of the second dropdown. The update works on the first initial index changed, but does not work for any subsequent index change.

The markup is as such:

<asp:UpdatePanel runat="server" ID="pnl1" UpdateMode="Always">
    <ContentTemplate>
        <div id="blah" runat="server" class="well well-large" style="height:100px;">
        <asp:DropDownList ID="ddl1" runat="server" OnSelectedIndexChanged="func1_click" AutoPostBack="true" /><br />
        <asp:DropDownList ID="ddl2" runat="server" OnSelectedIndexChanged="func2_click" AutoPostBack="true"  />
        </div>
    </ContentTemplate>
</asp:UpdatePanel>

<asp:UpdatePanel runat="server" ID="pnl2" UpdateMode="Always">
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="ddl2" EventName="SelectedIndexChanged" />
        </Triggers>
        <ContentTemplate>
            <asp:Label ID="lbl" runat="server" />
        </ContentTemplate>
</asp:UpdatePanel>

The Codebehind functions:

protected void func1_click(object sender, EventArgs e)
{
    if (ddl1.SelectedValue == "")
        ddl2.Visible = false;
    else
    {
        SqlCommand cmd = new SqlCommand("SELECT blah blah...", con);
        con.Open();
        cmd.Parameters.AddWithValue("@id", ddl1.SelectedValue);

        ddl2.DataSource = cmd.ExecuteReader();
        ddl2.DataTextField = "text";
        ddl2.DataValueField = "value";
        ddl2.DataBind();
        ddl2.Items.Insert(0, new ListItem("--Select--", ""));
        ddl2.SelectedIndex = 0;
        ddl2.Visible = true;
    }
}

protected void func2_click(object sender, EventArgs e)
{
    lbl.Text = ddl1.SelectedItem + " > " + ddl2.SelectedItem;
}

When I try to toggle the the second dropdownlist more than once, it does not update the update panel. I have to toggle the first dropdownlist and then the second dropdownlist to see results. If this is not clear, please let me know. Thanks for your help!

 
0
 

Hi,
Do your code behind functions fire on postback?

 
0
 

func1 always fires, func2 fires on the first indexchange, but not subsequent index changes

 
0
 

Upon further investigation, ddl2 always defaults to the value of the first index. Postback issue?

 
0
 

Be sure to register both dropdowns in the scriptmanager.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: