Solved

ASP.NET Literal loses Text Value on Postback

Posted on 2010-08-27
1
1,376 Views
Last Modified: 2013-11-07
I have a web page that doesn't seem to preserve a literal control's Text property on postback. Here is the markup:
<%@ Page Language="VB" AutoEventWireup="false" MasterPageFile="~/MyMaster.master" CodeFile="MyPage.aspx.vb" Inherits="MyPage" %>
<%@ MasterType VirtualPath="~/MyMaster.master" %>

<asp:Content ID="MasterContent" ContentPlaceHolderID="MasterContent" Runat="Server">
  <div style="padding: 3px;" >
      <button id="btnLinkMerchant" style="width:75px; border:solid 1px #66CCFF;" 
          onserverclick="btnLinkMerchant_Click" runat="server"
        >
          <img id="imgLinkMerchant" runat="server" src="~/images/merchantAdd.jpg" alt="" /><br />
          <asp:Literal ID="litLinkMerchant" runat="server" Text="Opt In" />
      </button>
  </div>
</asp:Content>

Open in new window


Here's the code behind:
Partial Class MyPage
    Inherits System.Web.UI.Page

    Public Property MerchantID() As Integer
        Get
            Return CNull(ViewState("MerchantID"), -1)
        End Get
        Private Set(ByVal value As Integer)
            ViewState("MerchantID") = value
        End Set
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Call DisplayInfo(1)  ' dummy value
        End If
    End Sub

    Private Sub DisplayInfo(ByVal nInfoID As Integer)
        Dim ws As New MyWebService

        Dim sXML As String = ws.GetInfo(nInfoID)
        Dim reader As New System.IO.StringReader(sXML)
        Dim ds As New DataSet
        ds.ReadXml(reader)

        ' Need to figure out if linked to merchant
        If ds.Tables(0).Rows.Count > 0 Then
            ' Determine Merchant
            MerchantID = ds.Tables(0).Rows(0).Item("MerchantID")

            ' Determine Opt In 
            Call DisplayOptInOut(ds.Tables(0).Rows(0).Item("OptInInd"))
        End If
    End Sub

    Private Sub DisplayOptInOut(ByVal bIsOptIn As Boolean)
        If bIsOptIn Then
            ' Show Opt Out
            Me.imgLinkMerchant.Src = "~/images/merchantRemove.jpg"
            Me.litLinkMerchant.Text = "Opt Out"
        Else
            ' Show Opt In
            Me.imgLinkMerchant.Src = "~/images/merchantAdd.jpg"
            Me.litLinkMerchant.Text = "Opt In"
        End If
    End Sub

    Public Sub btnLinkMerchant_Click(ByVal sender As Object, ByVal e As EventArgs)
        Call LinkUnlinkMerchant()
    End Sub

    Private Sub LinkUnlinkMerchant()
            ' Add / Remove Favorite
            Dim bLink As Boolean = (Me.litLinkMerchant.Text = "Opt In")     ' desired action

            Dim ws As New MyWebService
            If bLink Then
                ws.LinkToMerchant(MerchantID)
            Else
                ws.UnlinkFromMerchant(MerchantID)
            End If

            Call DisplayInfo(1)
    End Sub

End Class

Open in new window


When I run the page, the button initially shows Opt-In. If I click it, the page posts back and correctly displays Opt-Out. (The database values are being correctly updated by the web service methods.)

But if I now click on the Opt-Out button, it stays Opt-Out. If I step through the code, I found that the text for the literal says still says Opt-In during page_load even though the button event changed it's text to Opt-In. Since the literal's viewstate is enabled by default, shouldn't its text value be Opt-In on postback?

In stepping through this, I did notice that it seems like some routines are called twice. Since I'm toggling the literal's value I wonder if I'm flipping it twice and thus causing this error. But I don't see where that's happening yet.

Thanks in advance.
0
Comment
Question by:ZekeLA
1 Comment
 
LVL 1

Accepted Solution

by:
ZekeLA earned 0 total points
ID: 33543592
I used a viewstate property to track OptIn status instead of the literal conrol text and that did the trick.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now