Click here to Skip to main content
15,073,911 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a gridview with a template field having dropdownlist. On selectedindexchanged of this dropdownlist data will be populated in that grid view row. I am binding dropdownlist items in pageload. I cant put it in !IsPostBack because i am using web user controls need to bind on every page load. On click of save button dropdownlist seletedindexchanged event is firing for every row in the gridview and gridviewrow loosing any changes made. Can you please tell me how can i avoid firing dropdownlist selectedindex changed event. It need to be fired on only selectedindexchanged of dropdownlist.

Thanks
Posted

No, you have to put the GridView Bind function inside the !IsPostBack, otherwise there will be many problems including the problem you have specified.

This is because, before every event fires, it will go to page load first. And if you are binding the GridView on PageLoad, then it will be newly loaded again.

So, when the event will be fired after that, it will show the old values as the Grid is already loaded with old values.

So, put your code in !IsPostBack.

Now as per your requirement, you need to refresh the Grid sometimes.
For that, just call the Bind function inside the event or function you want to bind it. Like below.
C#
protected void Button1_Click(Object sender, EventArgs e)
{
    // Call Bind function here. It will refresh the Grid if you need it. 
    // Don't call Bind function in every PageLoad. Always check !IsPostBack.
    BindGrid();
}
One more thing you can try. You can check whether postback happen due to Save button click if yes than don't do data binding.

Below condition check whether postback happen due to save button click.


C#
bool bindData = true;
        if (Request[btnSave.UniqueID] != null)
        {
            bindData = false;
        }



you can bindData flag wherever your binding data.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900