473,403 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

how do I access control I added programmatically?

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)?
Nov 19 '05 #1
4 1391
If you create a control dynamically, you are expected to create it again
upon each postback.
Use an Addhandler statement to hook up events.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"RN" <re*************@please.com> wrote in message
news:PL******************@twister.socal.rr.com...
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)?

Nov 19 '05 #2
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
"RN" <re*************@please.com> wrote in message
news:PL******************@twister.socal.rr.com...
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)?

Nov 19 '05 #3
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.

"RN" <re*************@please.com> wrote in message
news:PL******************@twister.socal.rr.com...
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)?

Nov 19 '05 #4
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

"RN" wrote:
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)?

Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Fabio Rossi | last post by:
Hi! I'm learning C++ from the book "Thinking in C++". Now I'm reading about nested classes and access control. I have written this code #include <iostream> class Outer { private: int...
8
by: deko | last post by:
I'm trying to find a way to set form/control properties programmatically. In a nut shell: 1. Open a database 2. Open a form in design view 3. Do something like this: For Each prp In...
1
by: TK | last post by:
What's the best way to implement roll-base access control with forms authentication? I have a IIS6+ASP.NET server which hosts some ASP.NET web applications as separated path that's like...
0
by: William F. Zachmann | last post by:
A web site that will run on Windows Server 2003 and IIS 6.0 needs to provide three levels of access, one for the public and two others for two levels of subscribers. This is a port of a prior site...
4
by: JimC | last post by:
On my main form in a C# program, I create an instance of another form that contains a ListView control, in the usual way, that is: public class frmMain : System.Windows.Forms.Form { // ...
6
by: Notgiven | last post by:
I am considering a large project and they currently use LDAP on MS platform. It would be moved to a LAMP platform. OpenLDAP is an option though I have not used it before. I do feel fairly...
0
by: islandfong | last post by:
Hi, I am using ASP.NET 2.0 to implement a user access control system. The idea is that I would allow users (admin) to create new role, new user, assign user to role and the role is given...
5
by: Lewis Perin | last post by:
Is anyone aware of robust software, suited to a preexisting PHP application, that handles permissions for various types of requests by role rather than user ID? I'm speaking of maintaining/editing...
8
by: xz | last post by:
Why C++ (as well as Java) adopts class-based access control instead of instance-based access control? I had never paid attention to whether an access-control is class-based or instance-based but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.