如何在由下拉列表选择的gridview中显示数据? [英] How to display data in a gridview which selected by a dropdown list ?

查看:64
本文介绍了如何在由下拉列表选择的gridview中显示数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只得到了下拉列表的第一个索引的输出。当我选择下一个索引时,它会刷新整个页面而没有响应。如何在下拉列表中获取所有其他索引的数据?

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?

推荐答案

您好,

查看此示例

Hi ,
Check this Example
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;
   }







<div>
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"

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





最好的问候

M.Mitwalli



Best Regards
M.Mitwalli


您好,



如果您尝试根据下拉列表绑定网格视图,那么 -



根据我的理解,在你的下拉列表中,第一个选项是默认选择的一个。另外,下拉列表的绑定应该在页面加载中发生。



尝试,

在页面加载的if(!IsPostback)中绑定你的下拉列表。



希望这会有所帮助。

请发布您的代码,以便我们帮助您找到正确的解决方案。
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.


p rotected 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 (选择*来自ODetails,其中orderID = @orderid,Cn))

{

Cn.Open();



Cmd.Parameters.AddWithValue(@ orderid,int.Parse(DropDownList1.SelectedValue));

SqlData Reader 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))

{

使用(SqlCommand Cmd = new SqlCommand(SELECT * FROM Orders,Cn))

{

Cn.Open();

SqlDataAdapter adpt = new SqlDataAdapter(Cmd);

adpt.Fill(dt);

}



}

返回dt;

}
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;
}


这篇关于如何在由下拉列表选择的gridview中显示数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆