473,378 Members | 1,360 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,378 software developers and data experts.

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="myImage_Click"/>
and I would put the code into
protected void myImageClick(object 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(object sender, EventArgs e)
{
int typesPerRow = 4;
if (!IsPostBack)
{

PointType[] types = PointSystem.GetPointTypes();
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(FormatCell(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(FormatCell(types[counter]));
}
emailPointTable.Rows.Add(tr);
}
}
private TableCell FormatCell(PointType t)
{
ImageButtton pointPic;
TableCell cell;
LinkButton selectme;

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

pointPic = new ImageButton();
pointPic.Click += new ImageClickEventHandler(this.pointTypeSelected);
pointPic.SkinID = "skinImage";
pointPic.ImageUrl = t.ImageLarge;
pointPic.Height = 128;
pointPic.Width = 128;

selectme = new LinkButton();
selectme.Text = t.Name;
selectme.CommandName = "SelectPointType";

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

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

myImage.OnClick += new ImageClickEventHandler(myImageClick);

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***@newsgroups.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="myImage_Click"/>
and I would put the code into
protected void myImageClick(object 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(object sender, EventArgs e)
{
int typesPerRow = 4;
if (!IsPostBack)
{

PointType[] types = PointSystem.GetPointTypes();
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(FormatCell(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(FormatCell(types[counter]));
}
emailPointTable.Rows.Add(tr);
}
}
private TableCell FormatCell(PointType t)
{
ImageButtton pointPic;
TableCell cell;
LinkButton selectme;

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

pointPic = new ImageButton();
pointPic.Click += new ImageClickEventHandler(this.pointTypeSelected);
pointPic.SkinID = "skinImage";
pointPic.ImageUrl = t.ImageLarge;
pointPic.Height = 128;
pointPic.Width = 128;

selectme = new LinkButton();
selectme.Text = t.Name;
selectme.CommandName = "SelectPointType";

cell.Controls.Add(pointPic);
cell.Controls.Add(new LiteralControl("<br/>"));
cell.Controls.Add(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.IsPostBack), 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.GetPointTypes();
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(FormatCell(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(FormatCell(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.IsPostBack), 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
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...
6
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...
4
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...
0
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...
2
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...
0
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...
6
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...
5
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...
3
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.