473,725 Members | 2,251 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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().Rep lace("-",
String.Empty);
detailsbtn.Text =
DateTime.Parse( row["OrderDate"].ToString()).To ShortDateString ()+" - No.
"+row["OrderNumbe r"].ToString()+" from user " +row["UserName"].ToString();
detailsbtn.Comm andArgument = row["OrderID"].ToString();
detailsbtn.Comm and += new CommandEventHan dler(OnViewDeta ils);
// Add the controls
phOrders.Contro ls.Add(detailsb tn);
phOrders.Contro ls.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(O bject sender, CommandEventArg s 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 15 '05 #1
2 1681
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="<%# RepeaterDataSou rce() %>" id="repeater1" >
<asp:button runat="server" onCommand="Butt onClicked" CommandName =
"commandnam e" CommandParamete r=<%# DataBinder.Eval ( Container.DataI tem,
"ColumnName ") %>
... other controls as needed
<br>
</asp:repeater>

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

All you have to do then is call repeater1.DataB ind();
Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Andrew Banks" <ba****@nojunkb lueyonder.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().Rep lace("-",
String.Empty);
detailsbtn.Text =
DateTime.Parse( row["OrderDate"].ToString()).To ShortDateString ()+" - No.
"+row["OrderNumbe r"].ToString()+" from user " +row["UserName"].ToString();
detailsbtn.Comm andArgument = row["OrderID"].ToString();
detailsbtn.Comm and += new CommandEventHan dler(OnViewDeta ils);
// Add the controls
phOrders.Contro ls.Add(detailsb tn);
phOrders.Contro ls.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(O bject sender, CommandEventArg s 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 15 '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****@nojunkb lueyonder.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().Rep lace("-",
String.Empty);
detailsbtn.Text =
DateTime.Parse( row["OrderDate"].ToString()).To ShortDateString ()+" - No.
"+row["OrderNumbe r"].ToString()+" from user " +row["UserName"].ToString();
detailsbtn.Comm andArgument = row["OrderID"].ToString();
detailsbtn.Comm and += new CommandEventHan dler(OnViewDeta ils);
// Add the controls
phOrders.Contro ls.Add(detailsb tn);
phOrders.Contro ls.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(O bject sender, CommandEventArg s 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 15 '05 #3

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

Similar topics

33
2858
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" onclick="show(this)">111111</li>
2
2381
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 Error_Handler("<routine name>", Err) = acDataErrDisplay Then On Error GoTo 0 Stop: Resume ' Press twice to view the problem.
3
1969
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 Method I have code that checks to see if a DB table has been updated.. if so the new new data is uploaded to the listbox by calling lstbox1.datasource = myDBDataSet;
5
3041
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 is hooked into a column ORGANISATION.SHORT_NAME) and a Button which I am using as a submit button.
2
1277
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) foreach (DataRow row in ds.Tables.Rows) { detailsbtn = new LinkButton(); //Assign a unique ID to the details control detailsbtn.ID = "details" + row.ToString().Replace("-", String.Empty);
7
2646
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 my button service in the dialog form, I have: Private Sub btnSave_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnSave.Click
14
3264
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 instance of the object. WHY? Also if I can figure this out I'm hoping that I can put this in the load event of the parent and still get it to work.
6
2011
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 understanding that application.run() creates a message loop for the form and passes all messages to it. If showing the form is optional, and I want to to display 2 forms, which form will application.run() pass the windows messages to?
14
3365
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 I'll make the inactive forms invisible but this is creating a memory corruption problem when user close the form2 or form3 and not the formMain. My main form has a Next button which makes the main form invisible and starts a new form which I'll...
0
8889
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9116
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8099
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3228
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.