In my previous post I explained about selected fields to show in Gridview, now I am going to explain how to filter the data by selected fields using textbox.
asp.net code
<select id="selectfields2" name="selectfields" data-width="100%" multiple class="selectpicker show-tick" data-live-search="true" data-style="btn-danger" title="Select fields" enableviewstate="true">
<option value="*">Select All</option>
<option value="Address">Address</option>
</select>
<asp:TextBox ID="txtfilter" runat="server" OnTextChanged="txtfilter_TextChanged" AutoPostBack="true" CssClass="form-control" placeholder=" search here"></asp:TextBox>
<asp:HiddenField ID="hdfields" runat="server" />
<asp:HiddenField ID="hdfieldsquery" runat="server" />
C# CODE
protected void txtfilter_TextChanged(object sender, EventArgs e)
{
string FieldName2 = Page.Request.Form["selectfields"].ToString();
string[] SplitFieldName2 = FieldName2.Split(',');
foreach(string st in SplitFieldName2)
{
hdfieldsquery.Value = hdfieldsquery.Value + st + " like '%" + txtfilter.Text.ToString() + "%' or ";
}
if (FieldName2.ToString() != "*")
{
if (hdfields.Value.ToString() != "")
{
string SQLQuery2 = "Select * from FinalData where " + hdfieldsquery.Value.ToString() + "IDS like '%" + txtfilter.Text.ToString() + "%' or University like '%" + txtfilter.Text.ToString() + "%' or State like '%" + txtfilter.Text.ToString() + "%' order by IDS ASC";
SqlCommand CMD = new SqlCommand(SQLQuery2);
gv.DataSource = GetData(CMD);
gv.DataBind();
hdfieldsquery.Value = null;
}
}
}
}
Hope the above code will help you, if you have any query please revert.
asp.net code
<select id="selectfields2" name="selectfields" data-width="100%" multiple class="selectpicker show-tick" data-live-search="true" data-style="btn-danger" title="Select fields" enableviewstate="true">
<option value="*">Select All</option>
<option value="Address">Address</option>
</select>
<asp:TextBox ID="txtfilter" runat="server" OnTextChanged="txtfilter_TextChanged" AutoPostBack="true" CssClass="form-control" placeholder=" search here"></asp:TextBox>
<asp:HiddenField ID="hdfields" runat="server" />
<asp:HiddenField ID="hdfieldsquery" runat="server" />
C# CODE
protected void txtfilter_TextChanged(object sender, EventArgs e)
{
string FieldName2 = Page.Request.Form["selectfields"].ToString();
string[] SplitFieldName2 = FieldName2.Split(',');
foreach(string st in SplitFieldName2)
{
hdfieldsquery.Value = hdfieldsquery.Value + st + " like '%" + txtfilter.Text.ToString() + "%' or ";
}
if (FieldName2.ToString() != "*")
{
if (hdfields.Value.ToString() != "")
{
string SQLQuery2 = "Select * from FinalData where " + hdfieldsquery.Value.ToString() + "IDS like '%" + txtfilter.Text.ToString() + "%' or University like '%" + txtfilter.Text.ToString() + "%' or State like '%" + txtfilter.Text.ToString() + "%' order by IDS ASC";
SqlCommand CMD = new SqlCommand(SQLQuery2);
gv.DataSource = GetData(CMD);
gv.DataBind();
hdfieldsquery.Value = null;
}
}
}
}
Hope the above code will help you, if you have any query please revert.