1,105,633 Community Members

Update an UpdatePanel from dropdownlists

Member Avatar
while(!success)
Junior Poster in Training
61 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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!

Member Avatar
G_Waddell
Practically a Posting Shark
821 posts since Nov 2009
Reputation Points: 131 [?]
Q&As Helped to Solve: 137 [?]
Skill Endorsements: 13 [?]
 
0
 

Hi,
Do your code behind functions fire on postback?

Member Avatar
while(!success)
Junior Poster in Training
61 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
while(!success)
Junior Poster in Training
61 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
ggamble
Junior Poster
151 posts since Mar 2006
Reputation Points: 1 [?]
Q&As Helped to Solve: 17 [?]
Skill Endorsements: 4 [?]
 
0
 

Be sure to register both dropdowns in the scriptmanager.

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