Adding Web Control

  • Thread starter Thread starter Duncan Dimech
  • Start date Start date
D

Duncan Dimech

Dear All
I am writing a tool which requires to have controls added to it dynamically.
To make the task more complex, the addition of the control cannot happen
anywhere but it has to be instead of a token

example

<p> this is a test <textbox /> and a text box should be entered </P>

the tool should render the message and instead of <textbox /> it should
show an ASP.Net textbox control

Please note that position cannot be predetermined


Thanks for the support, it will be appreciated
Duncan
 
Are you allowed to parse and then display? If so, you can set up the page
with a pass and then redirect the user to the new page If this is not a
possibility, you will have to set up a handler to create the parse and
display bits. The easiest might be inheriting from the page class and
creating the scripting engine to display from a raw parse. Either way, it is
going to be a pain.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*************************************************
Think outside of the box!
*************************************************
 
I am first parsing and then diplaying the info

Can you please elaborate on your solution

Duncan
 
On postback, pull the string from the textbox. Use regular expressions
or string.replace to replace the keyword tokens with valid asp.net
syntax. Then, call the ParseControl() function to render the output.
This will return a new control with all the child elements instantiated
and you can add it to the current page. If you plan on manipulating or
using the new controls on subsequent postbacks you'll need to add the
parsed control in the init event for them to work properly.
 
Thanks to Micheal and Gregory

I have managed to add controls to a page and handle the event of the button
which solves the first part of the problem - second is then quite easy as
one will just need to find the token and split the text in two variables or
array

Code for initial part is shared below in case you are having the same
problem like me

Protected Sub Page_Init(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Init
Dim c As Control 'declare control
c = ParseControl("HelloWorld <br /> ") 'parsecontrol takes text as
well as html
form1.Controls.Add(c) 'add control to form

c = ParseControl("<asp:Button ID=""Button1"" runat=""server""
Text=""Button"" />") 'html for button
form1.Controls.Add(c)

Dim btn As Button = FindControl("Button1")
AddHandler btn.Click, AddressOf MyOtherClick 'add event handler for
button

c = ParseControl("<asp:TextBox ID=""Searchbox""
runat=""server""></asp:TextBox>")
form1.Controls.Add(c) 'adding a textbox control

c = ParseControl("<br /> End Here")
form1.Controls.Add(c) 'adding html

End Sub

Private Sub MyOtherClick(ByVal sender As Object, ByVal e As EventArgs)
Dim tb As TextBox = Me.Page.FindControl("Searchbox")
MsgBox(tb.Text) 'display contents of text box
End Sub

Duncan
 
Back
Top