Testing existance of a datagrid control on a webform

I

Iain Porter

Hi all,
I have a usercontrol (a textbox with dropdown calendar that fills the
textbox with the selected date) that I implement in a datagrid in a webform.
On first loading the page, the datagrid's visibility is set to 'false', and
so the UC depCal is not rendered on the page. Hence the following code in
the InitializeComponent() method causes an error:

this.depCal.DateChanged += new
custCalendar.DateChangedEventHandler(this.depCal_DateSet);

The error is: 'Object reference not set to an instance of an object'

Hence I think what I need to do is check for the existance of the control
depCal on the page before exectuing the above code snippet. I tried the
code below, again in the InitializeComponent() method, but it didn't work.
Even when the Datagrid and hence the control depCal is visible on the page,
I never get inside the IF statement.

if ( depCal != null ) {
this.depCal.DateChanged += new
custCalendar.DateChangedEventHandler(this.depCal_DateSet);
}

Can you tell me if I'm approaching the problem from the right angle, and how
to solve it? Thanks in advance for your help,

Iain
 
T

Teemu Keiski

I think that you would need to wire event handlers in this case

a) dynamically in ItemCreated event handler of the DataGrid (in code)
or
b) declaratively in the aspx (in user control itself)

this way wiring goes hand-on-hand with the control. This of course requires
that DateChanged event is top-level event on user control as you probably
have developed it.
 
I

Iain Porter

A bit more background - I'm new to .NET and OOP, so please forgive any naive
questions! - thanks for your time.

My UserControl contains the following code:
// the event stuff
public delegate void DateChangedEventHandler(object sender, EventArgs
e);
public event DateChangedEventHandler DateChanged;

protected void OnDateChanged(EventArgs e) { // declare method that fires
event
if (DateChanged != null) {
DateChanged(this, e);
}
}

public void SelectionChanged(Object sender, EventArgs e) {
OnDateChanged(e);
}

// a property
public string CurrentDate {
get { return txtDate.Text; }
set { txtDate.Text = value; }
}

My .aspx file contains a datagrid with the following column:
<asp:templatecolumn HeaderText="Date">
<itemtemplate>
<asp:label id="date" runat="server" />
</itemtemplate>
<edititemtemplate>
<usercontrol:calendar runat="server" id="dateCal"
CurrentDate='<%# DataBinder.Eval(Container.DataItem, "date") %>'
/>
</edititemtemplate>
<asp:templatecolumn>

My .aspx.cs file contains the following in the InitialiseComponent() event:
this.depCal.DateChanged += new
custCalendar.DateChangedEventHandler(this.depCal_DateSet);

Sooo. The point of the whole thing is to get the value in the textbox of
the usercontrol into the page on which the usercontrol sits, for submission
to a database. The code I have works fine, except that the usercontrol is
not always apparant on the page - only after a button is clicked. Hence the
code in the InitializeComponent() event causes an error 'Object reference is
not set to an instance of the object'.

Teemu, if you've answered my question already, i apologise, but could you
elaborate?
Does the above mean that I can't wire the event declaratively in the
usercontrol?
Can I put the code that I have in the InitializeComponent() event
anywhere, and as you're suggesting, some sort of ItemCreated event
somewhere?
What is a top-level event as opposed to a lower level? How is it
defined and what are the implications?

Thanks a whole lot,
Iain
 
I

Iain Porter

By 'dynamically in ItemCreated event handler of the DataGrid (in code)' do
you something like the following code, placed in the InitializeComponent()
event?
grid.Load += new System.EventArgs(this.ItemCreated)

And then in ItemCreated, have the code:

this.dateCal.DateChanged += new
custCalendar.DateChangedEventHandler(this.dateCal_DateSet);

Which links to the code we already have:

private void dateCal_DateSet(object sender, EventArgs e) {
DateCalVal = dateCal.SelectedDate;
}

Thanks again,

Iain
 
I

Iain Porter

Well, I tried that.

The Grid_Load event occurs, and the method below runs:
private void Grid_Load(object sender, System.EventArgs e) { //new
if ( dateCal != null ) { //new
this.dateCal.DateChanged += new
custCalendar.DateChangedEventHandler(this.dateCal_DateSet); //new
}
}

However, still even though I can see the dateCal right there on the page, it
still skips over the if statement. I'm not sure whether the problem is with
how I'm testing for the existance of the control, or when I'm testing for
it.

Is there another event that comes after the page or control Load that I by
which time the usercontrol register as not null when I can see it on the
page?

Thanks again,
Iain
 
T

Teemu Keiski

You could wire an event handler in the aspx like:

....
<edititemtemplate>
<usercontrol:calendar runat="server" id="dateCal" CurrentDate='<%#
DataBinder.Eval(Container.DataItem, "date") %>'
OnDateChanged="depCal_DateSet" />
</edititemtemplate>
<asp:templatecolumn>
</edititemtemplate>
...
Because the event is top level event in user control. I.e
On<EventName>="handler_name". Top-level event is just the event that is
exposed outside the component/control, like in this case it brings the
Calendar's event "to the top" in the UC.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
 
T

Teemu Keiski

It is DataGrid's ItemCreated event (not Load event), and in its handler you
could search up the UC instance and wire the event handler. ItemCreated is
called once for every item created in the grid (Item,AlternatingItem and so
on).

//Handle ItemCreated (this could be in Page's Init or InitializeComponent()
in VS.NET
this.DataGrid1.ItemCreated += new
System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemCreate
d);

//ItemCreated handler
private void DataGrid1_ItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.AlternatingItem ||
e.Item.ItemType==ListItemType.Item)
{
//Locate UC instance
<UCType> uc=(<UCType>)e.Item.FindControl("UC_id");

//Wire event handler for the instance
uc.DateChanged += new
custCalendar.DateChangedEventHandler(this.dateCal_DateSet);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top