[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

ASP.NET Literal loses Text Value on Postback

Posted on 2010-08-27
1
Medium Priority
?
1,441 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

649 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