在dropdownlist indexchanged上填充gridview [英] populate gridview on dropdownlist indexchanged

查看:56
本文介绍了在dropdownlist indexchanged上填充gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨每个团体

i有gridview从数据库填充日期我想更改下拉列表中的选定数据SelectedIndexChanged我试试这个代码但是nosense这是我的代码



hi every body
i have gridview which populate date from database i want to change seclected data on dropdown SelectedIndexChanged i try this code but nosense this is my code

protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           BindData();
       }
   }

private void BindData()
   {
       if (ddlTguidedit.SelectedIndex==0)
       {
           string strQuery = "SELECT [Pdfid],[Arpdf_name],[Arpdf_des],[pdf_date] FROM [books_alaa].[dbo].[Tbl_uploadpdf]";
           SqlCommand cmd = new SqlCommand(strQuery);
           GridView1.DataSource = GetData(cmd);
           GridView1.DataBind();
       }
       else
       {
           string strQuery = " SELECT Pdfid, Enpdf_name AS Arpdf_name, Enpdf_des AS Arpdf_des, pdf_url, pdf_date FROM Tbl_uploadpdf";
           SqlCommand cmd = new SqlCommand(strQuery);
           GridView1.DataSource = GetData(cmd);
           GridView1.DataBind();
       }
   }

   private DataTable GetData(SqlCommand cmd)
   {
       DataTable dt = new DataTable();
       SqlConnection con = new SqlConnection(strConnString);
       SqlDataAdapter sda = new SqlDataAdapter();
       cmd.CommandType = CommandType.Text;
       cmd.Connection = con;
       con.Open();
       sda.SelectCommand = cmd;
       sda.Fill(dt);
       return dt;
   }

protected void ddlTguidedit_SelectedIndexChanged(object sender, EventArgs e)
   {
       if (ddlTguidedit.SelectedIndex == 0)
       {
           string strQuery = "SELECT [Pdfid],[Arpdf_name],[Arpdf_des],[pdf_date] FROM [books_alaa].[dbo].[Tbl_uploadpdf]";
           SqlCommand cmd = new SqlCommand(strQuery);
           GridView1.DataSource = GetData(cmd);
           GridView1.DataBind();
       }
       else
       {
           string strQuery = " SELECT Pdfid, Enpdf_name AS Arpdf_name, Enpdf_des AS Arpdf_des, pdf_url, pdf_date FROM Tbl_uploadpdf";
           SqlCommand cmd = new SqlCommand(strQuery);
           GridView1.DataSource = GetData(cmd);
           GridView1.DataBind();
       }
   }





为什么它不起作用?



why it doesnt work ??

推荐答案

你在if和else块中的查询似乎是从表 Tbl_uploadpdf 获取数据。您也没有任何条件来检索数据。它直接获取表中的所有数据。
Your query inside both if and else block seems to be getting the data from Table Tbl_uploadpdf. You also don't have any condition for retrieving the data. It is directly getting all the data from the Table.


在查询中为您的其他部分添加 where 条件,否则有if-else条件的获取数据没有差异。
Add a where condition in your query for your else part otherwise there is no difference in the fetched data for if-else condition.


这篇关于在dropdownlist indexchanged上填充gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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