Asked by:
Update ASP:Label within ASCX without refreshing the page?

Question
-
User1738536122 posted
I have an ASCX page that includes, among other things, an ASP:Label for messages, an ASP:TextBox for an input value, and an ASP:Button to submit the data to the code behind file for processing.
What I want to do is, when the button is clicked, validate the input; if it is valid, clear any existing message in the label and do the necessary processing, but if it is not valid, put "Invalid Input" into the label and return without processing. That I can do. The problem is, when I enter an invalid input and get the message, then enter a valid input and resubmit the page, the "Invalid Input" message remains on the screen until the processing is finished, which is Not A Good Thing as (a) the input is valid and (b) it takes a minute to do the processing, so the user has no way of knowing that the new input is valid until the page refreshes again.
The "obvious" answer is to handle the button click in JavaScript within the ASCX, but document.getElementById doesn't appear to work properly with ASP:Label.
Is there a way to update an ASP:Label's text - either from within the ASCX or from the code behind - without having to refresh the page?
Tuesday, March 3, 2020 2:55 PM
All replies
-
User1535942433 posted
Hi That Don Guy,
Accroding to your description,I don't understand your requirment clearly.
Could you tell us what is the process?Besides,I suggest you could post your current codes to us.It will help us solve problem.
Since you don't tell us more about your codes,as far as I think,you could set AutoPostBack true for label and UpdateMode property to Conditional.
More details,you could refer to below codes:
<asp:Label Style="color: White;" AutoPostBack="true" UpdateMode="Conditional" ID="lblTotal" runat="server"></asp:Label>
Best regards,
Yijing Sun
Wednesday, March 4, 2020 1:50 AM -
User1738536122 posted
Here's what's in the ASCX:
<div style="text-align: center">
<b>Name:</b>
<asp:TextBox ID="UserEntryBox" runat="server" />
<asp:Button ID="GoButton" Text="Go" runat="server"/>
<br/>
<asp:Label ID="MsgLabel" ForeColor="Red" runat="server" />
</div>
And here is the VB back end:
Overloads Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
MsgLabel.Text = String.Empty
End Sub
Private Sub GoButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles GoButton.Click
If UserEntryBox.Text.Length >= 1 Then
'Lengthy processing done here
Else
MsgLabel.Text = "Invalid Entry"
End If
End Sub
Wednesday, March 4, 2020 3:08 PM -
User1535942433 posted
Hi That Don Guy,
Accroding to your description,I suggest you could get the textbox val and check the length whether is big than 1 using javascript.
More details,you could refer to below codes:
<script src="Scripts/jquery-3.0.0.min.js"></script> <script> $(function () { $('#GoButton').on('click', function () { event.preventDefault(); if ($('#UserEntryBox').val().length>=1) { $('#demo').text(""); } else $('#demo').text("Invalid Entry"); }) }) </script> <div style="text-align: center"> <b>Name:</b> <asp:TextBox ID="UserEntryBox" runat="server"/> <asp:Button ID="GoButton" Text="Go" runat="server"/> <br /> <asp:Label ID="MsgLabel" ForeColor="Red" runat="server" /> <p id="demo"></p> </div>
Result:
Best regards,
Yijing Sun
Monday, March 9, 2020 9:42 AM