472,805 Members | 959 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

Problem calling event handler in web form (C#)

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
Nov 18 '05 #1
2 1249
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" <ba****@nojunkblueyonder.co.uk> wrote in message
news:Hk**********************@news-text.cableinet.net...
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

Nov 18 '05 #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/de...nLifecycle.asp

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

"Andrew Banks" <ba****@nojunkblueyonder.co.uk> wrote in message
news:Hk**********************@news-text.cableinet.net...
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

Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: abs | last post by:
Hi all. My list: <ul> <li id="a" onclick="show(this)">Aaaaaaaa</li> <li id="b" onclick="show(this)">Bbbbbbbb</li> <li id="c" onclick="show(this)">Cccccccc <ul> <li id="d"...
2
by: Steve Richfield | last post by:
My error handler works GREAT. However, VBA seems to have some bugs/features that are causing it fits. The little snippet that I put at the end of each routine looks like this: Error_Handler: If...
2
by: Andrew Banks | last post by:
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) ...
3
by: jayderk | last post by:
Hello All, I am running in to a situation where the listbox is not refreshing for me. I am using a timer to cycle every second and call the timer_elapsed() event. in the time_elapsed event...
5
by: Alastair Anderson | last post by:
I have created a very simple form with which I would like to update a single value in a single row of a database as a proof of concept. The relevant parts of the form are a DBWebTextBox (which...
7
by: Frank Maxey | last post by:
I am fairly new to VB.Net and am having a curious problem. I have an entry dialog form called from a main form. The calling form needs to check the DialogResult field for an OK response. In...
14
by: Altman | last post by:
Ok I have a control that is inherited from another class. In the child I put msgbox(me.name) in the load event. What always pops up is the name of the parent class and not the name of the...
6
by: Joel | last post by:
2 Questions: (1) The documentation says application.run() creates a standard message loop on the current thread and "optionally" shows a form. This is really confusing because I was of the...
6
by: Murray Hopkins | last post by:
Hi. THE QUESTION: How do I get a reference to my Object when processing an event handler bound to an html element ? CONTEXT: Sorry if it is a bit long. I am developing a JS calendar tool....
14
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using VS2005 and .net 2.0. I'm creating an application that has 3 forms. I want allow users to move forward and backward with the forms and retain the data users have entered. I thought...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.