How to maintain form controls value after user control post back?

 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);
}
}

0
adhi
2/23/2009 12:38:31 PM
asp.net.web-forms 93655 articles. 5 followers. Follow

4 Replies
1213 Views

Similar Articles

[PageSpeed] 33

 In the Page_Load event, add this code

 

if(!IsPostBack)

{

 //Bind Dropdowns

}


Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.

http://blog.nishanthnair.com
0
nishanthnair
2/23/2009 1:14:32 PM

thansk for reply

i already did this, i got same problem

0
adhi
2/23/2009 1:45:17 PM

 Can u email me your aspx and aspx.cs to nair.nishanth@gmail.com and i will look into it . here its tough to read your post. In future if u  r posting coede,

Please click on the Source code icon in the Messagebox, and paste the code there so that it is displayed in a formatted manner. 


Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.

http://blog.nishanthnair.com
0
nishanthnair
2/23/2009 1:50:40 PM

ok. thanks for your caring i will send to your email id,

0
adhi
2/23/2009 1:58:11 PM
Reply:

Similar Artilces:

How to access individual controls from a form containing the web user control containing those controls
 Below is a block of code in my .aspx form named default.aspx. In this form I load a web user control. I then add it to a place holder control.(Lines 3 and 4 below) So how do I access the individual controls in my web user control? I want to set the properties on those controls, but I am baffled as to how to access the control. For example I have a text label control named "label3". How do I access this control. Because for example, under certain situations I want to hide that control. ( Label3.Visible = false;)  1    if(!Page.IsPostBack) 2&nb...

how to call controls of web control form in coding of other forms?
hello everyone i have one problem i have created one webcontrol shoppingcart in which there are two labels one of qty and other of amt which are initially enable =falsenow on other page(aspx page) i have one txtbox of qty as txtqty i want tat as soon as user enters qty txtqty and clicks on add cart button it shd be display d value in lblqty of web control shopping cart make sure i m using datalist on aspx page so give me suggestions on tat basis and my coding is in vb. plz give ur suggestions as fast as possible. Neeti what do you mean by webcontrol.... it this user control or normal ...

Retreiving a value from a web form within a web user control
Hi  I have a page (web form) that retreives some data from a database. To be more specific: at some point when user clicks a button (in the web form) there is some ID number retreived from a database in code behind. At the same time the Visible property of the nestet web user control is chamged to true. I want than forward the ID to the web user control so that a grid view can load data from a database for that ID. How can I access that data (the ID) within a web user control context nested in that page?  Kind regards   Check this ;ink  http://www.codeproject.com/K...

Solution for web forms that look just like paper forms ? User controls ? Adobe fillable forms ?
 I am looking at ways to web-enable a series of company forms that need to have the form itself look just like the paper version.  It would be great if I could use an image of the form itself as a background, and then draw the UI components on top of this.  Then there is the question of printing these forms complete with the data that was filled in.   What are some options for doing this kind of application ?  Im open to 3rd party solutions as well.  Our company currently has a desktop application which does this, but I want to come up with a web based solu...

Problem in accessing web form data in user controls when create user control dynamically
HI. I have a problem using web user control. I designed a user control named NewsForm. This user control contains some web form elements like <asp:textbox>. When I add it in an .ASPX page, all things may be true. I can know which user filles in textbox (for example and other controls) <%@ Register TagPrefix="uc1" TagName="NewsForm" Src="NewsForm.ascx" %> and <UC1:NewsForm id="newsForm" runat="server"></UC1:NewsForm> But when i create it in code behined dynamically, however user control has web form controls but their value are blank for example //aspx co...

Controlling web control instances on Web Form
I have created a composite control, extending functionality of the Hidden Control. However, I would like to prevent the user from adding this control to the page multiple times. I only want one instance of the control on the Web Form. What setting in my project to I adjust to enable this functionality? I can not find it in any of the books that I have referenced. Is this a setting or is this code that has to be added to the inheriting class. Looks like there is no standard apporach. But I'd consider using static field to count number of created objects and throw exception if second object ...

multiple user control into a web form (how do I keep the status of each control?)
A user control implements a quiz.The control has a property (id_question) to choose the different question the user control have to display. The user control have another property, its STATUS (status = 1 mean to show the question, status=2 means to show a message to the user that have answered the question). In the user control there is a button, clicking the button to answer the question cause the control post the form and call a DB and register the answer. During Postback, in page_load method of the user control I change the status from 1 to 2 (from show quest...

when loading a previously created record on to form, some controls are missing values(controls are depended on values selected in other controls)
Hello - I have created a page for Editing records.  this page will open up a chosen record and allow user to edit the data.On the form, I have 3 drop down lists.   The second dropdown list should fill values depending on value chosen in first dropdown list.  The third dropdown list should show/fill values depending on value chosen in second dropdown list.So taking this into count, i have several records with various data.  When I open-up/load a record in Edit form, only the first dropdown list has a value selected that was the pre-existing va...

Pass Values from a user control to a web form
Hi All,  I am currently trying to add captcha to my webform. I have a question on how to pass a number/string from a .ascx to a .aspx page. The aspx is being used in the .ascx (user control) in an image tag. <asp:Image ID="imgCaptcha" runat="server" ImageUrl="Image.aspx"/> The aspx page is being rendered as a jpeg image. So Inorder for the captcha to be displayed with the image, I need to pass the code from the user control to the aspx page. I have an option to pass the number as a query string or use it in session. But is there any ot...

access controls on web user control x from web user control Y
I have 2 web users controls in a page and I would like to get or set the selected values of a few dropdown boxes on web user control X from web user control Y and I also would like to know how to access functions that are on a web user control X from a web user control Y, is this possible? How can I do it? User can access and set the values of the User Control from .aspx page through properties,using javascript and in code-behind of aspx page.The details of it are shown below Using PropertiesIf the Sample.ascx control has two textboxes and submit button.You can access the valu...

Reading form values in dynamically created child controls in a user control
I have a common problem for which I've searched the web for answers for hours to no avail. I have some radiobuttonlists ,dropdownlists,checkboxlists and text controls in a user control. The lists are created statically, however the items in the lists are populated at run time. The lists are displayed in default.aspx (the containing page) and populated on the Page_Load event of the user control .ascx file based on an external data source containing arrays of data. I want to be able to read the values selected by the user from deafult.aspx, the parent page. details: The .ascx...

user control data entry form, use same codebehind as web form?
Hi All, I have a quesiton about the approach I'm taking. I have a series of report forms which I have been in the process of consolidating to a single, generic page and subs/functions in a single code-behind. All the reports can use the same set of data entry elements (a dropdown, a radio list, and a textbox). I'm trying to convert these elements to a user control. However, this user control seems to need to inherit differently than the rest of the code-behind. I've been using a single class in the code behind. Is it feasible to separate out the data entry? the code behind im...

How do you expose an event from a server control within an user control on an web form?
I have a calendar inside of an user control Simple.ascx The user control is sitting on my web form Simple.aspx. How do I access the VisibleMonthChanged event from my web form Simple.aspx? Is it possible? Yes Create Event in usercontrol which fires on page when VisibleMonthChanged event fires on usercontrol. Jigar Desai-----------------------Do not forget to "Mark as Answer" on the post that helped you. Ok, I created the event Public Event MonthChanged() in Simple.ascx.vb. Then I add the follow to Simple.aspx.vb Private Sub Simple_MonthChanged() Si...

Web Form and Web User Control
Hello, I have a Web User Control (ascx) that includes a button. This button has an onclick event method. This Web User control is included into a Web Form. (thru LoadControl). However, when I click on the button, the event method is not executed. What should I include in order it works fine? Thanks in advance. GAA. Using Loadcontrol is using dynamic controls and when using dynamic controls you must recreate the same dynamic controls ono postback to econstruct the page control tree. It is best if you do this during the Page PreInit event. How complex are your controls ...