We help IT Professionals succeed at work.

c# .net arrays - Images and ImageButtons

EffinGood
EffinGood used Ask the Experts™
on
Hi,

I've looked everywhere but not found an answer to what should be a simple question.  How can I set up Images and ImageButtons to be dealt with as arrays?  Most "answers" to this that I've seen link to this page:

http://msdn.microsoft.com/en-us/library/aa289500%28v=vs.71%29.aspx

Which is a much more complicated solution than I need or can handle.

Here's an example.  I want to build a page that has 64 ImageButtons.  The ImageURLs for these should be set in some sort of for-loop in the code-behind, and there should be one single OnClick event that can tell which button triggered it.

Psuedo-code of what I would like to see happen:

.aspx code:

<asp:Button ID="Button1" runat="server" OnClick="Button_Click" />
<asp:Button ID="Button2" runat="server" OnClick="Button_Click" />
<asp:Button ID="Button3" runat="server" OnClick="Button_Click" />...

Code-behind:

protected void Page_Load(object sender, EventArgs e)
{
for (i=0; i<64; i++)
{
Buttoni.ImageURL=i+".png";
}
}

protected void Button_Click(object sender, EventArgs e)
{
// somehow tell which button triggered the event, and switch based on that value
}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You can use Repeater/DataList/GridView controls to generate controls at runtime.

When the button is inside a Repeater template, you don't want to use an OnClick event, but the ItemCommand event of the Repeater control.

Your .aspx code will look something like this:

<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<div class="box">
<asp:Button ID="buy" runat="server" Text="BUY" UseSubmitBehavior="False" CommandName="buy" />
</div>
</ItemTemplate>
</asp:Repeater>

If you have event validation turned on (it is on by default), you probably want to set UseSubmitBehavior to false so you don't get an exception.

In your code-behind you can handle the ItemCommand event using a switch statement:

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
switch (e.CommandName)
{
case "buy":
// Do some stuff when the BUY button is clicked.
break;

// Other commands here.

default:
break;
}
}

Finally, you must bind your Repeater to a data source with data if you want to see your button. For initial testing, you can just bind to a string array with a single element:

protected void Page_Load(object sender, EventArgs e)
{
Repeater1.DataSource = new string[] { string.Empty };
Repeater1.DataBind();
}

Reference from : http://community.spiceworks.com/topic/100278-how-to-fire-button-click-event-in-repeater-control-in-asp-net


You can change the CommandName or CommandArgument using below code:
<asp:Button ID="buy" runat="server" Text="BUY" UseSubmitBehavior="False" CommandName="buy" CommandArgument='<%# DataBinder.Eval (Container.DataItem, "Price") %>' />
Also to generate the buttons you need to bind the dataSource to the control.

DataTable dt = new DataTable("dt");
DataColumn dc1 = new DataColumn("dc1");
dt.Columns.Add(dc1);

for (i=0; i<64; i++)
{
    DataRow drNew = dt.NewRow();
    drNew["dc1"] = i.toString();
    dt.Rows.Add(drNew);
}


Repeater1.DataSource = dt;
Repeater1.DataBind();