textbox control

S

sean

Hi There,

I am trying to display some textboxes on my page, When I run the code the
textbox is written to the page (when I view source) but not to the screen.
Could someone tell me if I have the syntax correct?


Sean - thanks in advance for your answer

!-- code
response.write ("<asp:TextBox runat=server id=" & strMFormValue &"
/></td></tr>" & Vbcrlf)



<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<script language="vb" runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
If Not Page.IsPostBack then
BindData()
End If
End Sub


Sub BindData()
Dim dbRead As SqlDataReader
Dim strMFormControl as string
Dim strMFormValue as string
Dim strMDFormControl as string
Dim strMDFormValue as string

Dim myConnection as New
SqlConnection(ConfigurationSettings.AppSettings("connectionString"))

try
myConnection.Open()
Dim myCommand As New SqlCommand("GetFormById", myConnection)
myCommand.CommandType = CommandType.StoredProcedure

dbRead = myCommand.ExecuteReader()

While dbRead.Read ( )

strMFormValue = dbRead(1)
strMFormControl = dbRead(2)
strMDFormValue = dbRead(3)
strMDFormControl = dbRead(4)


response.write ("<form runat=server><table border=1 align=center>" &
Vbcrlf & _
"<tr><td colspan=2>" & dbRead(0) & "</td></tr>" & Vbcrlf & _
"<tr><td>" & strMFormValue & "</td><td>" & Vbcrlf)
DisplayFormItem(strMFormControl,strMFormValue)
response.write ("<tr><td>" & strMDFormValue & "</td><td>" & Vbcrlf)
DisplayFormItem(strMDFormControl,strMDFormValue)



response.write ("</table></form>")



End While
dbRead.close

Catch oexpData As OleDb.OleDbException
response.write ("an exception has occured,")
response.end
Finally
myConnection.close
End Try

End Sub

!--- function here


Function DisplayFormItem(ByVal strMFormControl, ByRef strMFormValue)

Select Case Trim(strMFormControl)

Case "textbox"
response.write ("<asp:TextBox runat=server id=" & strMFormValue &"
/></td></tr>" & Vbcrlf)

Case "checkbox"

End Select

End Function





</script>
 
T

Teemu Keiski

Hi,

you can't write ASP.NET control declarations with Response.Write. Having
<asp:textBox> in page source is subject to compilation when the page is
first time requested. Response.Write writes the given string to the HTML
response, which is also generated/written by the compiled controls which
execute during Page's lifecycle. Using Response.Write does not cause
compilation to happen for controls(or this control on the Page). Compilation
is crucial thing in page processing and you, in fact, can't avoid it to get
things working.

You have three ways to declare controls:

a) Have them statically in aspx source
b) Create them dynamically in code and add to the Page's Controls
collection.
c) To create from markup at runtime, use Page.ParseControl(<control
declaration>) which returns a compiled control instance which you can place
to Page's Controls collection

But you can't output control declarations using Response.Write and assume
that they get compiled as fully-working controls.
--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke

Hi There,

I am trying to display some textboxes on my page, When I run the code the
textbox is written to the page (when I view source) but not to the screen.
Could someone tell me if I have the syntax correct?


Sean - thanks in advance for your answer

!-- code
response.write ("<asp:TextBox runat=server id=" & strMFormValue &"
/></td></tr>" & Vbcrlf)



<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<script language="vb" runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
If Not Page.IsPostBack then
BindData()
End If
End Sub


Sub BindData()
Dim dbRead As SqlDataReader
Dim strMFormControl as string
Dim strMFormValue as string
Dim strMDFormControl as string
Dim strMDFormValue as string

Dim myConnection as New
SqlConnection(ConfigurationSettings.AppSettings("connectionString"))

try
myConnection.Open()
Dim myCommand As New SqlCommand("GetFormById", myConnection)
myCommand.CommandType = CommandType.StoredProcedure

dbRead = myCommand.ExecuteReader()

While dbRead.Read ( )

strMFormValue = dbRead(1)
strMFormControl = dbRead(2)
strMDFormValue = dbRead(3)
strMDFormControl = dbRead(4)


response.write ("<form runat=server><table border=1 align=center>" &
Vbcrlf & _
"<tr><td colspan=2>" & dbRead(0) & "</td></tr>" & Vbcrlf & _
"<tr><td>" & strMFormValue & "</td><td>" & Vbcrlf)
DisplayFormItem(strMFormControl,strMFormValue)
response.write ("<tr><td>" & strMDFormValue & "</td><td>" & Vbcrlf)
DisplayFormItem(strMDFormControl,strMDFormValue)



response.write ("</table></form>")



End While
dbRead.close

Catch oexpData As OleDb.OleDbException
response.write ("an exception has occured,")
response.end
Finally
myConnection.close
End Try

End Sub

!--- function here


Function DisplayFormItem(ByVal strMFormControl, ByRef strMFormValue)

Select Case Trim(strMFormControl)

Case "textbox"
response.write ("<asp:TextBox runat=server id=" & strMFormValue &"
/></td></tr>" & Vbcrlf)

Case "checkbox"

End Select

End Function





</script>
 
S

sean

HI Teemu,

Thanks for your answer, it seems I am having a bit of troublemaking the
transition from asp to asp .net. Could you possibly help me out with a some
examples of two below? or give me a link to a website which illustrates the
examples that you have mentioned?

Sean

b) Create them dynamically in code and add to the Page's Controls
collection.
c) To create from markup at runtime, use Page.ParseControl(<control
declaration>) which returns a compiled control instance which you can place
to Page's Controls collection
 
T

Teemu Keiski

I think you want to see the declarative way first.

http://www.dotnetjunkies.com/quickstart/aspplus/doc/webformsintro.aspx

If you create a TextBox at runtime, it happens something like this:

Dim tb As New TextBox
tb.ID="myTextBox"

'Make sure we add TB to server-side form
Page.FindControl("Form1").Controls.Add(tb)

Dynamical controls need to be added to the Controls collection on every
request (when controls are declared statically in aspx, they are
automatically part of the Controls collection)

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke



HI Teemu,

Thanks for your answer, it seems I am having a bit of troublemaking the
transition from asp to asp .net. Could you possibly help me out with a some
examples of two below? or give me a link to a website which illustrates the
examples that you have mentioned?

Sean

b) Create them dynamically in code and add to the Page's Controls
collection.
c) To create from markup at runtime, use Page.ParseControl(<control
declaration>) which returns a compiled control instance which you can place
to Page's Controls collection
 
M

Matt Berther

Hello Sean,

When writing output straight to the browser, you need to use valid HTML. The code that you are writing (asp:textbox) is used by the ASP.NET preprocessor to actually convert it to something like input type="text".

You have to choices here:

1. Modify your Response.Write lines to output the correct HTML.
2. Use the ASP.NET object model to create the objects and add them to the Pages control collection, getting rid of your response.Write calls.
 

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

Top