how do I access control I added programmatically?

R

RN

In certain situations, I add a datagrid and a button beneath it. All
programmatically added in the "code behind". I add them to a cell with
cell.add(datagrid) and cell.add(button). Take the button for example.
Usually, if the button were there statically, in the ASPX "html" code, I
would simply put some code I want to run in the button's click routine in my
code-behind. But it seems I can't have a button click routine that will
fire because the button is being added programatically and it doesn't
recognize the button as an object when the page comes back on postback.
How am I supposed to have a button click routine? Am I somehow declaring
the button incorrectly so it loses state or is there just no way to have the
button be recognized in the code behind if it was added programmaticaly
before the postback (e.g. when it posts back, even something simple like
button.visible will return an exception that there is no such object)?
 
S

Steve C. Orr [MVP, MCSD]

If you create a control dynamically, you are expected to create it again
upon each postback.
Use an Addhandler statement to hook up events.
 
K

Karl Seguin

RN:
The button has to be recreated on postback and the eventhandler added then.

So, you are probaby doing something like:

if not page.IsPostBack then
dim btn as new Button()
cell.Controls.Add(btn)
end if


You need to also do this when doing a postback, so remove the if/endif and
add the event handler

dim btn as new Button()
AddHandler btn.Click, AddressOf Btn_ClickFunction
cell.Controls.add(btn)

This has to be done before a certain point, for example it can't be done in
PreRender...but can be done in OnLoad on OnInit

Karl
 
O

Ollie Riches

when the button is added programmatically you have add an event handler for
the button click as well.

To Find a control that is contained in a datagrid you can use

DataGrid.FindControl("<control name>")

check out an example at (VB.Net)
http://www.codeproject.com/aspnet/griddemo2.asp


--
HTH

Ollie Riches
http://www.phoneanalyser.net

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a programmer
helping programmers.
 
G

Guest

You might also consider using the "handles" clause (vb) of a subroutine, e.g.
[handles myButton.click]. Not sure if that's more/less efficient than
addhandler (I do know there is a difference, something to do with how the
handlers are stacked). As stated out here the key is that your button is
still around after postback, else the events will be lost.

Also, if you have several similar buttons, either via addhandler or handles
it'll clean up your code to use a single handler for them all, and use
CommandName and/or CommandArgument to distinguish the buttons.

Bill
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top