根据下拉列表 ID 筛选网格视图. [网格视图外部]

本文关键字:网格 视图 外部 筛选 下拉列表 ID | 更新日期: 2023-09-27 18:33:53

我有一个下拉列表,它位于网格视图之外。下拉列表值来自主表。我希望根据下拉列表的选定值,应该过滤网格视图中的数据。请帮助我实现这一目标,因为我是新手,以前从未这样做过。请参阅我的下拉列表代码:-

<asp:DropDownList ID="ddlPagesNgo" runat="server" CssClass="selectpicker form-control-drp wd" Style="width: 100%" AutoPostBack="false"></asp:DropDownList>

Cs 代码:-

 private void Binddropdownlist()
    {
        SqlCommand cmd = new SqlCommand("Select * from tbl_ngoname", conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        ddlPagesNgo.DataTextField = ds.Tables[0].Columns["ngo_name"].ToString();
        ddlPagesNgo.DataSource = ds.Tables[0];
        ddlPagesNgo.DataBind();
        ddlPagesNgo.Items.Insert(0, new ListItem("--Select NGO--", "0"));
    }

另请参阅网格视图代码

<asp:GridView ID="grdCSRPageData" runat="server" Width="100%" border="1" Style="border: 1px solid #E5E5E5;" CellPadding="3"
                AutoGenerateColumns="False" OnDataBound="grdCSRPageData_DataBound" AllowPaging="true" CssClass="hoverTable"
                OnPageIndexChanging="grdCSRPageData_PageIndexChanging" DataKeyNames="Id" OnRowDeleting="grdCSRPageData_RowDeleting" 
                PageSize="5" ShowFooter="true" OnRowEditing="grdCSRPageData_RowEditing" OnRowUpdating="grdCSRPageData_RowUpdating" 
                OnRowCancelingEdit="grdCSRPageData_RowCancelingEdit">
                <AlternatingRowStyle CssClass="k-alt" BackColor="#f5f5f5"/>
                <Columns>
                    <asp:TemplateField HeaderText="Action" HeaderStyle-Width="5%" HeaderStyle-CssClass="k-grid td" >
                        <ItemTemplate>
                            <asp:Checkbox ID="chkSelect" runat="server" AutoPostBack="false" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="page_title"  HeaderText="Page Title" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td"  />
                    <asp:BoundField DataField="page_description" HeaderText="Page Description" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td" />
                    <asp:BoundField DataField="meta_title" HeaderText="Meta Title" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td" />
                    <asp:BoundField DataField="meta_keywords" HeaderText="Meta Keywords" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td" />
                    <asp:BoundField DataField="meta_description" HeaderText="Meta Description" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td" />
                    <asp:BoundField DataField="Active" HeaderText="Active" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td" />
                    <asp:TemplateField HeaderText="Action" HeaderStyle-Width="15%" HeaderStyle-CssClass="k-grid td" >
                        <ItemTemplate>
                            <asp:ImageButton ID="btnDelete" AlternateText="Delete" ImageUrl="~/images/delete.png" runat="server" Width="15" Height="15" CommandName="Delete" CommandArgument='<%# Eval("Id") %>' CausesValidation="false" OnClientClick="return confirm('Are you sure you want to delete this record?')" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:CommandField ButtonType="Image" ItemStyle-Width="15" EditImageUrl="~/images/edit.png" ShowEditButton="True" ControlStyle-Width="15" ControlStyle-Height="15" CancelImageUrl="~/images/close.png" UpdateImageUrl="~/images/update.png">
                        <ControlStyle Height="20px" Width="20px"></ControlStyle>
                    </asp:CommandField>
                </Columns>
            </asp:GridView>

如果您需要其他任何东西,请告诉我。

获取数据函数代码:-

  private DataTable GetData(SqlCommand cmd)
    {
        DataTable dt = new DataTable();
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString);
        SqlDataAdapter sda = new SqlDataAdapter();
        cmd.CommandType = CommandType.Text;
        cmd.Connection = conn;
        try
        {
            conn.Open();
            sda.SelectCommand = cmd;
            sda.Fill(dt);
            return dt;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            conn.Close();
            sda.Dispose();
            conn.Dispose();
        }
    }

根据下拉列表 ID 筛选网格视图. [网格视图外部]

获取下拉列表的选定索引更改事件的选定值,并将该值传递给查询要绑定到 gridview 的生成数据源

protected void ddlPagesNgo_SelectedIndexChanged(object sender, EventArgs e)
{
   BindGrid(ddlPagesNgo.SelectedValue);

}

您的下拉列表现在应该是

<asp:DropDownList ID="ddlPagesNgo" runat="server" CssClass="selectpicker form-control-drp wd" Style="width: 100%" AutoPostBack="True" OnSelectedIndexChanged="ddlPagesNgo_SelectedIndexChanged"></asp:DropDownList>

对网格视图绑定函数的更改

    public void BindGrid(int selectedID) 
    { 
    string strQuery = "select Id,page_title,page_description,meta_title,meta_keywords,meta_description,Active from tbl_Pages";
    strQuery +=" WHERE Id = "+selectedID; 
    strQuery +=" ORDER By Id DESC"; 
    SqlCommand cmd = new SqlCommand(strQuery); 
    DataTable dt = GetData(cmd); 
    grdCSRPageData.DataSource = dt; 
    grdCSRPageData.DataBind(); 
    }

另请查看参数化查询以防止 SQL 注入攻击。

我认为您必须执行以下操作:

  1. 在下拉值更改 - 提交表单(自动回发="true")上。
  2. 将 SelectedIndexChanged(对象发送方,EventArgs e) 事件处理程序添加到您的下拉菜单。
  3. 使用一些参数调用绑定下拉列表。
  4. 修改绑定下拉列表,以便为查询创建条件基于参数值。 使用来自的新数据重新绑定网格视图查询。

在 DropdownSelectedIndex 上绑定网格视图已更改。在下拉选择检查

        protected void ddlgrid_SelectedIndexChanged(object sender, EventArgs e)
     {
         bindGridview();
         if (ddlgrid.SelectedItem.Value == "1") // Dropdownlist Value 
         {
              // Write your Sql Procedure or Query To Display result 
         //  It will return only selected value in Gridview.
         }
        if (ddlgrid.SelectedItem.Value == "2")
         {
             // same here 
         }
     }