如何在客户端列中搜索gridview [英] How to search the gridview on column wise in client side

查看:100
本文介绍了如何在客户端列中搜索gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在在第一行绑定文本框如何搜索相应列中的记录



我尝试过:



protected void grdColdStorageDetails_DataBound(object sender,EventArgs e)

{

GridViewRow row = new GridViewRow(0,0, DataControlRowType.Header,DataControlRowState.Normal);

for(int i = 0; i< grdColdStorageDetails.Columns.Count; i ++)

{

TableHeaderCell cell = new TableHeaderCell();

TextBox txtSearch = new TextBox();

txtSearch.Attributes [placeholder] = grdColdStorageDetails.Columns [i]。 HeaderText;

txtSearch.CssClass =search_textbox;

cell.Controls.Add(txtSearch);

row.Controls.Add(cell );

}

grdColdStorageDetails.HeaderRow.Parent.C ontrols.AddAt(1,row);

}

i bound the textboxes in first row now how to search the records in corresponding columns

What I have tried:

protected void grdColdStorageDetails_DataBound(object sender, EventArgs e)
{
GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
for (int i = 0; i < grdColdStorageDetails.Columns.Count; i++)
{
TableHeaderCell cell = new TableHeaderCell();
TextBox txtSearch = new TextBox();
txtSearch.Attributes["placeholder"] = grdColdStorageDetails.Columns[i].HeaderText;
txtSearch.CssClass = "search_textbox";
cell.Controls.Add(txtSearch);
row.Controls.Add(cell);
}
grdColdStorageDetails.HeaderRow.Parent.Controls.AddAt(1, row);
}

推荐答案

如果尝试将服务器端控件与客户端功能。 GridView是一个服务器端数据控件,提供您可以利用的服务器端功能。如果你想在客户端(JavaScript)中操作GridView中的数据,那么你将不得不学习JavaScript来操作DOM(HTML元素)。您可以尝试查看jQuery lib,因为它提供了可用于操作DOM的选择器。但请注意,如果您不熟悉客户端脚本,这可能需要您花费大量时间和精力。



您可以使用其他选项煮熟的客户端网格,可以完成网格所需的所有基本功能。一个很好的例子是使用Bootstrap DataTables。请参阅此演示文稿: DataTables示例 - 单个列搜索(文本输入) [ ^ ]
Things will get more complicated if you try to combine server-side controls with client-side functionality. GridView is a server-side data control which provides server-side capabilities that you can take advantage of. If you want manipulate the data in GridView at the client (JavaScript) then you'll have to learn JavaScript to manipulate the DOM (HTML elements). You could try looking at jQuery lib as it provides selectors that you can use to manipulate the DOM. But be aware that this may take you alot of time and effort if you are not familiar with client-side scripting.

You other option would be to use a pre-cooked client-side grid that does all the basic things you need for your grid. One good example is using Bootstrap DataTables. See this for demo: DataTables example - Individual column searching (text inputs)[^]


试试这个我的示例代码..你需要改变它

try this this my sample code... you have to change it
void bindsearch()
       {
           if (txtcname.Text == "" && txtdname.Text == "" && ddlicense.SelectedItem.Text == "All" && ddStatus.SelectedItem.Text == "All")
           {
               bind();
               return;
           }
           string str = "";

           if (txtcname.Text.Trim() != "")
           {
               str = "and ClientMst.ClientName like '%" + txtcname.Text.Trim() + "%'";
           }
           if (txtdname.Text.Trim() != "")
           {
               str = "and ClientMst.DisplayName like '%" + txtdname.Text.Trim() + "%'";
           }
           if (txtcname.Text.Trim() != "" && txtdname.Text.Trim() != "")
           {
               str = "and ClientMst.ClientName = '" + txtcname.Text.Trim() + "'";
           }
           if (ddStatus.SelectedItem.Text.Trim() != "All")
           {
               str = str + "and Active ='" + ddStatus.SelectedValue.Trim() + "'";
           }
           if (ddlicense.SelectedItem.Text.Trim() != "All")
           {
               str = str + "and License ='" + ddlicense.SelectedValue.Trim() + "'";
           }
           princ.Columns[1].Visible = true;
           string query = "  select ClientID,ClientName,DisplayName,ContPerson,contmob,case when License=1 then 'Full' else (Replace(convert(Varchar(20),Period,113),' ','/')) end as License, " +
                          "  Period,case when Active=0 then 'Yes' else 'No' end as Active " +
                          "  from ClientMst where ClientID<>'' " + str + " ";
           SqlCommand cmd = new SqlCommand(query, con);
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           DataTable dt = new DataTable();
           da.Fill(dt);
           if (dt.Rows.Count != 0)
           {
               princ.DataSource = dt;
               princ.DataBind();
           }
           else
           {
               ddStatus.ClearSelection();
               ddlicense.ClearSelection();
               txtcname.Text = "";
               txtdname.Text = "";
               bind();
               MessageInfo.MessageIcon = MessageIcons.ErrorIcon;
               TMessageBox1.Show(this.Title, "NO Record", (TMessageBox.MessageIcons)MessageInfo.MessageIcon, true);
               return;
           }
           princ.Columns[1].Visible = false;
       }


这篇关于如何在客户端列中搜索gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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