1

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 will 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.

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Tthe button is inside a headertemplate, which allow me to choose button only, not buttonfield.
you can use the "classic" button as well just make sure to assign a CommandName and create an Handlerd for the GridView_RowCommnad
1

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.

1 Comment

Hi, thanks for your time, but it still doesn't work. if (!IsPostBack) doesn't trigger the RowCommand which leads no where, therefore the button inside is also not triggered.
1

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.

1 Comment

if (!IsPostBack) doesn't trigger the RowCommand which leads no where, therefore the button inside is also not triggered. The gridview binding is inside if (!IsPostBack) i suppose

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.