Risho
Risho

Reputation: 2647

Data lost in the postback

I need a way for a piece of data to presist across postbacks. Is there a way besides, cookies, and sessions?

I've got a listbox, when clicked, the selected index changed event takes a session which contains user id and feeds it to a method that fetches user's name, etc. Then I use FindControl method to locate a button inside the form view. I set the button's text property with data just fetched such as first and last name.

It works on the initial load, but if I click on other records in the listbox second time the text property is not set.

I did not write this code - I'm trying to make chages.

Here is the list box:

<asp:ListBox ID="lbxUpcommingEvents" runat="server" DataSourceID="odsUpcommingEvents" 
Rows="6" DataTextField="Title" DataValueField="id" AutoPostBack="true"
Width="206px" OnSelectedIndexChanged="lbxUpcommingEvents_OnSelectedIndexChanged"  />

Listbox'x on selected index changed:

protected void lbxUpcommingEvents_OnSelectedIndexChanged(object sender, EventArgs e)
{
    pnlEventsSignUp.Visible = true;
    string _selectedItemValue = lbxUpcommingEvents.SelectedValue.ToString();

    LoadName();

}

protected void LoadName()
{
    MembershipUser user = Membership.GetUser();
    DataSetTableAdapters.MemberInfoTableAdapter da = new DataSetTableAdapters.MemberInfoTableAdapter();

    Guid _memberId = Guid.Empty;
    _memberId = new Guid(Session["myId"].ToString());
    DataSet.MemberInfoDataTable dt = da.GetMember(_memberId);
    if (dt.Rows.Count == 1)
    {
        DataSet.MemberInfoRow mr = dt[0];

        RolloverLink rlnkSignUp = (RolloverLink)(fvEventSignUp.FindControl("rlnkSignUp"));
        rlnkSignUp.Text = "I, " + mr.firstname + " " + mr.lastname + " will be attending this event.";
    }
}

Here is markup for the form view where I set the RolloverLinkButton's text property.

<asp:Panel runat="server" ID="pnlEventsSignUp" visible="false">
<div class="dashedline" ></div>
<asp:FormView ID="fvEventSignUp" runat="server" DataSourceID="SqlDataSource1" 
    DataKeyNames="id" Width="100%" >
    <ItemTemplate>
        <h2>
            <asp:Label Text='<%# Eval("title") %>' runat="server" ID="titleLabel" />
        </h2>
        <div class="itemdetails">
            <br />
            location:
            <h3>
                <asp:Label ID="locationLabel" runat="server" Text='<%# ShowLocationLink(Eval("locationname"),Eval("location")) %>' />
            </h3>
            <p>
                <asp:Label Text='<%# Eval("starttime","{0:D}") %>' runat="server" ID="itemdateLabel" CssClass="GeneralText" />

                <asp:Label Text='<%# ShowDuration(Eval("starttime"),Eval("endtime")) %>' runat="server" ID="Label1"  CssClass="GeneralText" />
            </p>
        </div>
        <div class="downloadevent">
            <a href="#">
                <img src="images/icon_download_event.gif" alt="Download this event to your personal calendar"
                    width="15" height="26" /></a><a href='<%# "events_download.ashx?EventID=" + Convert.ToString(Eval("id")) %>'>Add
                        this event to your personal calendar</a>
        </div>
        <Club:ImageThumbnail ID="thumb1" runat="server" ImageSize="Large" PhotoID='<%# Eval("photo") %>' />
        <p>
            <asp:Label Text='<%# Eval("description") %>' runat="server" ID="descriptionLabel" />
        </p>
        <div class="dashedline" ></div>
        <asp:Panel ID="panel1" runat="server" CssClass="actionbuttons" Visible='<%#User.IsInRole("Administrators") %>'>
            <table border="0" cellpadding="2" cellspacing="2">
                <tr>
                    <td>
                        <asp:Label runat="server" ID="Label2" Text="Action<br />Required" Width="80px" />
                    </td>
                    <td>
                        <img src="images/RedArrow.jpg" alt="Red Arrow Right" />
                    </td>
                    <td>
                        <asp:CheckBox runat="server" ID="cbxCert" Height="30px" Text="Check to Confirm Attendance" /><br />
                        <asp:CustomValidator runat="server" ID="rfvConfirm" 
                                    ErrorMessage="You must check the box to continue" Font-Bold="true"
                                    ForeColor="#cc0000" ClientValidationFunction="ensureChecked" /> <br />
                        <Club:RolloverLink ID="rlnkSignUp" runat="server"
                        />
                    </td>
                </tr>
            </table>                    
        </asp:Panel>
    </ItemTemplate>
</asp:FormView>
</asp:Panel>

Upvotes: 3

Views: 2192

Answers (2)

Chuck Savage
Chuck Savage

Reputation: 11955

You can use ViewState. Since you are dynamically looking up the button, setting a property is quicker and we can then let Asp.Net find the button and set its text via one of the two methods I explain below:

public string rlnkSignUpText
{
    get { return Convert.ToString(ViewState["rlnkSignUpText"] ?? "default value"); }
    set { ViewState["rlnkSignUpText"] = value; }
}

Replace your code that searches for the button and assigns the text with:

rlnkSignUpText = "I, " + mr.firstname + " " + mr.lastname + " will be attending this event.";

Then use one of these methods to set the text to the control:

1) Use the control's OnPreRender event:

To render it, add OnPreRender="rlnkSignUp_PreRender" to your xaml like this:

<Club:RolloverLink ID="rlnkSignUp" runat="server" OnPreRender="rlnkSignUp_PreRender" />

Then in your codebehind:

protected void rlnkSignUp_PreRender(object sender, EventArgs e)
{
    RolloverLink rlnkSignUp = (RolloverLink)sender;
    rlnkSignUp.Text = rlnkSignUpText;
}

2) Bind the property and control together:

Instead of using the control's PreRender you can bind to the property with:

<Club:RolloverLink ID="rlnkSignUp" runat="server" Text='<%# rlnkSignUpText %>' />

You then need to call DataBind in your Page_PreRender() like so:

void Page_PreRender(object sender, EventArgs e)
{
    DataBind();
}

Upvotes: 4

Jayanga
Jayanga

Reputation: 887

Have you enabled ViewState in the control.

Enable View State

Upvotes: 1

Related Questions