VB.net label using a session variable
My vb.net application needs to use a session variable. I don't really know how to use it in ASP.net. I've been trying to use what my books have, but I can't get this label to work. I need the user input to be validated against the database and if their code is in the database, the textbox should disappear and a label will appear saying Welcome.
The way I wrote it, I get an error saying the server tag is not well formed and in the codebehind it says that the ID of my label is not declared. Can anyone spot any problems with the code I wrote?
<asp:Label ID="lblIB" runat="server" DataSourceID="dsIBs"
Text="Welcome, <%# Eval("First_Name") %> '&' <%# Eval("Last_Name")%>">
</asp:Label>
Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal
args As System.Web.UI.WebControls.ServerValidateEventArgs) Ha开发者_如何学Gondles
CustomValidator1.ServerValidate
args.IsValid = True
For Each drv As DataRowView In dsIBs.[Select](DataSourceSelectArguments.Empty)
If drv("baccount").ToString() = args.Value Then
args.IsValid = False
lblIB.Visible = False
Exit For
End If
Next
If args.IsValid Then
IBTextBox.Visible = False
IBTextBoxButton.Visible = False
lblIB.Visible = True
End If
End Sub
UPDATE:
<asp:Label ID="lblIB" runat="server" Text=""></asp:Label>
Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
args.IsValid = True
For Each drv As DataRowView In dsIBs.[Select](DataSourceSelectArguments.Empty)
If drv("baccount").ToString() = args.Value Then
args.IsValid = False
lblIB.Visible = False
Exit For
End If
Next
If args.IsValid Then
IBTextBox.Visible = False
IBTextBoxButton.Visible = False
lblIB.Visible = True
lblIB.Text = String.Format("Welcome, {0} {1}", Session("FirstName"), Session("LastName"))
End If
End Sub
UPDATE 2:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
lblIB.Visible = False
End Sub
If args.IsValid Then
IBTextBox.Visible = False
IBTextBoxButton.Visible = False
lblIB.Visible = True
lblIB.Text = String.Format("Welcome, {0} {1}", Session("FirstName"),
Session("LastName"))
Protected Sub IBTextBoxButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles IBTextBoxButton.Click
Session("IB") = IBTextBox.Text
Dim IB As String = Session("IB")
Response.Redirect(Request.RawUrl + "&IB=" + Session("IB"))
End Sub
Your markup is wrong because the Label control doesn't have a DataSourceID property, so you should remove it:
<asp:Label ID="lblIB" runat="server"
Text="Welcome, <%# Eval("First_Name") %> '&' <%# Eval("Last_Name")%>">
</asp:Label>
I would also remove the Eval expression (unless it's inside a databound control -gridview, etc-) in the Text property and leave it as:
<asp:Label ID="lblIB" runat="server"
Text="">
</asp:Label>
Finally, you should be able to perform your validation on the server side and set the Text programmatically doing something like:
lblIB.Text = "Welcome " + First_Name + " " + Last_Name
Where First_Name and Last_Name are read from the database somehow.
When you get server tag not well formed, this usually points to an issue with your quoting.
The parser gets confused over the quotes you use.
Fortunately, there is a simple solution.
Change :-
Text="Welcome, <%# Eval("First_Name") %> '&' <%# Eval("Last_Name")%>">
To:-
Text='Welcome, <%# Eval("First_Name") %> ‘&’ <%# Eval("Last_Name")%>'>
First, we've put single quotes around the text. This'll help to parser to recognize where your quoted text actually ends.
Second, I assume that you actually want to display the '&', and not using it for concatenation. That's this bit :-
‘&’
Remove if you don't need it.
Finally, if the label isn't actually being databound, then nothing will happen. You either need to databind the label or use slightly different quoting syntax.
Quoting syntax for emitting a string in earlier versions of VS is :-
<%= %>
Quoting syntax for emitting a string in .NET 4.0+ is
<%: %>
Since the Label is not contained within a databound control, you can't use databinding syntax (i.e. <%#
). Either set the text of the Label in the code-behind, or do it inline like this:
<asp:Label ID="Label1" runat="server" Text='<%=String.Format("{0} {1}", Session("FirstName"), Session("LastName"))%>' ... />
Or in the code-behind:
Label1.Text = String.Format("{0} {1}", Session("FirstName"), Session("LastName"))
精彩评论