Click to See Complete Forum and Search --> : when will a control's id avaiable at a datalist? after databind? or OnPreRender?


sirpelidor
02-17-2006, 07:58 PM
Hi, I have a DataList inside a UserControl which is sitting inside a ASPX page. And I'm trying figure out ways to refer that control (textbox)'s ID.


the code i'm using now is:

TextBox txt4JavaScriptUsage = (TextBox)e.Item.FindControl("txtBox");
txt4JavaScriptUsage.Attributes.Add("onkeypress", "alert(Event.keycode);");


now I get a Null Pointer Reference runtime error, so the Question is...where should I put these codes in order to fire the alert on key press?

I'm guessing its under
Protected Overrides void OnPreRender(System.event.args e){}

however, there's no e.Item method avaiable under OnPreRender, Page, ...etc :(

Thanks for your help

sirpelidor
02-20-2006, 03:27 PM
HA HA HA, ITS ALWAYS AWESOME TO FIGURE PROBLEM OUT OURSELVES!!!! (although the price we have to pay is our time :( )

ALRIGHT, IF ANYONE INTERESTED AT MY SOLUTION HERE IT GOES:

there's a event for DataList class call: Item_Created, that event is being call right after datalist has generated all controls and right before it rend as html.

so in C#, you wanna add a delegate:


this.datalist.ItemCreated +=new DataListItemEventHandler(datalist_ItemCreated);


now you add your method assoicate with your delegate, in this case..i wanna add a "onkeypress" event javascript.


private void datalist_ItemCreated(object sender, DataListItemEventArgs e)
{
TextBox txt4JavaScriptUsage = (TextBox)e.Item.FindControl("dateTxtBox");
if (txt4JavaScriptUsage != null)
{
txt4JavaScriptUsage.Attributes.Add("onkeypress", "alert(Event.keycode);");
}
}//end employmentDataList_ItemCreated


txt4JavaScriptUsage != null is important because if your text box only shows up at your Edit Template, you will get null pointer references when the Item Template is rended.


see ya!