binding value with listbox
-
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.
-
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.
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!
-
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.
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
-
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
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...
-
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...
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; }
-
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; }
-
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.