insert html around control

G

geoff

Is there any way, after finding a control on a page, to insert a
literal control either directly before or directly after it? Thanks.
 
F

Fred Hirschfeld

You should be able to do this using the Controls collection, the following
code should be methods added to a base form that you derive from where you
need this functionality. The code is not compiled but more like pseudo
code...

-------------------------------------------------------------------
public void InsertBefore(Control targetCtrl, Control newControl) {
int index=-1;

if (targetCtrl != null && newControl != null) {
for(int i=0; i<this.Controls.Count; i++) {
if (targetCtrl.Equals(this.Controls)) {
index = i;
break;
}
}

if (index != -1) {
this.Controls.Insert(index, newControl);
}
}
}

public void InsertAfter(Control targetCtrl, Control newControl) {
int index=-1;

if (targetCtrl != null && newControl != null) {
for(int i=0; i<this.Controls.Count; i++) {
if (targetCtrl.Equals(this.Controls)) {
index = ++i; // Note the ++ indicates advance and then
capture the value!
break;
}
}

if (index != -1) {
this.Controls.Insert(index, newControl);
}
}
}
 
S

Scott

Override the Render method and write whatever you want to the output;

protected override void Render(HtmlTextWriter out) {
out.Write("<!-- comment -->");
base.Render(out);
out.Write("<!-- end comment -->");
}

Scott
 
G

geoff tyler

Thanks, both of those sound like good ideas. I'll give them a try in
the morning.
 
G

geoff tyler

Overriding the render method would work if my class actually inherited
from some other control, but since I'm just finding a control on a page,
I can't really do anything with it's render method can I? If I'm wrong
here, can you post some example code? Here's what I have so far.

Control c = this.Page.FindControl(msBoundControlID);

So, what do I do with c now to override it's render method? Thanks.
 
S

Scott

I thought maybe you were working with your own Control subclass; you're right, overriding the Render method is going to work for
you.
 
F

Fred Hirschfeld

I would suggest you use the methods I sent earlier and make a base web ui
class with them. Then instead of your code behind deriving from
System.Web.UI.Page it would derive from the new base one you create. Now you
can just call either of those methods I indicated and it should be fine.

public class UIBase : System.Web.UI.Page {

public UIBase() : base()

{

//

// TODO: Add constructor logic here

//

}

... Add methods described earlier ...

}


Fred
 
S

Scott

That should be "isn't going" and not "is going"

Scott said:
I thought maybe you were working with your own Control subclass; you're right, overriding the Render method is going to work for
you.
 

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,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top