Problem calling event handler in web form (C#)

Discussion in 'ASP .Net' started by Andrew Banks, Mar 3, 2004.

  1. Andrew Banks

    Andrew Banks Guest

    I've got the following code as part of a C# web form but am having problems
    calling a command. I create a dataset and put some data on the screen. This
    works fine. (relevant sample below)

    foreach (DataRow row in ds.Tables["Orders"].Rows)
    {
    detailsbtn = new LinkButton();
    //Assign a unique ID to the details control
    detailsbtn.ID = "details" + row["OrderID"].ToString().Replace("-",
    String.Empty);
    detailsbtn.Text =
    DateTime.Parse(row["OrderDate"].ToString()).ToShortDateString()+" - No.
    "+row["OrderNumber"].ToString()+" from user " +row["UserName"].ToString();
    detailsbtn.CommandArgument = row["OrderID"].ToString();
    detailsbtn.Command += new CommandEventHandler(OnViewDetails);
    // Add the controls
    phOrders.Controls.Add(detailsbtn);
    phOrders.Controls.Add(new LiteralControl("<br>"));
    }

    I then have the following for the OnViewDetails handler. When I click one of
    the LinkButtons created in the above code, this handler should be called but
    it doesn't seem to be calling it. It simply posts back to the page but
    doesn't set the text of the label.

    private void OnViewDetails(Object sender, CommandEventArgs e)
    {
    this.lblMessage.Text = "Button has been pressed";
    }

    As this is part of a bigger page, I've uploaded the aspx and cs files to
    http://andrew-banks.co.uk/problem.zip if people need access to the full code
    in order to help.

    Thanks in advance,
    Andrew
     
    Andrew Banks, Mar 3, 2004
    #1
    1. Advertising

  2. Quite possibly you have a sequence issue here. Event Handlers fire in a
    certain sequence, and if the code that adds the event handler runs after the
    control event handlers are processed, it never will be processed. Here is a
    link to a page that details the sequence of events in all ASP.Net Controls
    (including Page):

    http://msdn.microsoft.com/library/d...guide/html/cpconControlExecutionLifecycle.asp

    --
    HTH,
    Kevin Spencer
    ..Net Developer
    Microsoft MVP
    Big things are made up
    of lots of little things.

    "Andrew Banks" <> wrote in message
    news:Hkk1c.7492$...
    > I've got the following code as part of a C# web form but am having

    problems
    > calling a command. I create a dataset and put some data on the screen.

    This
    > works fine. (relevant sample below)
    >
    > foreach (DataRow row in ds.Tables["Orders"].Rows)
    > {
    > detailsbtn = new LinkButton();
    > //Assign a unique ID to the details control
    > detailsbtn.ID = "details" + row["OrderID"].ToString().Replace("-",
    > String.Empty);
    > detailsbtn.Text =
    > DateTime.Parse(row["OrderDate"].ToString()).ToShortDateString()+" - No.
    > "+row["OrderNumber"].ToString()+" from user " +row["UserName"].ToString();
    > detailsbtn.CommandArgument = row["OrderID"].ToString();
    > detailsbtn.Command += new CommandEventHandler(OnViewDetails);
    > // Add the controls
    > phOrders.Controls.Add(detailsbtn);
    > phOrders.Controls.Add(new LiteralControl("<br>"));
    > }
    >
    > I then have the following for the OnViewDetails handler. When I click one

    of
    > the LinkButtons created in the above code, this handler should be called

    but
    > it doesn't seem to be calling it. It simply posts back to the page but
    > doesn't set the text of the label.
    >
    > private void OnViewDetails(Object sender, CommandEventArgs e)
    > {
    > this.lblMessage.Text = "Button has been pressed";
    > }
    >
    > As this is part of a bigger page, I've uploaded the aspx and cs files to
    > http://andrew-banks.co.uk/problem.zip if people need access to the full

    code
    > in order to help.
    >
    > Thanks in advance,
    > Andrew
    >
    >
     
    Kevin Spencer, Mar 3, 2004
    #2
    1. Advertising

  3. Hi Andrew,

    I haven't havethe chance to see the whole code, but I think where your
    problem lie, you are inserting the controls by code, IIRC if you do this you
    will have to recreate them on postback.
    I just took a fast look at your code and I would suggest you to change it,
    instead of using a PlaceHolder and inserting the controls by hand, you
    should use a Repeater and just DataBind, this is clearer by far.

    The code in the aspx page should looks like:
    I wrote the code below directly in OE, so do not take it literally !!

    <asp:repeater datasource="<%# RepeaterDataSource() %>" id="repeater1" >
    <asp:button runat="server" onCommand="ButtonClicked" CommandName =
    "commandname" CommandParameter=<%# DataBinder.Eval( Container.DataItem,
    "ColumnName") %>
    ... other controls as needed
    <br>
    </asp:repeater>

    Then in the codebehind page you will have something like this:
    IList RepeaterDataSource()
    {
    // create and return the DataTable
    }

    All you have to do then is call repeater1.DataBind();


    Hope this help,

    --
    Ignacio Machin,
    ignacio.machin AT dot.state.fl.us
    Florida Department Of Transportation


    "Andrew Banks" <> wrote in message
    news:Hkk1c.7492$...
    > I've got the following code as part of a C# web form but am having

    problems
    > calling a command. I create a dataset and put some data on the screen.

    This
    > works fine. (relevant sample below)
    >
    > foreach (DataRow row in ds.Tables["Orders"].Rows)
    > {
    > detailsbtn = new LinkButton();
    > //Assign a unique ID to the details control
    > detailsbtn.ID = "details" + row["OrderID"].ToString().Replace("-",
    > String.Empty);
    > detailsbtn.Text =
    > DateTime.Parse(row["OrderDate"].ToString()).ToShortDateString()+" - No.
    > "+row["OrderNumber"].ToString()+" from user " +row["UserName"].ToString();
    > detailsbtn.CommandArgument = row["OrderID"].ToString();
    > detailsbtn.Command += new CommandEventHandler(OnViewDetails);
    > // Add the controls
    > phOrders.Controls.Add(detailsbtn);
    > phOrders.Controls.Add(new LiteralControl("<br>"));
    > }
    >
    > I then have the following for the OnViewDetails handler. When I click one

    of
    > the LinkButtons created in the above code, this handler should be called

    but
    > it doesn't seem to be calling it. It simply posts back to the page but
    > doesn't set the text of the label.
    >
    > private void OnViewDetails(Object sender, CommandEventArgs e)
    > {
    > this.lblMessage.Text = "Button has been pressed";
    > }
    >
    > As this is part of a bigger page, I've uploaded the aspx and cs files to
    > http://andrew-banks.co.uk/problem.zip if people need access to the full

    code
    > in order to help.
    >
    > Thanks in advance,
    > Andrew
    >
    >
     
    Ignacio Machin \( .NET/ C# MVP \), Mar 3, 2004
    #3
    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. Lachlan Gemmell
    Replies:
    3
    Views:
    579
  2. Eric
    Replies:
    3
    Views:
    2,768
    =?Utf-8?B?a2FycGFnYW0=?=
    Nov 9, 2004
  3. Replies:
    4
    Views:
    8,139
    ajaimisra
    Aug 4, 2006
  4. Replies:
    1
    Views:
    754
    Damien
    Feb 22, 2007
  5. Replies:
    0
    Views:
    163
Loading...

Share This Page