简体   繁体   中英

DropDownList in my masterpage gets bind every time childpage loads - How can I avoid that binding?

I have a DropDownList in Masterpage that I fill on Page_Load() event but when child page loads , this DropDownList is binding again because its in Page_Load() event and selected value in this list is lost. Is there any way I can avoid this repeated binding in Masterpage?

I'm passing the selected value from DropDownList through QueryString and showing items according that value in child page.

I'm writing code in C# in ASP.Net using visual studio 2011.

 if (!IsPostBack)
    {
        ViewState["id"] = null;
        if (drpdwnCategory.Items.Count < 1)
        {
            fillDropList();
        }
    }
    if (Request.QueryString["catId"] != null)
    {
        drpdwnCategory.SelectedIndex = _drpdwnCategory.Items.IndexOf(drpdwnCategory.Items.FindByValue(enrpt.getdecrept(Request.QueryString["catId"])));
    }
 if(!IsPostback)

{

// code to only run at first page load here
// fill dropdown

} 

You still have to bind the dropdown on page load of the child pages, otherwise your dropdown would not have any values inside.

What you can do is to check for the querystring on your masterpage, and then set the SelectedValue property of the dropdown to whatever is in the querystring during your dropdown databind.

You can check if dropdown has items. and if it has items you dont bind again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM