Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Friends,

I have used listbox control that populating values from sql table.In the gridview bound field I have used the listbox column.If we click select link in gridview it will go to next page and view the selected row's items.But in the next page selected list box item is not viewing in next page.

Default4.aspx
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
           AutoGenerateEditButton="True" AutoGenerateSelectButton="True"
           onselectedindexchanged="GridView1_SelectedIndexChanged">
           <Columns>
               <asp:BoundField DataField="BugNumber" HeaderText="Id" />
           </Columns>
       </asp:GridView>



Default4.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=CDC015;Initial Catalog=Dashboard;Persist Security Info=True;User ID=prima;Password=123abcd*");
        con.Open();
        SqlCommand cmd = new SqlCommand("select BugNumber from Bug", con);
        SqlDataAdapter ada = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        ada.Fill(ds, "sample");
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
        
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            if (GridView1.SelectedRow != null)
            {

                GridViewRow selectedRow = GridView1.SelectedRow;

                Session["BugNumber"] = selectedRow.Cells[1].Text;
                Response.Redirect("Default5.aspx");
            }
        }
    }
}


Default5.aspx
<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>


Default.aspx.cs
if (this.PreviousPage != null)
       {

       }
       string PrimaryImpactedArea = System.Web.HttpContext.Current.Session["BugNumber"].ToString();
       ListBox1.SelectedItem.Text = PrimaryImpactedArea;



In the above code i am unable to see the selected listbox item from gridview column into next page.Plz help me.



Thanks in advance
Posted
Comments
Er Daljeet Singh 14-Nov-13 1:26am    
Dear first of all add some value in the listbox ,
second check wether the session is having or not.
third i session is having value then that value should be present in the listbox

Try below code in Default5.aspx.cs page to keep session item selected in listbox1:

C#
foreach (ListItem item in ListBox1.Items)
{
  var PrimaryImpactedArea = Session["BugNumber"];
  if (PrimaryImpactedArea != null && item.Text == PrimaryImpactedArea)
  {
    item.Selected = true;
  }
}
 
Share this answer
 
You are Binding your datagrid in Page_Load event.
So it every time Bind it when it get Post Back. Just you need to do is
Put it inside if Block with Check IsNotPostback

If(Page.IsNotPostBack)
{
Logic of Datagrid Binding
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900