Tuesday, March 17, 2009

Persist data from a user control back to the page

This is maybe something very specific to my scenario, but I needed to persist data from a user control back to the page holding this control.

I used the following.

In the Page Class: (MyPage)
------------------
I declared the class as such

class MyPage : BasePage, MyFilter.IFilterPersist

I added the following methods

public string GetLastSearchSupplier(string controlId)
{
if (ViewState[controlId] == null) ViewState[controlId] = "-12";
return (string)ViewState[controlId];
}
public void SetLastSearchSupplier(string controlId, string value)
{
ViewState[controlId] = value;
}

In the User Control: (MyFilter)
--------------------

I declared an Interface

public interface IFilterPersist
{
string GetLastSearchSupplier(string controlId);
void SetLastSearchSupplier(string controlId, string value);
}

I declared an instance of the filter

IFilterPersist ownerI;

In the constructor I instantiated the filter

this.ownerI = parentPage as IFilterPersist;

Where I wanted to Get or Set this value to persist it to the containing page I did this

call GetLastSearch... / SetLastSearch...(userControl.ID, "sdsad");

No comments:

Post a Comment