Solved

ASP.NET Literal loses Text Value on Postback

Posted on 2010-08-27
1
1,373 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

17 Experts available now in Live!

Get 1:1 Help Now