Replace text as server controls

A

Andla Rand

Hi,

I would appreciate some directions on how to replace text as server
controls. I'm currently reading text from a database and would like to
build server logic based on specific information retrieved from the
server.

Here is some sample code that I'm currently playing with:

string repl=spn.InnerHtml;
repl=repl.Replace("[button]","<INPUT type=\"button\" id=\"btn\"
value=\"Button\">");
spn.InnerHtml=repl;

As you se the [button] is replaced with a button but if I make this a
server button then the server does not process the information as a
server control and thus the client get the server code.

I would appreciate any help on this.

Regards
Andla
 
E

Eric Marvets

Sure you can.

Step 1: Place an ASP.NET placeholder where the text will be rendered.
Step2: Pase out the text before your button and do the following

PlaceHolderX.Controls.Add(new LiteralControl(sTextBefore));
PlaceHolderX.Controls.Add(new Button());
PlaceHolderX.Controls.Add(new LiteralControl(sTextAfter));

Now what happens on subsequent page requests will be your problem. You will
need to create the controls on Post back in the same order as when they were
shipped to the client, so the viewstate and events will tie back together.
Before you ship the page to the client again, you are welcome to change the
order or type of controls, but when the page is posted back again, they need
to recreate them in the exact same order.

--
Eric Marvets
Principal Consultant

the bang project

<shameless self promotion>

Email (e-mail address removed) for Information on Our Architecture and
Mentoring Services

</shameless self promotion>
 
R

Rajaram Mhadgut

Hi Andla,

This is Rajaram.

As per my understanding u want to create server controls dynamically.
But in first method u have created client side control by using
statement :

repl=repl.Replace("[button]","<INPUT type=\"button\" id=\"btn\"
value=\"Button\">");

This will always works fine, since it process string and give code to
client and this string contains plain HTML code which can be dispalyed
by client browser, but now u want to display server control, If u used
same way to display server control , It will generate output string
which looks like server control code but since this code is already
transfered to client browser and client browser can not recognise this
code and display it as plain HTML code,
To avoide this proble u need to create ur own server control which
accpts string and create all dynamic server contorls.
To generate server controls pls don't use replace method ,
break input string and use if then conditions to add server control
For eg:


It is not actual code but u can understand what u have to do:

If Instr(yourstr,"[Button]") <> -1 then
button_pos = Instr(yourstr,"[Button]")
content_before_button = left(yourstr,button_pos)
content_after_button = left(yourstr,button_pos+ 8) ' 8 is used as length
of string "[Button]"
Response.write content_before_button
' and here is code to display server side button
<asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 92px; POSITION:
absolute; TOP: 54px" runat="server" Width="207px" Height="39px"
Text="Button"></asp:Button>
Response.write content_after_button




I think this will work fine .
But still u have any queries contact me.
Email me at (e-mail address removed) .

Regards,
Rajaram Mhadgut :)
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top