Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Problem calling event handler in web form (C#)

Reply
Thread Tools

Problem calling event handler in web form (C#)

 
 
Andrew Banks
Guest
Posts: n/a
 
      03-03-2004
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


 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      03-03-2004
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/de...nLifecycle.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
>
>



 
Reply With Quote
 
 
 
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      03-03-2004
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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Event Handler that creates adds another event handler kaczmar2@gmail.com ASP .Net 1 02-22-2007 07:37 AM
problem in changing the actual event handler into our own event ashok.dhananjeyan@gmail.com Javascript 0 10-30-2006 02:32 PM
Web form event handler not firing? jeff29_b@yahoo.com ASP .Net 4 08-04-2006 10:58 AM
How to ref web form controls in client side event handler? Eric ASP .Net 3 11-09-2004 01:22 PM
Calling DHTML function from code behind event handler Lachlan Gemmell ASP .Net 3 01-23-2004 10:26 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57