cookie textbox value problems

  • Thread starter Justin Morris via DotNetMonster.com
  • Start date
J

Justin Morris via DotNetMonster.com

<asp:TextBox ID="TextBox1" runat="server" value='<%=Server.HtmlEncode
(Request.Cookies("Username")("Username"))%>'/>
<input name="Password" type="text" id="Password" value='<%
=Server.HtmlEncode(Request.Cookies("Username")("Username"))%>'>

i have created a cookie and want to use it with my login page. I currently
have asp:TextBox with form validation control. I can get the cookie value
to appear but not in the asp:TextBox. I can use a label to show it on the
page or in a standard HTML text field. I don't understand what is going
on. Can we not bind values to the asp:TextBox?

code for setting cookie works
_____________________________
<script runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
If NOT IsPostBack Then
Dim MyCookie As HttpCookie = New HttpCookie("Username")
Dim dt As DateTime = DateTime.Now()
Dim ts As New TimeSpan(1,0,10,0)

MyCookie.Expires = dt.Add(ts)
MyCookie.Domain = "jmac-solutions.com"
MyCookie.Path = "/photoshare"
MyCookie.Values("Username") = "(e-mail address removed)"
Response.Cookies.Add(MyCookie)

End If
End Sub
</script>

code for retrieving cookie info
_________________________________
<script runat="server">
Sub Page_Load()
Dim bc As HttpBrowserCapabilities
Dim CookiesAvailable
bc = Request.Browser
If bc.Cookies Then
Message.Text = "Cookies are available with this browser.<br>"
Message.Text &= "Save login info?: <input type=checkbox name=checkbox
value=checkbox>"
Else
Message.Text = "Cookies are NOT available with this browser"

End If

Dim str As String
str = Request.Cookies("Username").Value
Response.Write(str & " <br>")

If Not Request.Cookies("Username") Is Nothing Then
Label1.Text = Server.HtmlEncode(Request.Cookies("Username")("Username"))
End If


End Sub
</script>

<form runat="server">
<asp:Label id="Message" runat="server" /><br>
<asp:Label id="Label1" runat="server" /> <br>
<asp:TextBox ID="TextBox1" runat="server" value='<%=Server.HtmlEncode
(Request.Cookies("Username")("Username"))%>'/>
<input name="Password" type="text" id="Password" value='<%
=Server.HtmlEncode(Request.Cookies("Username")("Username"))%>'>
</form>
 
T

Teemu Keiski

The syntax you use, is not databinding syntax but tells Page to write the
string out at rendering stage. And as you use single quotes, it doesn't
indeed work with TextBox (it also takes the text into its Text property) but
comes out as a literal string as TextBox's text. If you change single quotes
to double quotes, you get error stating that <%...%> construct cannot be
used inside server-side tags.

You have two solutions:

- Set the Text property in code

OR

Change databinding syntax in textBox as follows (to the ASP.NET's correct
databinding syntax)

<asp:TextBox ID="TextBox1" runat="server"
Text='<%#Server.HtmlEncode(Request.Cookies("Username")("Username"))%>'></asp:TextBox>

(Note the <%#...%> construct)

And then after setting the cookie, call TextBox1.DataBind() to initiate
databinding (or if you have call to Page.DataBind() which does for entire
page, that's fine too unless you want to bind only a specific control)
 
J

Justin Morris via DotNetMonster.com

<asp:TextBox ID="TextBox1" runat="server"
Text='<%#Server.HtmlEncode(Request.Cookies("Username")("Username"))%
'></asp:TextBox>

i had tried with this syntax # also, it still didn't work just a blank box.
Please help-
 
B

Brock Allen

Don't use <asp:TextBox> unless you need to:

<input type=input value=<%= Session("fooey")%>

If you do need it on the server as a full blown TextBox, then do the assignment
in code, not inline in the ASPX.

<asp:TextBox runat=server id=_name />

Sub Page_Load()
_name.Text = Sesison("fooey")
End Sub
 

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,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top