help with Web Control

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have created a web control (.ascx).
I placed the control on one of my asp.net page.
In the control I have following properties..

public int itemId;
private string itemName;

public string iName
{
get
{
return itemName;
}
}


Code in the control gets the itemId and writes Item information through the
render function and sets the value of itemName like
this.itemName = dtReader.getString(2);

Within the control I have verified that the property itemName or iName
works, but on the ASP.NET page it comes empty.

I can't figure out what am I doing wrong.

Any help?
 
Hi,

could it be that this value disappears on postback? That would be because
the property doesn't use ViewState (only member properties aren't restored
over a postback)

if you change the property to be

public string iName
{
get
{
string s=ViewState["itemName"] as string;
return (s==null) ? String.Empty : s;
}
set
{
ViewState["itemName"] = value;
}
}

and then the code setting this value would start using set accessor, does
that help? When you say in "render function" do you mean that setting the
proeprty happens in control's own overridden Render method?
 
This happens on the first time the page is loaded so not something to do with
viewstate.
mean that setting the
proeprty happens in control's own overridden Render method?
Yes I set the variable in Control's own overridden Render method.

I think its happening because on the ASP.net page, I try to access this
property in the Page_OnLoad event, even before the control is rendered.

Do you know a good article creating events on a web control?

Thanks.



Teemu Keiski said:
Hi,

could it be that this value disappears on postback? That would be because
the property doesn't use ViewState (only member properties aren't restored
over a postback)

if you change the property to be

public string iName
{
get
{
string s=ViewState["itemName"] as string;
return (s==null) ? String.Empty : s;
}
set
{
ViewState["itemName"] = value;
}
}

and then the code setting this value would start using set accessor, does
that help? When you say in "render function" do you mean that setting the
proeprty happens in control's own overridden Render method?


--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
 
Hmm,

if you try to access it in Page_Load that is way earlier than Control's own
Render method. See ther Page/control lifecycle goes like this for the Page
and controls (being already in the controls collection, dynamic ones play
catchup)

1. Instantiate
2. Initialize
3. TrackViewState
4. LoadViewState (postback)
5. Load postback data (postback, IPostBackDatahandler.LoadPostdata)
6. Load
7. Load postback data for dynamical controls added on Page_Load (postback)
8. Raise Changed Events (postback,
IPostBackDatahandler.RaisePostDataChanged)
9. Raise postback event (postback, IPostBackEventHandler.RaisePostBackEvent)
10.PreRender
11. SaveViewState
12. Render
13. Unload
14. Dispose
 
Back
Top