C# .NET - Create ASP-Image-buttons at runtime - Asked By Gustav Wiberg on 11-Feb-08 08:36 AM

Hi!

c#.Net is growing as a good language! :-)


But I have a problem...

(I'm doing the following thing)

1. Loop through a database table.


2. Create ASP-buttons.

The ASP-Imagebuttons are created something like this where IDCampaign is the Identity in the table.

<asp:imagebutton id=\"arrowImage" + IDCampaign.ToString() + "\" runat=\"server\" ImageUrl=\"images/arrow_campaign.gif\"></asp:imagebutton>


3. Loop End

The images doesn't appear on the page (i know that arrow_campaign.gif exists in subfolder images). I can't see how I can add an attribute to a dynamically created asp-image-button? IS it possible???

I'm developing in ASP 1.4.. (because of some backward-compability-stuff)

 

You are trying to use codebehind - style - Peter Bromberg replied to Gustav Wiberg on 11-Feb-08 09:13 AM

programming constructs in the markup of the page, which you cannot do.

You probably need to build your imagebuttons dynamically via databinding,

not "looping through a DataTable".

IS Code-behind - Gustav Wiberg replied to Peter Bromberg on 11-Feb-08 09:20 AM

Hi again!

This IS codebehind-style C#....


while(dataReader.Read())

{

IDCampaign = dataReader.GetInt32(0);

campaignIngress = dataReader.GetValue(2).ToString();

startDate = dataReader.GetValue(4).ToString();

//Declare arrowImage as an Imagebutton

//

System.Web.UI.WebControls.ImageButton[] arrowImage;

info = "<div id=\"info_campaign_leftcolumn_content\"><span class=\"redText10\">" + startDate + "</span><br>";

info = info + "<span class=\"blackText10\">" + campaignIngress;

info = info + "<div id=\"arrow_campaign_leftcolumn_content\"><asp:imagebutton id=\"arrowImage\" runat=\"server\" ImageUrl=\"images/arrow_campaign.gif\"></asp:imagebutton></div></span>";

info = info + "</div>";

Response.Write(info);

}


Can I do something like above? (What am I missing). Do you have any example how to achieve what I'm trying to do. (or some good place to look)


Do you present data from DataTable in grid? - Nenad Prekupec replied to Gustav Wiberg on 11-Feb-08 09:37 AM

If so, there is event, if I recall correctly, DataBoundItem that is used for creating/formating things on row level.

Producing code like you want:

<asp:imagebutton id=\"arrowImage\" runat=\"server\" ImageUrl=\"images/arrow_campaign.gif\"></asp:imagebutton>

as you are preparing HTML code you can't use "asp:imagebutton" tag, but can create plain HTML button (<input type="button"...>)and bind JS to onclickevent and even PostBack on it.
To do it as server control you have to have container for your controls to place them in it and use regular C# code:
ImageButton button = new ImageButton();
and placing it into that page, ImageButton container.

I hope that's clear.. bottom line is if you are creating controls as HTML directly to Response stream they have to be still HTML controls otherwise you won't see anything.

But you can
Thanks! - Gustav Wiberg replied to Nenad Prekupec on 11-Feb-08 09:41 AM

Hi!

AHA! Yes, that certainly cleared it out. Thank you very much!

No you cannot do this. - Peter Bromberg replied to Gustav Wiberg on 11-Feb-08 09:47 AM

here is what you can do:

System.Web.UI.WebControls.ImageButton imageButton;

imageButton.ID = "ArrowImage"+IDCampaign.ToString();

imageButton.ImageUrl = http://mywebsite/images/myarrowimage.gif;

...etc

Placeholder1.Controls.Add(imageButton);



Another thing - Gustav Wiberg replied to Nenad Prekupec on 11-Feb-08 10:02 AM

"as you are preparing HTML code you can't use "asp:imagebutton" tag, but can create plain HTML button (<input type="button"...>) and bind JS to onclickevent and even PostBack on it."

Can you explain to me HOW I bind a onClickEvent to the button?
(I can type an onclick-statement for the button, but how do I change the textvalue of a server-control label for example?)

Well there are some constrictions in the way you have choosed. - Nenad Prekupec replied to Gustav Wiberg on 11-Feb-08 11:10 AM
The way you want to create button, as I said, you can't create server side control but just plain html button and you can bind only JS events. So constriction on this is that you can't bind directly to post-back event.

There some tricks you can use with constructing JS code to "onclick" attribute of HTML button to behave as post-back event, set certain value to it (I'm not 100% sure, if you can link value to the button, if that is possible, didn't worked like that for a while) and process post-back with values being set with each button click.

To create server-side control you'll have to go through dynamically creating ImageButton in code-behind as explained before and adding to control container.
Thanks again - Gustav Wiberg replied to Nenad Prekupec on 12-Feb-08 05:34 AM
for clearing things up!