Problem calling event handler in web form (C#)

A

Andrew Banks

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
 
K

Kevin Spencer

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.
 
I

Ignacio Machin \( .NET/ C# MVP \)

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,
 

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