473,387 Members | 1,903 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 1264
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.