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) 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
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:
I would also remove the Eval expression (unless it’s inside a databound control -gridview, etc-) in the Text property and leave it as:
Finally, you should be able to perform your validation on the server side and set the Text programmatically doing something like:
Where First_Name and Last_Name are read from the database somehow.