Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I got the output for only first index of the drop down list. When I select the next index , it refresh the whole page and no response. How to get data for for all other indexes in the drop down list?
Posted
Comments
Sunil Kumar Pandab 10-Sep-12 5:03am    
Share your code so that we can clarify your problem.

Hi ,
Check this Example
C#
protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           DropDownList1.DataSource = GetData();
           DropDownList1.DataValueField = "orderID";
           DropDownList1.DataTextField = "OrderAmount";
           DropDownList1.DataBind();
       }
   }
   protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
   {
       using (SqlConnection Cn = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))
       {
           using (SqlCommand Cmd = new SqlCommand("select * from ODetails where orderID= @orderid", Cn))
           {
               Cn.Open();

               Cmd.Parameters.AddWithValue("@orderid", int.Parse(DropDownList1.SelectedValue));
               SqlDataReader Dr = Cmd.ExecuteReader();
               if (Dr.HasRows)
               {
                   GridView1.DataSource = Dr;
                   GridView1.DataBind();
               }
               Dr.Close();

               Cn.Close();
           }

       }
   }
   DataTable GetData()
   {
       DataTable dt = new DataTable();
       using (SqlConnection Cn = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))
       {
           using (SqlCommand Cmd = new SqlCommand("SELECT * FROM  Orders ", Cn))
           {
               Cn.Open();
               SqlDataAdapter adpt = new SqlDataAdapter(Cmd);
               adpt.Fill(dt);
           }

       }
       return dt;
   }


XML
<div>
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
        onselectedindexchanged="DropDownList1_SelectedIndexChanged">
    </asp:DropDownList>
    <br />
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
</div>


Best Regards
M.Mitwalli
 
Share this answer
 
Comments
Manjunatha Reddy.M 19-Dec-13 12:08pm    
I've created two controls like as dropdown list and list box, based on both selections I need to display the sharepoint list items using caml query, if any ideas let me know.
Hi,

If you are trying to bind your grid view based on dropdown, then-

As per my understanding, in your dropdown first option is the default selected one.Also, the binding of dropdown should be happening in pageload.

Try,
binding your dropdown in if(!IsPostback) of the pageload.

Hope this helps.
Please post your code so that we can help with the correct solution.
 
Share this answer
 
v2
Comments
gayani dassa 10-Sep-12 5:18am    
yeah. I have use if(!IsPostBack)method. I will post my code . thank you.
gayani dassa 10-Sep-12 5:41am    
//code of relevant page
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = dba.getsupname();
g_ddl_supplier.DataSource = dt;
g_ddl_supplier.DataValueField = "Sup_Name";
g_ddl_supplier.DataTextField = "Sup_Name";
g_ddl_supplier.DataBind();

}
protected void g_ddl_supplier_SelectedIndexChanged(object sender, EventArgs e)
{
string name = g_ddl_supplier.Text;


DataSet ds = dba.getsupname1(name);
GridView2.DataSource = ds.Tables["Equipment"].DefaultView;
GridView2.DataBind();

}

//in DB Access class
public DataSet getsupname1(string sname)
{

if (conn.State.ToString() == "Closed")
{
conn.Open();
}

SqlCommand newCmd = conn.CreateCommand();
newCmd.Connection = conn;
newCmd.CommandType = CommandType.Text;

newCmd.CommandText = "SELECT Sup_Name,Equip_Type,SerialNo FROM Equipment WHERE Sup_Name ='"+ sname +"'";
newCmd.ExecuteNonQuery();

SqlDataAdapter dr = new SqlDataAdapter(newCmd);
DataSet ds = new DataSet();
dr.Fill(ds, "Equipment");

conn.Close();
return ds;
}
manognya kota 10-Sep-12 5:47am    
i dint find
if(!IsPostBack)method in your pageload event.

try,

protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostback)
{
DataTable dt = dba.getsupname();
g_ddl_supplier.DataSource = dt;
g_ddl_supplier.DataValueField = "Sup_Name";
g_ddl_supplier.DataTextField = "Sup_Name";
g_ddl_supplier.DataBind();
}

}
gayani dassa 10-Sep-12 6:05am    
now its working for the other indexes in the drop down list except first index.
manognya kota 10-Sep-12 6:54am    
What is not working?Did you put a breakpoint and check what value is selected is actually passed to getsupname1(string sname) method?Which index is set as default?
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.DataSource = GetData();
DropDownList1.DataValueField = "orderID";
DropDownList1.DataTextField = "OrderAmount";
DropDownList1.DataBind();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
using (SqlConnection Cn = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))
{
using (SqlCommand Cmd = new SqlCommand("select * from ODetails where orderID= @orderid", Cn))
{
Cn.Open();

Cmd.Parameters.AddWithValue("@orderid", int.Parse(DropDownList1.SelectedValue));
SqlDataReader Dr = Cmd.ExecuteReader();
if (Dr.HasRows)
{
GridView1.DataSource = Dr;
GridView1.DataBind();
}
Dr.Close();

Cn.Close();
}

}
}
DataTable GetData()
{
DataTable dt = new DataTable();
using (SqlConnection Cn = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))
{
using (SqlCommand Cmd = new SqlCommand("SELECT * FROM Orders ", Cn))
{
Cn.Open();
SqlDataAdapter adpt = new SqlDataAdapter(Cmd);
adpt.Fill(dt);
}

}
return dt;
}
 
Share this answer
 
Comments
AnvilRanger 29-Oct-15 7:45am    
Answering a question that is 3 years old that already has an accepted solution is considered spam and can lead to getting your account banned.

It is even worse to just copy and paste that accepted solution as your own.
On selected index change of dropdown filter your gridview datasource according to the selected value of dropdown and bind it. Use just like this in your SelectedIndexChanged event of dropdown:
C#
var query = from c in YoourDataTable.AsEnumerable() where c.Field<string>("ID") == ddl.SelectedValue.ToString() select s;
GridView1.DataSource = query;
GridView1.DataBind();



--Amit
 
Share this answer
 
v2

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