Set Focus on dropdown list after Partial Postback


——————————————————————-
Problem
——————————————————————-
I have a dropdownlilst that does a Partial postback when it’s index is changed.

<asp:UpdatePanel ID=”UpdatePanel1″ runat=”server”>

<ContentTemplate>

<asp:DropDownList ID=”ddlCountry” runat=”server” CssClass=”combobox” AutoPostBack=”true”

OnSelectedIndexChanged=”ddlCountry_SelectedIndexChanged” onblur=”check_ddlCountry()”

TabIndex=”5″>

</asp:DropDownList>

<asp:DropDownList ID=”ddlState” runat=”server” CssClass=”combobox” AutoPostBack=”true”

OnSelectedIndexChanged=”ddlState_SelectedIndexChanged” onblur=”check_ddlState()”

TabIndex=”6″>

</asp:DropDownList>

</ContentTemplate>

</asp:UpdatePanel>

After it does the Partial postback, it loses focus and when I click tab, I start from the item that

is TabIndex=”1″. I would like to set focus back to this item.

———————————————-
Solution 1
———————————————-

protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
int CountryID = Convert.ToInt32(ddlCountry.SelectedValue);

if (CountryID > 0)
{
FillState(CountryID);
}

ScriptManager.RegisterClientScriptBlock(this, typeof(Page), “FocusOnState”,

“document.getElementById(‘” + ddlState.ClientID + “‘).focus(); “, true);

}

———————————————-
Solution 2
———————————————-
If Your page is AJAX enabled page and you have ScriptManager on the page, then you can use this code.

protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
int CountryID = Convert.ToInt32(ddlCountry.SelectedValue);

if (CountryID > 0)
{
FillState(CountryID);
}

ScriptManager manager = ScriptManager.GetCurrent(this);
manager.SetFocus(“ddlState”);

}

Both Solution Work for me. Hope this help you also..

Thanks, Enjoy Programming.

Advertisements

4 thoughts on “Set Focus on dropdown list after Partial Postback

  1. Hi
    If you can help me….
    I have two dropdown lists. First one is catagory and second is subcatagory. The subcatagory is hidden on page load. when I select the catagory, the subcatagory become visible and popup the items accordingly. But when I submit it to review, and click go back button (to edit the item description…), the subcatagory goes hidden.
    Can you please give me the solution.

    • Hi ,

      So u want to retain those dropdownlist visible as u come up on edit page right !!!

      if u want to do this , there r lots of way of doing it.

      first , when u r in edit mode u will have your subcategory value (id) that u save in the db, so where ever u come in edit page just check whether u have value in the table field (subcategory) u just make it visible with appro value selected.. this is wht i guess normally happen…

      Now if u want to retain hiddenfield value between pages than u need something else to do like session variable , querystring..

      Hope this will help !!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s