943,708 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 1514
  • ASP.NET RSS
Dec 1st, 2008
0

listbox and selectedvalue urgent

Expand Post »
hi,

I am trying to add a value to listbox. i use lstlistbox.items.add(new listitem(value)).

this adds it fine in click event of button, but i need to use the selectedvalue of the listbox again in same event.

as the listbox item doesnt show i cant use selectedvalue. is there a way to first populate the listbox with its selected value then somehow use the value.

thanks
Reputation Points: 10
Solved Threads: 1
Posting Whiz
sam1 is offline Offline
300 posts
since Nov 2004
Dec 1st, 2008
0

Re: listbox and selectedvalue urgent

I'm sorry but I don't understand your problem.
Lets assume you have the below stated example, it adds a few items to the listbox and after a selection is done, it responds the selected value to the page.....

Is thát what you achieved?

ASP.NET Syntax (Toggle Plain Text)
  1. Partial Class _Default
  2. Inherits System.Web.UI.Page
  3.  
  4. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  5. If Not Page.IsPostBack Then
  6.  
  7. For iCnt As Integer = 1 To 5
  8. Dim lItem As New ListItem
  9. lItem.Text = String.Format("New Item {0}", iCnt.ToString)
  10. lItem.Value = iCnt
  11. Me.ListBox1.Items.Add(lItem)
  12. lItem = Nothing
  13. Next iCnt
  14. End If
  15. End Sub
  16.  
  17. Protected Sub ListBox1_SelectedIndexChanged( _
  18. ByVal sender As Object, _
  19. ByVal e As System.EventArgs) _
  20. Handles ListBox1.SelectedIndexChanged
  21. HttpContext.Current.Response.Write( _
  22. String.Format("Selected Value = {0}", _
  23. CType(sender, ListBox).SelectedValue.ToString))
  24. End Sub
  25. End Class

pagecode;
ASP.NET Syntax (Toggle Plain Text)
  1. <%@ Page
  2. Language="VB"
  3. AutoEventWireup="false"
  4. CodeFile="Default.aspx.vb"
  5. Inherits="_Default" %>
  6. <!DOCTYPE
  7. html PUBLIC
  8. "-//W3C//DTD XHTML 1.0 Transitional//EN"
  9. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  10. <html xmlns="http://www.w3.org/1999/xhtml">
  11. <head runat="server">
  12. <title></title>
  13. </head>
  14. <body>
  15. <form
  16. id="form1"
  17. runat="server">
  18. <div>
  19. <asp:ListBox
  20. ID="ListBox1"
  21. runat="server"
  22. AutoPostBack="True" />
  23. </div>
  24. </form>
  25. </body>
  26. </html>
Last edited by 4advanced; Dec 1st, 2008 at 7:48 am.
Reputation Points: 33
Solved Threads: 10
Junior Poster in Training
4advanced is offline Offline
67 posts
since Nov 2008
Dec 2nd, 2008
0

Re: listbox and selectedvalue urgent

hii

Add the code code in your .aspx

ASP.NET Syntax (Toggle Plain Text)
  1. <asp:DropDownList id="ddl1" runat="server" AutoPostBack="True" AppendDataBoundItems="True" >
  2. <asp:ListItem Selected="True" Value="-1">--Select Me--</asp:ListItem>
  3. <asp:ListItem Value="1">Value 1</asp:ListItem>
  4. <asp:ListItem Value="2">Value 2</asp:ListItem>
  5.  
  6. </asp:DropDownList>

And you can bind additional values at run time from database and can use selected

ASP.NET Syntax (Toggle Plain Text)
  1. Private Sub ddl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddl1.SelectedIndexChanged
  2.  
  3. your code
  4. End Sub

Mark as solved if it helps you!!!

Regards
Reputation Points: 12
Solved Threads: 38
Junior Poster
reach_yousuf is offline Offline
194 posts
since Sep 2007
Dec 3rd, 2008
0

Re: listbox and selectedvalue urgent

can u specify problem
Reputation Points: 9
Solved Threads: 2
Newbie Poster
paslanmaz is offline Offline
13 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: print basket to image
Next Thread in ASP.NET Forum Timeline: displaying search results in grid





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC