Determining control that caused a postback

G

Guest

string ctrlname = page.Request.Params.Get("__EVENTTARGET");
if (ctrlname != null && ctrlname != string.Empty)
{
return this.Page.FindControl(ctrlname);
}


I have a form on which I was trying to determine which user-action caused a
postback. In various places I found the code above that works for most
controls.

The problem I have been having is with radiobutton list control. A value
for "ctrlname" will be found, but this.Page.FindControl(ctrlname) returns a
null value. The problem appears to be related to the value for ctrlname that
is returned. Its construction appears to be related to the type of control.
Dropdown list controls seem to have the format
pagename:controlname
while my radiobutton list control has the format
pagename_controlname_n, where n is 1, 2, 3, ....

For my needs, I wrote code to extract the real control name which is then
passed to the Page.FindControl method.

My question has to do with the values returned by

page.Request.Params.Get("__EVENTTARGET");

Are there rules for how it constructs the value it returns for each type of
control, and if so, where can I find them?

Thanks
 
M

Marina

Why do you need to figure this out?

Handle the events of the various controls, the click for the buttons,
selectedindexchanged for the dropdowns, etc.
 
G

Guest

new to the asp.net world, so pardon my ignorance. I have a radiobutton list
control, a couple dropdown list controls, and a grid control on the form.
When the user selects items from the list controls, the grid would update
with the proper data. Originally I was handling loading the proper data into
the controls during the postback and wanted to know which one caused the
post-back, so I could take different action during the post-back. I think I
just started down this path and just didn't see the simpler solution. Thanks
for pointing me back in the right direction.

For curiosity sake, I still am wondering if there are rules to the value
returned by

page.Request.Params.Get("__EVENTTARGET");


thanks
 
B

Bruce Barker

Request.Params.Get("__EVENTTARGET") returns the name that FindControl
expects. but you need to recreate the controls first for FindContol to work.

note: the button and imagebutton controls do not use _EVENTTARGET

-- bruce (sqlwork.com)
 
G

Guest

Can you elaborate on what you mean by "recreate the controls first for
FindControl to work"?

thanks
 

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

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,815
Latest member
treekmostly22

Latest Threads

Top