hello experts,
I have created filter grid in asp.net & C#.
My form design has contains 3 dropdown controls and Filter grid(User control).
Its working fine(filter), but one problem arise.
the form controls are clear when i filter a grid(post back).
I tried to reterive values using view state, hidden controls, i got only null values.
Anyone please help, its urgent to me.
Its my user control code.

FilterGrid.aspx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="FilterGrid.ascx.cs" Inherits="FilterGrid" %>
<style type = "text/css">
.cssPager td
{
border-left: white 1px solid;
padding-left: 4px; 
padding-right: 4px; 
padding-bottom:4px; 
}
.cssHead th
{
border-left: white 1px solid;
padding-left: 4px; 
padding-right: 4px; 
padding-bottom:4px; 
}
</style> 
<script type="text/javascript" language="javascript">
//this method sets filter condition.
function filtergrid(path, columnname)
{
var val;
var baseurl; 
baseurl = path; 
val = document.getElementById('txt' + columnname).value;
self.location.href = baseurl + '?selectedcolumn=' + columnname + '&selectedvalue=' + val;
}
//this method removes filter condition.
function removefilter(path)
{
var baseurl; 
baseurl = path; 
self.location.href = baseurl;
}
</script>
 
<asp:datagrid id="ListGrid" runat="server" AutoGenerateColumns="false" AllowPaging="true" CssClass="cssPager" Font="verdana"
Font-Size="15px" GridLines="Vertical" BorderColor="Transparent" BorderStyle="None" Width="100%" OnPageIndexChanged="ListGrid_PageIndexChanged" PageSize="15">
<SelectedItemStyle BackColor="#C0C0FF" />
<PagerStyle HorizontalAlign="Left" VerticalAlign="Middle" NextPageText="" PrevPageText="" Mode="NumericPages" />
<AlternatingItemStyle BackColor="#D2D8DE" /> 
<ItemStyle BackColor="White" HorizontalAlign="Left" />
<HeaderStyle BackColor="#467DA0" CssClass="cssHead" ForeColor="White" Font-Bold="True" HorizontalAlign="Center" VerticalAlign="Middle"/>
</asp:datagrid>

FilterGrid.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Utilities;
public partial class FilterGrid : System.Web.UI.UserControl
{
#region " Properties and Instance Variables "
private string _FilterColumnName;
private string _FilterValue;
private string[] _ColumnNames;
private string[] _DisplayColumnNames;
string _path = string.Empty;
DataTable dtGrid = new DataTable();
clsGridFilter objFilter = new clsGridFilter();
#endregion 
protected void Page_Load(object sender, EventArgs e)
{
}
public void BindData(string path, DataTable dtTable, string[] columnNames, string[] DisplayNames)
{
ListGrid.Columns.Clear();
_ColumnNames = columnNames;
_DisplayColumnNames = DisplayNames;
_path = path;
dtGrid = dtTable;
//initialization method 
MyInit();
DataView listView = default(DataView);
listView = FilterDataSource(dtTable);
if ((listView != null))
{
FillDataGridColumns(path, listView.Table, _ColumnNames, _DisplayColumnNames);
}
ListGrid.DataSource = listView;
ListGrid.DataBind();
}
//initialization method 
private void MyInit()
{
SetQueryParameters();
}
//Sets the instance variables 
private void SetQueryParameters()
{
_FilterColumnName = Request.QueryString["selectedcolumn"];
_FilterValue = Request.QueryString["selectedvalue"];
}
//Filters Datasource 
private DataView FilterDataSource(DataTable ListTable)
{
//Dataview Row Filter based on querystring 
//This example handles "String","Double" datatypes and can be expanded to any other datatypes. 
DataView tempView = new DataView(ListTable);
if ((_FilterColumnName != null) & (_FilterValue != null))
{
switch (ListTable.Columns[_FilterColumnName].DataType.ToString().ToUpper())
{
case "SYSTEM.STRING":
//for strings - "Like 'xxx%'" 
tempView.RowFilter = "[" + _FilterColumnName + "] Like '" + _FilterValue + "%'";
break;
case "SYSTEM.DOUBLE":
//for numeric values - "= condition" 
tempView.RowFilter = "[" + _FilterColumnName + "] = " + _FilterValue;
break;
case "SYSTEM.DECIMAL":
if (_FilterValue != "")
{
tempView.RowFilter = "[" + _FilterColumnName + "] = " + _FilterValue;
}
break;
case "SYSTEM.DATETIME":
if (_FilterValue != "")
{
tempView.RowFilter = "[" + _FilterColumnName + "] = " + _FilterValue;
}
break;
}
}
return tempView;
}
private void FillDataGridColumns(string path, DataTable ListTable, string[] ColumnNames, string[] DisplayNames)
{
int i = 0;
string headerhtml = null;
// int InCollectionIndx = 0;
ListGrid.AutoGenerateColumns = false;
if ((ColumnNames != null))
{
if ((ListTable != null))
{
for (i = 0; i <= ColumnNames.Length - 1; i++)
{
headerhtml = null;
if (ColumnNames[i].Trim().Length > 0)
{
if (ListTable.Columns.Contains(ColumnNames[i]))
{
BoundColumn x = new BoundColumn();
//set Column DataField 
x.DataField = ColumnNames[i];
//set Header Display 
if ((DisplayNames != null))
{
if (DisplayNames.Length > i)
{
if (DisplayNames[i].Trim().Length > 0)
{
//Header with textbox and go button 
headerhtml = DisplayNames[i];
headerhtml = headerhtml + " <br> " + "<Input type=text style=width:70px;height:15px class=FilterTextBox id=txt" + ColumnNames[i] + " />&nbsp;&nbsp;<Input class=ButtonStyle style=height:20px type=button id=btn" + ColumnNames[i] + " onclick=\"javascript:filtergrid('" + path + "','" + ColumnNames[i] + "');\" value='Go'/>";
x.HeaderText = headerhtml;
if ((_FilterColumnName != null) & (_FilterValue != null))
{
if (ColumnNames[i].Trim().ToUpper() == _FilterColumnName.Trim().ToUpper())
{
//if filterd column then add "Remove" link to the header text. 
headerhtml = DisplayNames[i];
headerhtml = headerhtml + " <br> " + "<Input style=width:70px;height:15px type=text class=FilteredTextBox id=txt" + ColumnNames[i] + " value='" + _FilterValue + "' />&nbsp;<Input class=ButtonStyle style=height:20px type=button id=btn" + ColumnNames[i] + " onclick=\"javascript:filtergrid('" + path + "','" + ColumnNames[i] + "');\" value='Go' />";
headerhtml = headerhtml + "&nbsp;&nbsp;" + "<a style=color:Red href=\"javascript:removefilter('" + path + "');\" >Remove</a>";
x.HeaderText = headerhtml;
}
}
}
}
}
x.Visible = true;
//add to datagrid 
// ListGrid.DataSource = ListTable; 
ListGrid.Columns.Add(x);
}
}
}
}
}
}
protected void ListGrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
ListGrid.CurrentPageIndex = e.NewPageIndex;
BindData(_path, dtGrid, _ColumnNames, _DisplayColumnNames);
}
}

hi;

i am not a C# Expert but,

in vb.net we use condition
if not ispostback()then

//code

end if

try to find something like this in C#

good luck;

This article has been dead for over six months. Start a new discussion instead.