473,385 Members | 2,005 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,385 software developers and data experts.

Write web controls out to a page and associating event handlers

Hey all, I realize this must be a fairly common requirement in ASP.NET but I
have yet to come across the right way to do it.

Basically, right now I have a single asp:label on my webpage and I am
writing out to the Text property of that Label object.

However, now I would like to write a control like a button to that Text and
associate an event handler with that button.

How should I do that - or am I going about this in the wrong way - i.e.
should I be creating my own custom web control?

Anyway, here is my attempt at writing an asp:button out to the page and
associating an event handler with it:
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);
Button button = new Button ();
button.Click += new EventHandler(nextButtonClick);
htw.Write(button.);
htw.Flush();
MyLabel.Text += sb.ToString();

Thanks for any help,
Novice
Nov 18 '05 #1
1 1317
"Novice" <6tc1ATqlinkDOTqueensuDOTca> wrote in message
news:86**********************************@microsof t.com...
Hey all, I realize this must be a fairly common requirement in ASP.NET but
I
have yet to come across the right way to do it.

Basically, right now I have a single asp:label on my webpage and I am
writing out to the Text property of that Label object.

However, now I would like to write a control like a button to that Text
and
associate an event handler with that button.

How should I do that - or am I going about this in the wrong way - i.e.
should I be creating my own custom web control?


I strongly recommend that you use a property named "Text" for text and
nothing else. Even if something else (like HTML) works today, I'd worry that
the authors of the control might forget that "Text" is not juts used for
Text, and may break your code by making the control work according to their
assumptions...

If you want to dynamically display one of several controls (like your button
example), there are several ways to do this. The simplest is to place all of
the possible controls on the page with Visible set to false. Then, at
run-time, change the Visible property of the control you want to see to
true. The overhead of this isn't as high as it sounds, since a control with
the Visible property clear will not render any HTML.

Another way is to put a Panel control on the page, and at run-time, add the
desired control (or controls) to the Panel:

Button myButton = new Button();
myButton.Text = "Button";
myPanel.Controls.Add(myButton);

---
John Saunders
Nov 18 '05 #2

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

Similar topics

2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
0
by: Kenton Smeltzer | last post by:
Hello All, I am having a problem with events and the addition of controls on a page I am developing. First let me tell you what I have tried and then maybe someone can see something I missed. ...
1
by: Greg | last post by:
In my ASP.NET application I have a button that when pressed, data needs to be saved based on what the user typed in. I have other controls on the same page that should be updated as a result of the...
1
by: Lamont Adams | last post by:
Hi all, I've created numerous custom controls of varying complexity, but I've been on this problem for a day and a half, and I can't figure this mystery out. I hope one of you kind folks can...
3
by: Ed Chiu | last post by:
Hi, I plan to use panel to store multiple HTML text in a web form page, here is the code snippet that add HTML into panel: Dim lit1 As New Literal lit1.Text = strHTML...
22
by: Mr Newbie | last post by:
I was thinking about developing a workflow application yesterday and was musing over the different approaches than one could take in restricting specific actions on a ticket( Form ) at any said...
1
by: weboweb | last post by:
Hello aspnet experts! I have a design question for the more experienced developers (more than me at least :-)). 1) I have a page in the application I'm building that displays a web user...
1
by: Dica | last post by:
i've got a script that loops through a dataset and creates dynamic web controls and event handlers: while (oDr.Read()){ RadioButton oRb = new RadioButton(); oRb.ID = oDr.ToString();...
3
balabaster
by: balabaster | last post by:
I've got a user control that builds a table of dynamic data based on a :LINQ class holding the data. The data is loaded using the LoadData(DataInstance) method. The table it builds contains a...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.