Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Adding control & Event Handler at run-time

Reply
Thread Tools

Adding control & Event Handler at run-time

 
 
matt del vecchio
Guest
Posts: n/a
 
      01-23-2004
hello,

For some reason I am having difficulty doing what i thought would be
simple.

i want to loop thru a short list of categories, and for each one
programmatically add new LinkButton control. i think want to be able
to detect which new LinkButton was clicked. so like so:


LinkButton myLKB;

while (myDR.Read()) {
myLKB = new LinkButton();
myLKB.Text = myDR.GetString(1); //category description

myLKB.CommandName = "CategoryID";
myLKB.CommandArgument = myDR.GetString(0); //category id code
myLKB.Command += new CommandEventHandler(this.myLKB_Command);
}

....

private void myLKB_Command(object sender, System.EventArgs e){
Response.Write("in handler!!<br>");
Response.Write(CommandName + ": " + e.CommandArgument);
}


....yet the handler never seems to get called (no response.write is
ever done). i have also tried this w/ a .Click event, since im not
really sure if i should use a .Click or .Command.

any suggestions?


thanks!
matt
 
Reply With Quote
 
 
 
 
Victor Garcia Aprea [MVP]
Guest
Posts: n/a
 
      01-23-2004
Hi Matt,

At which point is the code that creates the linkbuttons being executed? This
sounds like you're creating the child controls too late. Please take a look
at the docs about "Control Execution Lifecycle" to learn more about it.
Here[1] you will find some links to the docs plus some more extra info I've
written about the topic.

[1] http://weblogs.asp.net/vga/archive/2.../11/23498.aspx

--
Victor Garcia Aprea
Microsoft MVP | ASP.NET
Looking for insights on ASP.NET? Read my blog:
http://obies.com/vga/blog.aspx
To contact me remove 'NOSPAM'. Please post all questions to the newsgroup

"matt del vecchio" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> hello,
>
> For some reason I am having difficulty doing what i thought would be
> simple.
>
> i want to loop thru a short list of categories, and for each one
> programmatically add new LinkButton control. i think want to be able
> to detect which new LinkButton was clicked. so like so:
>
>
> LinkButton myLKB;
>
> while (myDR.Read()) {
> myLKB = new LinkButton();
> myLKB.Text = myDR.GetString(1); //category description
>
> myLKB.CommandName = "CategoryID";
> myLKB.CommandArgument = myDR.GetString(0); //category id code
> myLKB.Command += new CommandEventHandler(this.myLKB_Command);
> }
>
> ...
>
> private void myLKB_Command(object sender, System.EventArgs e){
> Response.Write("in handler!!<br>");
> Response.Write(CommandName + ": " + e.CommandArgument);
> }
>
>
> ...yet the handler never seems to get called (no response.write is
> ever done). i have also tried this w/ a .Click event, since im not
> really sure if i should use a .Click or .Command.
>
> any suggestions?
>
>
> thanks!
> matt



 
Reply With Quote
 
 
 
 
CMA
Guest
Posts: n/a
 
      01-23-2004
here u have to follow these steps...
set the ID for each link button while creating it.
myLKB.ID = myDR.GetString(0); // or u can set an integer value THIS SHOULD
BE UNIQUE

and then in the event method (myLKB_Command)
u can access the ID like this...
LinkButton lbtnLink = (LinkButton)sender;
now, "lbtnLink.ID" is the ID of the button u clicked...

hope this helps.
regards,
CMA





"matt del vecchio" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> hello,
>
> For some reason I am having difficulty doing what i thought would be
> simple.
>
> i want to loop thru a short list of categories, and for each one
> programmatically add new LinkButton control. i think want to be able
> to detect which new LinkButton was clicked. so like so:
>
>
> LinkButton myLKB;
>
> while (myDR.Read()) {
> myLKB = new LinkButton();
> myLKB.Text = myDR.GetString(1); //category description
>
> myLKB.CommandName = "CategoryID";
> myLKB.CommandArgument = myDR.GetString(0); //category id code
> myLKB.Command += new CommandEventHandler(this.myLKB_Command);
> }
>
> ...
>
> private void myLKB_Command(object sender, System.EventArgs e){
> Response.Write("in handler!!<br>");
> Response.Write(CommandName + ": " + e.CommandArgument);
> }
>
>
> ...yet the handler never seems to get called (no response.write is
> ever done). i have also tried this w/ a .Click event, since im not
> really sure if i should use a .Click or .Command.
>
> any suggestions?
>
>
> thanks!
> matt



 
Reply With Quote
 
Matt Del Vecchio
Guest
Posts: n/a
 
      01-23-2004
hi Victor,

i have seen that lifecyle chart many times, but it hasnt made anything
clearer. my app goes like this:

- the first time its called, theres a static dropdownlist and a static
button, both design-time. the user thus makes a department selection and
clicks "GO".

- the "GO" button event handler turns the dropdownlist & button
invisible. it then calls another routine, GenerateCategoryButtons(),
passing in the department that was selected from the dropdownlist.

- this button-generating routine loops thru a datareader and thus
dynamically spits out the appropiate buttons, adding them to a
previously-empty placeholder (set on the page ala design-time).

...and that pretty much seems to work: a user loads the app, makes a
selection, hits GO and sees the appropiate buttons.

i just cant seem to handle those buttons' clicks.

does that help any? i beleive i forgot to show the line where the
controls get added to the placeholder, but it's there.


thanks,
matt

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Matt Del Vecchio
Guest
Posts: n/a
 
      01-23-2004
...well, i actually am setting the .ID, i just cut it out because it
didnt see to make a diff--if you dont give a control a name asp.net
finds one to use.

also, i dont think casing a type to the sender will do anything because
the entire event handler routine isnt firing--the Response.Write is
never hit. stepping thru the code it never reaches the handler.


matt


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
matt del vecchio
Guest
Posts: n/a
 
      01-23-2004
btw, i am indeed adding these to the form (forgot to include). looks like this:

LinkButton myLKB;
while (myDR.Read()) {
int itemID = myDR.GetInt16(0);

myLKB = new LinkButton();
myLKB.ID = "lkb" + itemID.ToString();
myLKB.Text = myDR.GetString(1);
myLKB.CssClass = "lk02";


//TRY: set up an eventhandler for the linkbutton's click..?
//myLKB.Click += new System.EventHandler(this.myLKB_Click);

//TRY: set up an eventhandler for the linkbutton's command-
//name/argument pair?
myLKB.CommandName = "NewAreaID";
myLKB.CommandArgument = itemID.ToString();
myLKB.Command += new CommandEventHandler(this.myLKB_Command);

ph1.Controls.Add(myLKB);
ph1.Controls.Add(new LiteralControl(" "));
}
 
Reply With Quote
 
matt del vecchio
Guest
Posts: n/a
 
      01-23-2004
ok, typos aside, i found my problem.

it was in fact w/ the control lifecycle, as Victor suspected.

i was building my dynamic linkbuttons on a static button's click
event. while this did generate them and allow me to add them to the
form, it does not allow me to create new event handlers. to do this
properly, i must create the controls & their events/handlers from
(atleast) Page_Load().

some c++ devs told me that makes sense. but coming from ASP and not a
true OO background, the "why" is still voodoo to me.

anyone care to take a stab at explaining? and not in
one-experienced-OO-developer-to-another fashion?


thanks!
matt
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Event Handler is not called for Control inside User Control ccbalapatel@gmail.com ASP .Net 2 04-04-2007 11:43 PM
Event Handler that creates adds another event handler kaczmar2@gmail.com ASP .Net 1 02-22-2007 07:37 AM
Avoiding &amp; when adding a JavaScript event handler using Attributes.Add() Nathan Sokalski ASP .Net 8 03-01-2006 05:08 AM
Adding "onunload" JavaScript event handler to body in code-behind Laurent Bugnion ASP .Net 1 01-21-2006 09:53 PM
Adding event handler for dynamically created controls =?Utf-8?B?QW5kcmV3IFdpZWQ=?= ASP .Net 3 04-25-2004 07:36 PM



Advertisments