Adding HTML controls dynamically in asp.net page

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 1
Reputation: ItsMeYuvan is an unknown quantity at this point 
Solved Threads: 0
ItsMeYuvan ItsMeYuvan is offline Offline
Newbie Poster

Adding HTML controls dynamically in asp.net page

 
0
  #1
Feb 29th, 2008
All,

I am adding HTML controls with runat ="server" dynamically from my asp.net code.

<form id="frm1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" /></div>
<div runat="Server"> <asplaceHolder ID="p1" runat="server"><%=strControl%></asplaceHolder></div>
</form>

The code behind has
Public strControl as New string
strControl = "<input type=""hidden"" id=""test"" value=""test1"" runat=""server""/>"

Am able to view the input hidden control when the page is rendered. On click of submit button in page am writing a code like

Dim hdn1 as New HTMLInputHidden
hdn1 = Ctype(FindControl("test"), HTMLInputHidden)

But hdn1 always returns me nothing. is it possible to get the control in the code behind? I think there has to be something missing in my code. Can anyone help?

Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Adding HTML controls dynamically in asp.net page

 
0
  #2
Feb 29th, 2008
you cannot add the control like that. There you are just adding an attribute to the HTML input, but it is never rendered as a control. Therefore you cannot find the control on the page.

You would need something like this (untested):
  1. Dim hdn1 As New HtmlInputHidden
  2. hdn1.value = "test1"
  3. Controls.Add(hdn1)
  4.  
  5. '--onclick:
  6. Dim hdn1 As HtmlInputHidden = CType(FindCOntrol("hdn1"), HtmlInputHidden)
  7.  
  8. response.write(hdn1.value)
  9.  
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



Tag cloud for ASP.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC