Geo Ego
Geo Ego

Reputation: 1335

ASP.NET webforms listbox - cross-page posting

I'm new to ASP.NET, and I'm having a problem getting a listbox to return the proper data when posting to another page. I have gotten dropdownlists and radio buttons to work. For instance, for dropdownlists, I simply did this:

Public ReadOnly Property SiteID() As String
    Get
        Return ddlSites.SelectedValue
    End Get

Then, on the next page, I successfully returned the value using PreviousPage.SiteID. However, when I try this for a listbox:

Public ReadOnly Property CustID() As String
    Get
        Return lstLoggedInCustomers.SelectedValue.ToString
    End Get
End Property

then call it using PreviousPage, I get an empty string, as SelectedIndex is always returning -1, even though I select an item in the listbox.

Upvotes: 3

Views: 495

Answers (3)

Thomas
Thomas

Reputation: 34198

In this case you can persist the value in viewstate before redirecting to another page. So from the next page you call just like:

Public ReadOnly Property CustID() As String
    Get
        Return viewstate("myCustID") 
    End Get
End Property

I hope it will work.

Upvotes: 0

Daniel
Daniel

Reputation: 2832

I had a similar situation. I used user252340's approach and store the value:

Session["MemberName"] = memberName;

and then in the PageLoad of the other page get the value:

 MemberName.Value = Session["MemberName"].ToString();

Hope this helps

Upvotes: 1

Pieter van Kampen
Pieter van Kampen

Reputation: 2077

The SelectedValue is read from the ViewState on postback. But the ViewState is no longer available on the next page. Why don't you save the SelectedValue in Session so you can refer to it on the next page?

Upvotes: 1

Related Questions