Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
|
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 6th, 2011, 12:11 PM
Authorized User
 
Join Date: Dec 2007
Posts: 48
Thanks: 4
Thanked 0 Times in 0 Posts
Default 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
__________________
Thomas G Magaro

Last edited by flashmanTom; July 6th, 2011 at 12:14 PM..
 
Old July 6th, 2011, 02:26 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
flashmanTom (July 8th, 2011)
 
Old July 7th, 2011, 11:55 AM
Authorized User
 
Join Date: Dec 2007
Posts: 48
Thanks: 4
Thanked 0 Times in 0 Posts
Default Listbox

Hi Imar,

Thanks for the info.

Tom
__________________
Thomas G Magaro
 
Old May 21st, 2012, 06:00 AM
Registered User
 
Join Date: May 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
ListBox selected items andreas5 ASP.NET 4 General Discussion 2 August 31st, 2010 06:03 PM
Getting Listbox selected value into a string arnabghosh ASP.NET 3.5 Professionals 1 October 20th, 2009 09:30 PM
HELP in if else control.....listbox value is not selected properly skhan ASP.NET 2.0 Basics 1 August 26th, 2009 02:35 PM
Set Listbox Selected property DaDeViL VB How-To 7 October 4th, 2006 04:04 PM
How to get selected Item from Listbox? Iceman571 ADO.NET 1 March 25th, 2004 06:33 AM





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