Converting html string to object

W

Wouter vanEck

Hi,

I need to find a way to read a html type string from a database and add it
to a placeholder control on the page and then I need to be able to populate
the fields that were in that html string at run time.

The html string may look like this "<table><tr><td>First Name</td><td><input
name=_firstname runat=server id=_firstname ....></td></tr></table>"

Is there a way to convert this string into an object which you could access
like _firstname.text or _firstname.innerhtml, any thing to being able to set
the value would be great.

Is there anyone who knows about an article or has an idea how to go about
doing this??

Thanks,

Wouter
 
V

vMike

Wouter vanEck said:
Hi,

I need to find a way to read a html type string from a database and add it
to a placeholder control on the page and then I need to be able to populate
the fields that were in that html string at run time.

The html string may look like this "<table><tr><td>First
Name said:
name=_firstname runat=server id=_firstname ....></td></tr></table>"

Is there a way to convert this string into an object which you could access
like _firstname.text or _firstname.innerhtml, any thing to being able to set
the value would be great.

Is there anyone who knows about an article or has an idea how to go about
doing this??

Thanks,

Wouter
You can put a placeholder (say it is called plc1) on a page and then in code
do the following
plc1.controls.add(New literalcontrol(yourhtmltext))

Then on postback get the request.form("_firstname") . Take the runat out of
the hmtltext
 
W

Wouter vanEck

Hi,

Thanks for your quick response but I was actually trying to push a value to
the field. The reading part is easy, just like you said, but I need to be
able to populate the field after a postback.

Wouter
 
J

John Saunders

Wouter vanEck said:
Hi,

I need to find a way to read a html type string from a database and add it
to a placeholder control on the page and then I need to be able to
populate
the fields that were in that html string at run time.

The html string may look like this "<table><tr><td>First
Name</td><td><input
name=_firstname runat=server id=_firstname ....></td></tr></table>"

Is there a way to convert this string into an object which you could
access
like _firstname.text or _firstname.innerhtml, any thing to being able to
set
the value would be great.

Is there anyone who knows about an article or has an idea how to go about
doing this??

You can't do this directly. If you can turn the database contents into a
user control (.ascx file) on disk, then you can load it with LoadControl.

Other than that, you'll have to parse the HTML string by yourself, using
regular expressions or something.

This appears to have been designed on the assumption that you can turn a
string into a set of server-side controls. If this turns out not to be the
case, and if you can't do the user control trick, then perhaps you could
change design a bit. If the string in the database were a bit easier to
parse for "fields", you'd have better luck with it. For instance, it could
contain strings like %fieldName%. That's pretty easy to parse for. It's a
lot easier to parse for than runat="server" id="_firstname".

John Saunders
 
W

Wouter vanEck

Hi John,

It is not the answer I was hoping for, I am currently trying to work with
rendering the string to the page.. same result unless I am doing it wrong.

Wouter
 
V

vMike

Wouter vanEck said:
Hi,

Thanks for your quick response but I was actually trying to push a value to
the field. The reading part is easy, just like you said, but I need to be
able to populate the field after a postback.
If that is you only option then you could probably override the render of
the placeholder and do a some kind of string replace before output, but you
might want to consider some other approach.
 
J

John Saunders

Wouter vanEck said:
Hi John,

It is not the answer I was hoping for, I am currently trying to work with
rendering the string to the page.. same result unless I am doing it wrong.

Are you aware that the runat="server" in these controls is meaningless?

John Saunders
 
V

vMike

Wouter vanEck said:
Hi,

I need to find a way to read a html type string from a database and add it
to a placeholder control on the page and then I need to be able to populate
the fields that were in that html string at run time.

The html string may look like this "<table><tr><td>First
Name said:
name=_firstname runat=server id=_firstname ....></td></tr></table>"

Is there a way to convert this string into an object which you could access
like _firstname.text or _firstname.innerhtml, any thing to being able to set
the value would be great.

Is there anyone who knows about an article or has an idea how to go about
doing this??

Thanks,
Actual something like this might work for you. plc1 is a placeholder
control.


dim strtext as string = "<table><tr><td>First Name</td><td><input
runat='server' id='_firstname'></td></tr></table>"
dim ctrl as control = parsecontrol(strtext)
ctrl.id = "ctrl1"
dim ctrl2 as htmlinputcontrol = ctype(ctrl.controls(1),htmlcontrol)
ctrl2.value = "yourvaluefor input box"
plc1.controls.add(ctrl)
 
R

Roger Helliwell

Hi,

I need to find a way to read a html type string from a database and add it
to a placeholder control on the page and then I need to be able to populate
the fields that were in that html string at run time.

The html string may look like this "<table><tr><td>First Name</td><td><input
name=_firstname runat=server id=_firstname ....></td></tr></table>"

Is there a way to convert this string into an object which you could access
like _firstname.text or _firstname.innerhtml, any thing to being able to set
the value would be great.

Is there anyone who knows about an article or has an idea how to go about
doing this??

Thanks,

Wouter

Hi Wouter,

Not sure off hand how to convert your string into an object, but you
can easily convert your string into a control like so:

string sSomeHtmlString = get string from database();
Control ctrl = Page.ParseControl(sSomeHtmlString); // Create control
Form1.Controls.Add(ctrl); // Add control to the Form

During postbacks, using FindControl("_firstname") to get at individual
values. Likewise you could also set each value as needed.

Hopefully this is what you're looking for.
Roger
 

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,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top