How to? AutoGenerateSelectButton manually

E

eric.olstad

A GridView control in ASP.NET has the option to Auto-Generate the
select button (which is a link that calls __doPostBack which I presume
does a post-back to the server). Is there information on how to
generate this __doPostBack function manually?

The motivation is simple. Using the same __doPostBack([tablename],
'Select$[table row index]') function generated on the 'Select' button
link from AutoGenerateSelectButton flag being set to true, I have made
it so that whenever you click on a row in the GridView, it executes as
if the select button was clicked. However, if I remove the
AutoGenerateSelectButton property, the __doPostBack function is no
longer defined. This is quite annoying because the 'Select' button is
ugly and takes away from the ease of use.

I haven't found much information on this, so any would be greatly
appreciated.
 
D

Darren Kopp

well, what you are looking for is the GetPostBackEventReference()
method. you can inject that into the javascript onclick event or
whatever for the table row, i believe, but i have not done it myself.
you will want to create the table yourself.

hopefully this gives you the start that you need, if you need more
help, shoot me an email.

HTH,
Darren Kopp
http://blog.secudocs.com/
 
E

eric.olstad

Ok, after some playing around I got it. I'm going to post it here for
all those that might come across this issue.

Here's the progression that I imagine most people inexperienced at this
(like me) will go through.

In Page_Load:
if (!IsPostBack){
[...]
// Add __getPostBack to onclick handler for a gridview row
for (int i=0; i < myGridView.Rows.Count; i++)
{
myGridView.Rows.Attributes.Add("onclick",
ClientScript.GetPostBackEventReference(myGridView, "Select$"+i));
}
}

But this still doesn't work. You must add to the @Page directive
EnableEventValidation="false". This may raise some security issues to
be dealt with later.

Ok, so at this point, you can remove the AutoGenerateSelectButton
attribute from the GridView and it will work. However, once the
Postback is performed, __doPostBack is no longer registered. Simply
move the loop out of the "if(!IsPostBack)" scope and it's all set.

Thanks for the info, Darren Kopp.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top