Multi Value Data Binding and State Management

I

Icon Iconoclast

I am facing a problem with state management while using Multi Value Data
Binding. ViewState is enabled. When I click on a radiobutton and click the
Button, the output always is:
From Listbox selection: -1

What's the solution to retain state?

Here's Default.aspx:
##############
<body>
<form id="form1" runat="server">
<div>
<asp:RadioButtonList ID="rdLaptops" runat="server" />
<asp:Button ID="cmd" runat="server" Text="OK" onclick="cmd_Click" />
<asp:Label ID="lbl" runat="server" Text="" /><br />
</div>
</form>
</body>
###############

Here's Default.aspx.cs:
###############
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

List<string> myLaptops=new List<string>();
myLaptops.Add("Lenovo");
myLaptops.Add("DELL");
myLaptops.Add("Acer");
myLaptops.Add("HP");

rdLaptops.DataSource = myLaptops;
DataBind();
}
protected void cmd_Click(object sender, EventArgs e)
{
string myString;
myString = rdLaptops.SelectedIndex.ToString();
lbl.Text = "From Radiobutton selection: " + myString;
}
}
###################
 
S

Steve S

Your resetting the datasource each time the page is posted back to the
server. Try checking for Page.IsPostBack, see code below.

if (!Page.IsPostBack)
{
List<string> myLaptops = new List<string>();
myLaptops.Add("Lenovo");
myLaptops.Add("DELL");
myLaptops.Add("Acer");
myLaptops.Add("HP");

rdLaptops.DataSource = myLaptops;
DataBind();
}
 
B

bruce barker

better yet, move the code to oninit and turn off viewstate (make those
pages lighter and faster).

-- bruce (sqlwork.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top