如何将GridView绑定到下拉列表选择

如何将GridView绑定到下拉列表选择

本文介绍了如何将GridView绑定到下拉列表选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

这是我的代码,当用户从两个不同的下拉列表中选择image_id和logo_id ...相应地,图像和徽标将显示在两个gridviews中...但是当用户在dropdownlist中更改数据时,gridview不会反映更改

hi everyone,

this is my code where when user selects image_id and logo_id from two different dropdownlist...accordingly images and logos will be displayed on two gridviews...but when user is changing data in dropdownlist the gridview doesnot reflects the change

public partial class USER_SEND_WATERMARK_REQUEST : System.Web.UI.Page
{
    String uName;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["uName"] != null)
        {
            Label2.Text = Request.QueryString["uName"];
            uName = Request.QueryString["uName"];
        }
        if (!IsPostBack)
        {
            dropdownlist1bind();
            dropdownlist2bind();
            BindGridView(sender,e);
            BindGridView2(sender,e);
        }

    }
    protected void LinkButton7_Click1(object sender, EventArgs e)
    {
        Response.Redirect("USER_HOME.aspx?uName="+uName);
    }
    protected void LinkButton6_Click1(object sender, EventArgs e)
    {
        Response.Redirect("LOGIN.aspx");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    }
     public void  dropdownlist1bind()
    {
        try
        {
           SqlConnection con= new SqlConnection(ConfigurationManager.ConnectionStrings["WATERMARKING"].ToString());
           SqlCommand cmd=new SqlCommand();

            cmd.CommandText = "Select IMAGE_ID from IMAGE where USERNAME = '"+ uName +"' ";
            cmd.Connection = con;
            con.Open();

            DropDownList1.DataSource = cmd.ExecuteReader();
            DropDownList1.DataTextField = "IMAGE_ID";
            DropDownList1.DataBind();

        }
        catch (Exception ex)
        {
            Label11.Text="error";
        }

    }
    public void dropdownlist2bind()
    {
        try
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["WATERMARKING"].ToString());
            SqlCommand cmd = new SqlCommand();

            cmd.CommandText = "Select LOGO_ID from LOGO where USERNAME = '" + uName + "' ";
            cmd.Connection = con;
            con.Open();

            DropDownList2.DataSource = cmd.ExecuteReader();
            DropDownList2.DataTextField = "LOGO_ID";
            DropDownList2.DataBind();

        }
        catch (Exception ex)
        {
            Label12.Text = "error";
        }

    }

    public void BindGridView(object sender, EventArgs e)
    {
        DropDownList ss = DropDownList1;
        int i;
        if (int.TryParse(ss.SelectedValue, out i))
        {
            ConnectionStringSettings set = ConfigurationManager.ConnectionStrings["WATERMARKING"];
            String cns = set.ConnectionString;
            SqlConnection con = new SqlConnection(cns);
            con.Open();
            string sql = "Select IMAGE_CONTENT,IMAGE_ID from IMAGE where USERNAME='" + uName + "' and IMAGE_ID= '" + i + "'";
            SqlDataAdapter da = new SqlDataAdapter(sql, cns);
            DataTable dt = new DataTable();
            da.Fill(dt);
            gvCheckboxes.DataSource = dt;
            gvCheckboxes.DataBind();



        }
        else
        {
            Label13.Text = "no IMAGE selected";
        }

    }


    public void BindGridView2(object sender, EventArgs e)
    {
        DropDownList ss = DropDownList2;
       int i;
       if (int.TryParse(ss.SelectedValue, out i))
       {
           ConnectionStringSettings set = ConfigurationManager.ConnectionStrings["WATERMARKING"];
           String cns = set.ConnectionString;
           SqlConnection con = new SqlConnection(cns);
           con.Open();
           string sql = "Select LOGO_CONTENT,LOGO_ID from LOGO where USERNAME='" + uName + "' and LOGO_ID= '" + i + "'";
           SqlDataAdapter da = new SqlDataAdapter(sql, cns);
           DataTable dt = new DataTable();
           da.Fill(dt);
           GridView2.DataSource = dt;
           GridView2.DataBind();
       }
       else
       {
           Label14.Text = "no LOGO selected";
       }

    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList1.SelectedIndexChanged += new EventHandler(BindGridView);

    }
    protected void gvCheckboxes_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList2.SelectedIndexChanged += new EventHandler(BindGridView2);
    }
}



[版本]添加了前置标记



[edition] Added pre tags

推荐答案

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
   DropDownList1.SelectedIndexChanged += new EventHandler(BindGridView);
}


您只是将事件处理程序附加到SelectedIndexChanged evevt上,而没有其他操作.究竟是什么你想/inted做的是调用BindGridView事件(并重新绑定数据网格)每当有选择的指数的变化.因此,您的代码也无法按预期工作.

尝试更简单的方法:


You are just attaching an event handler to the SelectedIndexChanged evevt and nothing else. What actually you wanted/inted to do was to call BindGridView event (and rebind data to grid) whenever there is a change in selected index. Thus, your code does not work as you expect it too.

Try something simpler like:

   protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dropdownlist1bind();
            dropdownlist2bind();
            BindGridView("");
            BindGridView2("");
        }
    }

    // Similar implementation for BindGridView method 
    public void BindGridView2(string currentSelectedValue)
    {
       // Keeping 'i' intact as it looks like you are using some logic to get its value 
       // and then use it in query to fetch data
       int i;
       if (int.TryParse(currentSelectedValue, out i))
       {
           ConnectionStringSettings set = ConfigurationManager.ConnectionStrings["WATERMARKING"];
           String cns = set.ConnectionString;
           SqlConnection con = new SqlConnection(cns);
           con.Open();
           string sql = "Select LOGO_CONTENT,LOGO_ID from LOGO where USERNAME='" + uName + "' and LOGO_ID= '" + i + "'";
           SqlDataAdapter da = new SqlDataAdapter(sql, cns);
           DataTable dt = new DataTable();
           da.Fill(dt);
           GridView2.DataSource = dt;
           GridView2.DataBind();
       }
       else
       {
           Label14.Text = "no LOGO selected";
       }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGridView(DropDownList1.SelectedValue);
    }

    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGridView2(DropDownList2.SelectedValue);
    }
}


这篇关于如何将GridView绑定到下拉列表选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 21:17