Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. binding value with listbox

binding value with listbox

Scheduled Pinned Locked Moved ASP.NET
databasewpfwcfhelp
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jainiraj
    wrote on last edited by
    #1

    I am binding value with a listbox.when an item is selected and then a button is clicked then i want to display a label with text as value bounded with that item. here is code.. query = "select CustId,FirstName+' '+LastName as Name from custdetails where BrokerId='" + Session["broker"] + "'"; dt = obj.select(query); ListBox1.DataSource = dt; ListBox1.DataTextField = "Name"; ListBox1.DataValueField = "CustId"; this.DataBind(); protected void Button1_Click(object sender, EventArgs e) { //String value = .ToString(); Label2.Text = ListBox1.SelectedValue; } but problem is label becomes invisible i.e. it is not taking any value.

    S M 2 Replies Last reply
    0
    • J jainiraj

      I am binding value with a listbox.when an item is selected and then a button is clicked then i want to display a label with text as value bounded with that item. here is code.. query = "select CustId,FirstName+' '+LastName as Name from custdetails where BrokerId='" + Session["broker"] + "'"; dt = obj.select(query); ListBox1.DataSource = dt; ListBox1.DataTextField = "Name"; ListBox1.DataValueField = "CustId"; this.DataBind(); protected void Button1_Click(object sender, EventArgs e) { //String value = .ToString(); Label2.Text = ListBox1.SelectedValue; } but problem is label becomes invisible i.e. it is not taking any value.

      S Offline
      S Offline
      scy278753517
      wrote on last edited by
      #2

      Have you done it yet? I can't help you ,I am new here ,if you get the answer,please let me konw ,thank you.

      I can do!

      1 Reply Last reply
      0
      • J jainiraj

        I am binding value with a listbox.when an item is selected and then a button is clicked then i want to display a label with text as value bounded with that item. here is code.. query = "select CustId,FirstName+' '+LastName as Name from custdetails where BrokerId='" + Session["broker"] + "'"; dt = obj.select(query); ListBox1.DataSource = dt; ListBox1.DataTextField = "Name"; ListBox1.DataValueField = "CustId"; this.DataBind(); protected void Button1_Click(object sender, EventArgs e) { //String value = .ToString(); Label2.Text = ListBox1.SelectedValue; } but problem is label becomes invisible i.e. it is not taking any value.

        M Offline
        M Offline
        mdazeemuddin
        wrote on last edited by
        #3

        Hi jain declare one variable in your code and use OnSelectedIndexChanged event of list box grape that item using str=listbox.selecteditem.text where str is a string variable and listbox is ur listbox id use this str variable in ur button i hope this will help you

        J 1 Reply Last reply
        0
        • M mdazeemuddin

          Hi jain declare one variable in your code and use OnSelectedIndexChanged event of list box grape that item using str=listbox.selecteditem.text where str is a string variable and listbox is ur listbox id use this str variable in ur button i hope this will help you

          J Offline
          J Offline
          jainiraj
          wrote on last edited by
          #4

          on writing protected void Button1_Click(object sender, EventArgs e) { String value = ListBox1.SelectedItem.Value; Label2.text=value; } It is giving an exception System.NullReferenceException: Object reference not set to an instance of an object. plz help...

          T 1 Reply Last reply
          0
          • J jainiraj

            on writing protected void Button1_Click(object sender, EventArgs e) { String value = ListBox1.SelectedItem.Value; Label2.text=value; } It is giving an exception System.NullReferenceException: Object reference not set to an instance of an object. plz help...

            T Offline
            T Offline
            Tuwing Sabado
            wrote on last edited by
            #5

            Your listbox biding is correct but you should place it under Page Load event delegate and "Not Page.IsPostBack" condition to retain your listbox state. protected void Page_Load(object sender, EventArgs e) { if(!Page.IsPostBack) { query = "select CustId,FirstName+' '+LastName as Name from custdetails where BrokerId='" + Session["broker"] + "'"; dt = obj.select(query); ListBox1.DataSource = dt; ListBox1.DataTextField = "Name"; ListBox1.DataValueField = "CustId"; ListBox1.DataBind(); //<-- replace this with ListBox1 } } protected void Button1_Click(object sender, EventArgs e) { Label2.Text = ListBox1.SelectedValue; }

            J 1 Reply Last reply
            0
            • T Tuwing Sabado

              Your listbox biding is correct but you should place it under Page Load event delegate and "Not Page.IsPostBack" condition to retain your listbox state. protected void Page_Load(object sender, EventArgs e) { if(!Page.IsPostBack) { query = "select CustId,FirstName+' '+LastName as Name from custdetails where BrokerId='" + Session["broker"] + "'"; dt = obj.select(query); ListBox1.DataSource = dt; ListBox1.DataTextField = "Name"; ListBox1.DataValueField = "CustId"; ListBox1.DataBind(); //<-- replace this with ListBox1 } } protected void Button1_Click(object sender, EventArgs e) { Label2.Text = ListBox1.SelectedValue; }

              J Offline
              J Offline
              jainiraj
              wrote on last edited by
              #6

              Thanks a lot.... But why did it not work before...

              T 1 Reply Last reply
              0
              • J jainiraj

                Thanks a lot.... But why did it not work before...

                T Offline
                T Offline
                Tuwing Sabado
                wrote on last edited by
                #7

                You have a typo error from your first post. See the bold font below. query = "select CustId,FirstName+' '+LastName as Name from custdetails where BrokerId='" + Session["broker"] + "'"; dt = obj.select(query); ListBox1.DataSource = dt; ListBox1.DataTextField = "Name"; ListBox1.DataValueField = "CustId"; this.DataBind(); //"this" must be replaced by ListBox1.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups