473,789 Members | 2,550 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Linking events from controls, when the controls have been created

I've a situation where I'm creating controls programatically . I'm
programatically creating table cells and rows. I've got an array of data,
and I want to format the data on screen in rows of 4. I get my array,
calculate how many complete rows I need. Then for each of the complete rows,
I format a cell for the data in that array element. When I've completed a
row, I add it to my table. If I have a remainder, I do that at the end.

This works very well. However, I want users to be able to select one of the
images I'm showing by clicking on it. Normally, I would do this by
<asp:Image ID="myImage" runat="server" OnClick="myImag e_Click"/>
and I would put the code into
protected void myImageClick(ob ject sender, EventArgs e)
{
Code Here
}

However, because I'm creating the ImageButton controls programatically , I
can't quite get an event wired in. I've created event handlers, but I think
that because each ImageButton class in created in FormatCell, then they are
out of scope in a postback. Does anyone have a way for me to reliably wire
an event handler into a programatically created control?


The code is below

protected void Page_Load(objec t sender, EventArgs e)
{
int typesPerRow = 4;
if (!IsPostBack)
{

PointType[] types = PointSystem.Get PointTypes();
TableRow tr;

//Display the point types, four in a row.
int rows = types.Length / typesPerRow;
int remainder = types.Length % typesPerRow;
for (int i = 0; i < rows; i++)
{
tr = new TableRow();
for (int j = 0; j < typesPerRow; j++)
{
tr.Cells.Add(Fo rmatCell(types[counter]));
}
emailPointTable .Rows.Add(tr);
}

//Do the last (partial) row
if (remainder != 0)
{
tr = new TableRow();
for (int j = 0; j < remainder; j++)
{
tr.Cells.Add(Fo rmatCell(types[counter]));
}
emailPointTable .Rows.Add(tr);
}
}
private TableCell FormatCell(Poin tType t)
{
ImageButtton pointPic;
TableCell cell;
LinkButton selectme;

cell = new TableCell();
cell.SkinID = "skinCell";

pointPic = new ImageButton();
pointPic.Click += new ImageClickEvent Handler(this.po intTypeSelected );
pointPic.SkinID = "skinImage" ;
pointPic.ImageU rl = t.ImageLarge;
pointPic.Height = 128;
pointPic.Width = 128;

selectme = new LinkButton();
selectme.Text = t.Name;
selectme.Comman dName = "SelectPointTyp e";

cell.Controls.A dd(pointPic);
cell.Controls.A dd(new LiteralControl( "<br/>"));
cell.Controls.A dd(selectme);

return cell;
}
Jul 16 '07 #1
3 1465
when you create the control just add the handler

myImage.OnClick += new ImageClickEvent Handler(myImage Click);

to get the postback event, you need to recreate the controls (with the
same id's) in OnInit and assign the handlers
-- bruce (sqlwork.com)

ri***@newsgroup s.nospam wrote:
I've a situation where I'm creating controls programatically . I'm
programatically creating table cells and rows. I've got an array of data,
and I want to format the data on screen in rows of 4. I get my array,
calculate how many complete rows I need. Then for each of the complete rows,
I format a cell for the data in that array element. When I've completed a
row, I add it to my table. If I have a remainder, I do that at the end.

This works very well. However, I want users to be able to select one of the
images I'm showing by clicking on it. Normally, I would do this by
<asp:Image ID="myImage" runat="server" OnClick="myImag e_Click"/>
and I would put the code into
protected void myImageClick(ob ject sender, EventArgs e)
{
Code Here
}

However, because I'm creating the ImageButton controls programatically , I
can't quite get an event wired in. I've created event handlers, but I think
that because each ImageButton class in created in FormatCell, then they are
out of scope in a postback. Does anyone have a way for me to reliably wire
an event handler into a programatically created control?


The code is below

protected void Page_Load(objec t sender, EventArgs e)
{
int typesPerRow = 4;
if (!IsPostBack)
{

PointType[] types = PointSystem.Get PointTypes();
TableRow tr;

//Display the point types, four in a row.
int rows = types.Length / typesPerRow;
int remainder = types.Length % typesPerRow;
for (int i = 0; i < rows; i++)
{
tr = new TableRow();
for (int j = 0; j < typesPerRow; j++)
{
tr.Cells.Add(Fo rmatCell(types[counter]));
}
emailPointTable .Rows.Add(tr);
}

//Do the last (partial) row
if (remainder != 0)
{
tr = new TableRow();
for (int j = 0; j < remainder; j++)
{
tr.Cells.Add(Fo rmatCell(types[counter]));
}
emailPointTable .Rows.Add(tr);
}
}
private TableCell FormatCell(Poin tType t)
{
ImageButtton pointPic;
TableCell cell;
LinkButton selectme;

cell = new TableCell();
cell.SkinID = "skinCell";

pointPic = new ImageButton();
pointPic.Click += new ImageClickEvent Handler(this.po intTypeSelected );
pointPic.SkinID = "skinImage" ;
pointPic.ImageU rl = t.ImageLarge;
pointPic.Height = 128;
pointPic.Width = 128;

selectme = new LinkButton();
selectme.Text = t.Name;
selectme.Comman dName = "SelectPointTyp e";

cell.Controls.A dd(pointPic);
cell.Controls.A dd(new LiteralControl( "<br/>"));
cell.Controls.A dd(selectme);

return cell;
}
Jul 16 '07 #2
Hi rival
However, because I'm creating the ImageButton controls programatically , I
can't quite get an event wired in. I've created event handlers, but I
think
that because each ImageButton class in created in FormatCell, then they
are
out of scope in a postback. Does anyone have a way for me to reliably
wire
an event handler into a programatically created control?
you have to execute your code on every time, postback or not.
that means, place your code outside of if(!this.IsPost Back), then that
should work fine.

--
Gruss, Peter Bucher
Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET
Jul 16 '07 #3
Thanks for this - firstly, Bruce - I'd already got the event linked in as you
described. "you need to recreate the controls (with the
same id's) in OnInit and assign the handlers" was the issue. I didn't want
to do a hundred object declarations in case I need the object! I think you
are driving at the same thing Peter has written below.
"you have to execute your code on every time, postback or not"

I've changed my Page_Load to this:
if (!IsPostBack)
{
emailPointTable .Visible = true;
}
else
{
emailPointTable .Visible = false;
}
PointType[] types = PointSystem.Get PointTypes();
TableRow tr;

//Display the point types, three in a row.
int rows = types.Length / typesPerRow;
int remainder = types.Length % typesPerRow;
int counter = 0;
for (int i = 0; i < rows; i++)
{
tr = new TableRow();
for (int j = 0; j < typesPerRow; j++)
{
tr.Cells.Add(Fo rmatCell(types[counter]));
counter++;
}
emailPointTable .Rows.Add(tr);
}

//Do the last (partial) row
if (remainder != 0)
{
tr = new TableRow();
for (int j = 0; j < remainder; j++)
{
tr.Cells.Add(Fo rmatCell(types[counter]));
counter++;
}
emailPointTable .Rows.Add(tr);
}

So if I'm posting back, I just don't render the table. And it works!
Fantastic help from both Bruce and Peter

Thanks again


"Peter Bucher [MVP]" wrote:
Hi rival
However, because I'm creating the ImageButton controls programatically , I
can't quite get an event wired in. I've created event handlers, but I
think
that because each ImageButton class in created in FormatCell, then they
are
out of scope in a postback. Does anyone have a way for me to reliably
wire
an event handler into a programatically created control?
you have to execute your code on every time, postback or not.
that means, place your code outside of if(!this.IsPost Back), then that
should work fine.

--
Gruss, Peter Bucher
Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET
Jul 16 '07 #4

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

Similar topics

1
2841
by: Natalia DeBow | last post by:
Hi, I am working on a Windows-based client-server application. I am involved in the development of the remote client modules. I am using asynchronous delegates to obtain information from remote server and display this info on the UI. From doing some research, I know that the way my implementation works today is not thread-safe, because essentially my worker thread updates the UI, which is BAD. So, here I am trying to figure out how...
6
4480
by: Mark | last post by:
I have been working for quite some time on this issue which in theory should be quite simple. The problem is that the Cancel and Save events are not fired when their respective buttons are clicked. I have read several posts which say to put your column generating section in the Page_Init section and it will solve the problem....however, it hasn't solved mine. Can somebody please take a look at this and provide any insight if possible?
4
2561
by: blue | last post by:
I have a drop-down list, a radio button list and a submit button. I'm adding these controls to a table and I'm adding the table to a Placeholder. I'm adding it to the Placeholder because I don't know exactly where the table will be located on the page until runtime. Before the form control table is added to the Placeholder, I'm adding a whole bunch of tables to the Placeholder. This is a flowchart program and I have multiple action boxes...
0
1635
by: Pat Sagaser via .NET 247 | last post by:
I'm using a repeater with a dynamic template. I don't know the fields to display (or how many) until runtime. I have everything working except for linking Button events to the repeaters ItemCommand (see below). I've found plenty of examples for doing it using <ItemTemplate> in the aspx file, but I'm stumped when it comes to doing it dynamically at run time. /////////////////////////// .apsx file: <%@ Page language="c#"...
2
4362
by: RAJ | last post by:
In our multi-tier application, we have several ASP.NET user controls which will update the same data source provided by middle tier logic. In this particular scenario we have one user control displaying the contents of the data source, whilst another control updates the datasource via a command buttons implementation of 'Click', an event raised in the 'Handle Postback Events' stage of the control execution life cycle (via the...
0
1449
by: Klaus Jensen | last post by:
Hi! This has been annoying me for a while now, and I can't get it to work It is really driving me nuts! Basicly this simple webapp created to illustrate my problem, renders five buttons, and adds a handler to the click event. When a button is clicked, the background-color is set to blue.
6
2864
by: hlubocky | last post by:
I thought I had a good grasp of the problem related to dynamically creating controls, but it appears that as my application grew in complexity, the problem has resurfaced. As I understand it, in order for a dynamically created control to persist its state, it needs to be created and loaded into the page control hierarchy before the viewstate is loaded. The proposed solution, and that which I have been using, has been to create all of...
5
2724
by: Steve Moreno | last post by:
Hi all, I've got a web form that I've written code to create an array of DropDownList controls on the page depending on how many records are pulled back. The code to create the controls is working fine but now I need to add events to the newly created DropDownList controls. I need to add the SelectedIndexChanged event and I'm having a hard time getting the code to add events to the controls since the names (IDs) are varied to include...
3
2466
by: Jose Fernandez | last post by:
Hello. I would like to know how could i get all the subscriptions that my form has with the events of their controls. For example. I have a form with a textbox, a button and a dropdown. I create a subscription to the Click event of the Button and another to the SelectedIndexChanged del dropDown.
0
9663
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
10404
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
10195
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7525
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
5415
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
5548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.