Yashman Gupta
Yashman Gupta

Reputation: 296

Label is not printing any value in ASP.NET using VB

Need your expertise on the issue.

Background: I have a label control in the presentation layer which is purely used for status display. Status could be Update Fail Pass/Delete/Duplicate record etc. that is happening through GridView (refer Code 1).

The status in this label is shown using code behind procedure (refer Code 2)

Issue: The procedure is not displaying anything when called from UpdateTableRecordInDatabase(...) but it works when I hit the button (created test button to evaluate whether is there any issue in the code). I did the debug and the values are being passed successfully without any issues.

On Click of Test Button

Code 1

        <asp:Label ID="lblStatus" runat="server" visible="false"></asp:Label>

        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <asp:UpdatePanel ID="TablesUpdatePanel" runat="server">
            <ContentTemplate>
                <asp:GridView ID="ListOfTables" runat="server" AllowPaging="True"
                 AllowSorting="True" AutoGenerateColumns="False"
                 DataKeyNames="PK_RestaurantTableID" 
                 DataSourceID="DataSource1" Width="100%" CssClass="dataTable" 
                 PageSize="15" ShowFooter="True" CellPadding="5" 
                 CellSpacing="5" ShowHeaderWhenEmpty="True"
                 EmptyDataText="No records available in database" 
                 OnRowUpdating = "UpdateTableRecordInDatabase" 
                 OnRowDeleting="ListOfTables_RowDeleting">

                 <SortedAscendingHeaderStyle CssClass="sortasc" />
                 <SortedDescendingHeaderStyle CssClass="sortdesc" />

                 <columns>.....</columns>
                </asp:GridView>
                <asp:SQLDataSource .....>....</asp:SqlDataSource>
             </ContentTemplate>

            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="ListOfTables" />
            </Triggers>
        </asp:UpdatePanel>
</div>

Code 2

Protected Sub UpdateTableRecordInDatabase(sender As Object, e As GridViewUpdateEventArgs)
   If CheckForDuplicateRecords(intAccountID, strTableName) = FALSE then
      code here to update the table
   else
      e.Cancel = TRUE
      Response.Redirect(Request.Url.AbsoluteUri)
      PrintExceptionStatus("Table Names should be unique")
   end if

End Sub

Sub PrintExceptionStatus(strMessage As String)

    lblStatus.Visible = True
    lblStatus.BackColor = Drawing.Color.LightYellow
    lblStatus.ForeColor = Drawing.Color.Red
    lblStatus.Text = "<img src='Images/error.png' align='absmiddle' hspace='5'>" + strMessage.ToString()

End Sub

Is there anything which I am overlooking or missing. Please guide.

Upvotes: 0

Views: 278

Answers (2)

Andrei
Andrei

Reputation: 56688

Note that the GridView that fires the event is inside the UpdatePanel. Here is how it works - when the postback is triggered by something inside the UpdatePanel, the whole page life cycle is run, but then only the part of the page inside the UpdatePanel is updated on the client page.

So, after the GridView triggers the event, the text of the label is changed in the code behind (as you can see in debug), but since it is not inside the UpdatePanel the label is not changed, as you can see.

To solve the issue simply move the label inside the UpdatePanel - this make it a part of the HTML that is updated when postback inside UpdatePanel occurs:

<asp:UpdatePanel ID="TablesUpdatePanel" runat="server">
    <ContentTemplate>
        <asp:Label ID="lblStatus" runat="server" visible="false"></asp:Label>

Note that since it is inside the template, you might need FindControl now to access the label.

Upvotes: 1

Swapy
Swapy

Reputation: 55

If you execute the UpdateTableRecordInDatabase the debugger goes in the if block or else block? If it goes in the else block you have written:

  Response.Redirect(Request.Url.AbsoluteUri)

and after that you have called

  PrintExceptionStatus("Table Names should be unique")

so in this case Page will be redirected to the given URL and if you have given URL as that of the current page it will definitely make the label to visible false as again the page load event will occur so try commenting Response.Redirect(Request.Url.AbsoluteUri)

Upvotes: 1

Related Questions