I’m working with an asp.net website using C# in the background. I would like to be able to update an asp:label on a page, lets say Page1.aspx. I want this be updated depending on the outcome of a function in a class (.cs) in other folder. This could be behind.cs.
behind.cs
*some other code is here*
bool correct = false;
try
{
if (_myConn.State == ConnectionState.Closed)
{
_myConn.Open();
myCommand.ExecuteNonQuery();
}
if (Convert.ToInt32(myCommand.Parameters["@SQLVAR"].Value) < 1)
{
"Invalid Login" // I want to be the text of lblMessage.Text
}
else
{
correct = true;
}
_myConn.Close();
}
catch (Exception ex)
{
"Error connecting to the database" // I want to be the text of lblMessage.Text
}
return correct;
page1.aspx
<asp:label runat="server" ID="lblMessage" cssClass="error"></asp:label>
How can I update the asp:label on page1.aspx* from **behind.cs??
You can’t directly access the label from another class.
You can write a
TryLoginfunction that has an out parameter with the error message.In Page1.cs
In behind.cs