Testing existance of a datagrid control on a webform

Discussion in 'ASP .Net Datagrid Control' started by Iain Porter, Nov 25, 2003.

  1. Iain Porter

    Iain Porter Guest

    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
     
    Iain Porter, Nov 25, 2003
    #1
    1. Advertising

  2. Iain Porter

    Teemu Keiski Guest

    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.

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



    "Iain Porter" <> wrote in message
    news:...
    > 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
    >
    >
    >
     
    Teemu Keiski, Nov 26, 2003
    #2
    1. Advertising

  3. Iain Porter

    Iain Porter Guest

    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



    "Teemu Keiski" <> wrote in message
    news:...
    > 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.
    >
    > --
    > Teemu Keiski
    > MCP, Microsoft MVP (ASP.NET), AspInsiders member
    > ASP.NET Forum Moderator, AspAlliance Columnist
    >
    >
    >
    > "Iain Porter" <> wrote in message
    > news:...
    > > 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
    > >
    > >
    > >

    >
    >
     
    Iain Porter, Nov 26, 2003
    #3
  4. Iain Porter

    Iain Porter Guest

    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


    "Iain Porter" <> wrote in message
    news:...
    > 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
    >
    >
    >
    > "Teemu Keiski" <> wrote in message
    > news:...
    > > 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.
    > >
    > > --
    > > Teemu Keiski
    > > MCP, Microsoft MVP (ASP.NET), AspInsiders member
    > > ASP.NET Forum Moderator, AspAlliance Columnist
    > >
    > >
    > >
    > > "Iain Porter" <> wrote in message
    > > news:...
    > > > 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
    > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Iain Porter, Nov 26, 2003
    #4
  5. Iain Porter

    Iain Porter Guest

    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

    "Iain Porter" <> wrote in message
    news:...
    > 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
    >
    >
    > "Iain Porter" <> wrote in message
    > news:...
    > > 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
    > >
    > >
    > >
    > > "Teemu Keiski" <> wrote in message
    > > news:...
    > > > 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.
    > > >
    > > > --
    > > > Teemu Keiski
    > > > MCP, Microsoft MVP (ASP.NET), AspInsiders member
    > > > ASP.NET Forum Moderator, AspAlliance Columnist
    > > >
    > > >
    > > >
    > > > "Iain Porter" <> wrote in message
    > > > news:...
    > > > > 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
    > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Iain Porter, Nov 26, 2003
    #5
  6. Iain Porter

    Teemu Keiski Guest

    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




    "Iain Porter" <> wrote in message
    news:...
    > 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
    >
    >
    >
    > "Teemu Keiski" <> wrote in message
    > news:...
    > > 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.
    > >
    > > --
    > > Teemu Keiski
    > > MCP, Microsoft MVP (ASP.NET), AspInsiders member
    > > ASP.NET Forum Moderator, AspAlliance Columnist
    > >
    > >
    > >
    > > "Iain Porter" <> wrote in message
    > > news:...
    > > > 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
    > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Teemu Keiski, Nov 26, 2003
    #6
  7. Iain Porter

    Teemu Keiski Guest

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

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


    "Iain Porter" <> wrote in message
    news:...
    > 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
    >
    >
    > "Iain Porter" <> wrote in message
    > news:...
    > > 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
    > >
    > >
    > >
    > > "Teemu Keiski" <> wrote in message
    > > news:...
    > > > 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.
    > > >
    > > > --
    > > > Teemu Keiski
    > > > MCP, Microsoft MVP (ASP.NET), AspInsiders member
    > > > ASP.NET Forum Moderator, AspAlliance Columnist
    > > >
    > > >
    > > >
    > > > "Iain Porter" <> wrote in message
    > > > news:...
    > > > > 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
    > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Teemu Keiski, Nov 26, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. titof
    Replies:
    0
    Views:
    525
    titof
    Jul 24, 2003
  2. Iain Porter

    Testing existance of a control on the page

    Iain Porter, Nov 25, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    373
    Iain Porter
    Nov 26, 2003
  3. timmso
    Replies:
    1
    Views:
    516
    Alex Papadimoulis
    Dec 12, 2003
  4. darrel

    testing for cookies' existance

    darrel, Aug 26, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    627
    darrel
    Aug 26, 2005
  5. Iain Porter

    Testing existance of a datagrid control on a webform

    Iain Porter, Nov 25, 2003, in forum: ASP .Net Building Controls
    Replies:
    6
    Views:
    152
    Teemu Keiski
    Nov 26, 2003
Loading...

Share This Page