My question is this: what asp.net control do I have to use to generate a list of buttons with the text from database and with the action a link passing the ID of the object listed (id also from database)??
2 Answers
What solved my problem was this incredible simple noobish solution:
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand"
DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:Button Style="margin-top: 20px;" ID="Button1" Font-Size="X-Large" BackColor="Blue" ForeColor="White" runat="server" Text='<%# Eval("Descrizione") %>'
PostBackUrl='<%# "~/Assemblaggio3.aspx?idCompCrit="+Eval("ID") %>' /><br />
Comments
a datarepeater with a bunch of buttons with unique commandArgument and a common command Name should do it.
from the top of my head :
<asp:Repeater runat="server">
<ItemTemplate>
<asp:Button id="<%# DataBinder.Eval(Container.DataItem, "someuniquecounter") %>"
Text="<%# DataBinder.Eval(Container.DataItem, "columnContainingTextToDisplayToUser") %>"
CommandName="CommonCommandName"
CommandArgument="<%# DataBinder.Eval(Container.DataItem, "IdToPassToTheCommandEventHandler%>")"
OnCommand="CommandBtn_Click"
runat="server"/>
</ItemTemplate>
<SeparatorTemplate>
<br>
</SeparatorTemplate>
</asp:Repeater>
in your command handler, you could first test if the command name is correct (you could have many depending on the action you want to accomplish), then get the command argument (the id) to pass to the correct method.
void CommandBtn_Click(Object sender, CommandEventArgs e)
{
if((string.Compare(e.CommandName, "yourCommandName", false)==0)
{
YourMethodAcceptingTheIdAsParameter((int)e.CommandArgument);
}
}
1 Comment
velteyn
it gives me "id is not a valid identifier "