Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 6th, 2011, 12:11 PM
Authorized User
Points: 222, Level: 4
Points: 222, Level: 4 Points: 222, Level: 4 Points: 222, Level: 4
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2007
Location: Phoenix, AZ, USA.
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..
  #2 (permalink)  
Old July 6th, 2011, 02:26 PM
Imar's Avatar
Wrox Author
Points: 72,035, Level: 100
Points: 72,035, Level: 100 Points: 72,035, Level: 100 Points: 72,035, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,079
Thanks: 80
Thanked 1,587 Times in 1,563 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)
  #3 (permalink)  
Old July 7th, 2011, 11:55 AM
Authorized User
Points: 222, Level: 4
Points: 222, Level: 4 Points: 222, Level: 4 Points: 222, Level: 4
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2007
Location: Phoenix, AZ, USA.
Posts: 48
Thanks: 4
Thanked 0 Times in 0 Posts
Default Listbox

Hi Imar,

Thanks for the info.

Tom
__________________
Thomas G Magaro
  #4 (permalink)  
Old May 21st, 2012, 06:00 AM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 01:36 PM.


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