![]() |
Adding control & Event Handler at run-time
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 |
Re: Adding control & Event Handler at run-time
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" <mdelvecchio@visi.com> wrote in message news:1b2ca81b.0401221631.2eb68aa6@posting.google.c 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 |
Re: Adding control & Event Handler at run-time
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" <mdelvecchio@visi.com> wrote in message news:1b2ca81b.0401221631.2eb68aa6@posting.google.c 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 |
Re: Adding control & Event Handler at run-time
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! |
Re: Adding control & Event Handler at run-time
...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! |
Re: Adding control & Event Handler at run-time
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(" · ")); } |
Re: Adding control & Event Handler at run-time
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 |
| All times are GMT. The time now is 01:36 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.