By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,992 Members | 934 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,992 IT Pros & Developers. It's quick & easy.

cookie textbox value problems

P: n/a
<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")("Us ername"))%>'>

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") = "jm*****@jmac-solutions.com"
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")("Use rname"))
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")("Us ername"))%>'>
</form>

--
Message posted via http://www.dotnetmonster.com
Nov 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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("Userna me")("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)

--
Teemu Keiski
ASP.NET MVP, Finland
"Justin Morris via DotNetMonster.com" <fo***@DotNetMonster.com> wrote in
message news:bd******************************@DotNetMonste r.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")("Us ername"))%>'>

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") = "jm*****@jmac-solutions.com"
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")("Use rname"))
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")("Us ername"))%>'>
</form>

--
Message posted via http://www.dotnetmonster.com

Nov 19 '05 #2

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


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

--
Message posted via http://www.dotnetmonster.com
Nov 19 '05 #3

P: n/a
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

-Brock
DevelopMentor
http://staff.develop.com/ballen
<asp:TextBox ID="TextBox1" runat="server"
Text='<%#Server.HtmlEncode(Request.Cookies("Userna me")("Username"))%
'></asp:TextBox>

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


Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.