p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 2.0 Professional (http://p2p.wrox.com/forumdisplay.php?f=215)
-   -   Listbox control does not pass the selected value when clicked (http://p2p.wrox.com/showthread.php?t=84267)

flashmanTom July 6th, 2011 12:11 PM

Listbox control does not pass the selected value when clicked
 
Hi,

I am trying to work the delete role example in chapter 16. Just as an exercise, I'm trying to get the role clicked in the listbox deleted from the list. However, the list box doesn't return the selected value.

Here is my code:

<code>
<%@PageLanguage="C#" %>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
scriptrunat="server">
protected void Page_Load(object sender, EventArgs e)
{
Page.SetFocus(TextBox1);
ListBoxDataBind();
}

protected void Button1_Click(object sender, EventArgs e)
{
Roles.CreateRole(TextBox1.Text.ToString());
ListBoxDataBind();
TextBox1.Text = "";
}
protected void ListBoxDataBind()
{
ListBox1.DataSource = Roles.GetAllRoles();
ListBox1.DataBind();
}
protected void DeleteButton_Click(object sender, EventArgs e)
{
if (ListBox1.SelectedValue != "")
{
Roles.DeleteRole(ListBox1.SelectedValue);
ListBoxDataBind();
}
}
</script>
<
htmlxmlns="http://www.w3.org/1999/xhtml">
<
headrunat="server">
<title>Role Manager</title>
</
head>
<
body>
<formid="form1"runat="server">
<h1>Role Manager</h1>
<b>Add Role:</b><br/>
<asp:TextBoxID="TextBox1"runat="server"></asp:TextBox>
<p><asp:ButtonID="Button1"runat="server"Text="Add Role to Application"OnClick="Button1_Click"/></p>
<b>Roles Defined:</b><br/>
<asp:ListBoxID="ListBox1"runat="server"></asp:ListBox>
<p><asp:ButtonID="DeleteButton"runat="server"Text="Delete Role"OnClick="DeleteButton_Click"/></p>
</form>
</
body>
</
html>
</code>

Any help will be appreciated.

flashmanTom

Imar July 6th, 2011 02:26 PM

Hi there,

It's because you're rebinding the list on each post back. Try this instead:

Code:


protected void Page_Load(object sender, EventArgs e)
{
  Page.SetFocus(TextBox1);
  if (!Page.IsPostBack)
  { 
    ListBoxDataBind();
  }
}

Hope this helps,

Imar

flashmanTom July 7th, 2011 11:55 AM

Listbox
 
Hi Imar,

Thanks for the info.

Tom

NdAsit1441 May 21st, 2012 06:00 AM

I have bind the list box control in page post back event.
when application is running in ipad, the control get changed from listbox to dropdown shape and on button click it does not get the selected value.
but the same thing is working on a different page


All times are GMT -4. The time now is 09:06 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.