如何使用asp.net gridview selectbox过滤器 [英] how to use asp.net gridview selectbox filter

查看:41
本文介绍了如何使用asp.net gridview selectbox过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在asp.net gridview中我有这样的DropDownList



Inside asp.net gridview i have a DropDownList like this

<asp:DropDownList ID="ddlTrimName" AutoPostBack="true" runat="server" CssClass="content-grd"

ClientIDMode="Static" OnSelectedIndexChanged="ddlTrimName_SelectedIndexChanged">                  </asp:DropDownList>





这将填写页面加载。现在我想添加一个过滤器。当用户类型记录需要过滤时。在jquery中有没有任何方法可以做到这一点?



This will fill in page load. Now I want to add a filter. when user type records needs to be filter. is there any method in jquery to do this?

推荐答案

HTML Markup

The HTML Markup contains an ASP.Net GridView with DropDownList in ItemTemplate of TemplateField.







<asp:gridview id="GridView1" runat="server" autogeneratecolumns="false" onrowdatabound="OnRowDataBound" xmlns:asp="#unknown">
    <columns>
        <asp:boundfield headertext="Name" datafield="ContactName" />
        <asp:templatefield headertext="Country">
            <itemtemplate>
                <asp:label id="lblCountry" runat="server" text="<%# Eval("Country") %>" visible="false" />
                <asp:dropdownlist id="ddlCountries" runat="server">
                </asp:dropdownlist>
            </itemtemplate>
        </asp:templatefield>
    </columns>
</asp:gridview>

 

Binding the GridView

The below code binds the ASP.Net GridView with records from the Customers table of the Northwind database using C#


protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        GridView1.DataSource = GetData("SELECT ContactName, Country FROM Customers");
        GridView1.DataBind();
    }
}
 
private DataSet GetData(string query)
{
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlCommand cmd = new SqlCommand(query);
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            using (DataSet ds = new DataSet())
            {
                sda.Fill(ds);
                return ds;
            }
        }
    }
}

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //Find the DropDownList in the Row
        DropDownList ddlCountries = (e.Row.FindControl("ddlCountries") as DropDownList);
        ddlCountries.DataSource = GetData("SELECT DISTINCT Country FROM Customers");
        ddlCountries.DataTextField = "Country";
        ddlCountries.DataValueField = "Country";
        ddlCountries.DataBind();

        //Add Default Item in the DropDownList
        ddlCountries.Items.Insert(0, new ListItem("Please select"));
          
        //Select the Country of Customer in DropDownList
        string country = (e.Row.FindControl("lblCountry") as Label).Text;
        ddlCountries.Items.FindByValue(country).Selected = true;
    }
}


这篇关于如何使用asp.net gridview selectbox过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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