在dropdownlist indexchanged上填充gridview

时间:2014-07-27 11:02:08

标签: asp.net .net gridview

我有gridview,它从数据库中填充日期我想更改下拉列表中的选定数据当我从数据库中选择第一个索引时,它选择了数据,当我改变选择它获取另一个数据时尝试此代码但是nosense这是我的代码

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

为什么它不起作用?

1 个答案:

答案 0 :(得分:0)

!IsPostBack()删除BindData()。您正在DropDownList SelectedIndexChanged上加载网格数据。 BindData()函数不需要在!IsPostBack()中。无论DropDownList是什么索引,BindData()函数总是加载,它总是将索引作为0。