开发者

C# ASP .Net, triggering button control inside Gridview

开发者 https://www.devze.com 2023-03-10 10:20 出处:网络
I have a question about button inside the gridview. As the title says, I designed a gridview with a button inside. Whenever I trigger the button, it always binding the gridview before it can reach the

I have a question about button inside the gridview. As the title says, I designed a gridview with a button inside. Whenever I trigger the button, it always binding the gridview before it can reach the button event.

Say, onLoad page, I pull data and bind them to a gridview, then I would like to do a mass update which w开发者_如何学JAVAill be triggered by the button inside it. If i put !IsPostBack, it never triggers the button event, which means it always has to read the data bind them into gridview again then execute the button event.

Problem is, I have a huge data and it takes much time to update because of it. I am wondering if there are any ways to trigger the button event without rebinding the gridview again? Is it because of the button inside the gridview so it is necessary to rebind again before it could read the button event?

Any suggestion would be really appreciated.


Why don't you use the RowCommand instead of the onclick event? you can add a command name property to the button to understand what operation you need to do

Have a look at the link below and just replace the ButtonField with a classic button. don't forget to assign a CommandName

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewcommandeventargs.aspx


You should not use the button event, but the GridView.RowCommand event instead. See here. If you do this in combination with the if (!IsPostBack) check in Page_Load, it should work for your scenario.


your button must be triggered by OnRowCommand event and ! IsPostBack shouldn't prevent triggering your button

protected void GridView_RowCommand(object source, GridViewCommandEventArgs e)
{
    if(e.CommandName=="Update")
    {
    // put your update code here
    }
}

and you can use ajax to update your datasource without rebinding your GridView and update your UI using javascript.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号