Reputation: 14417
I have looked at other post but nothing explained what I want to do very well.
At runtime I have x amount of LinkButtons added to the form by a repeater dependant on the data it retrieves from the database:
<asp:Repeater ID="variantRepeat" runat="server"
onitemcommand="variantRepeat_ItemCommand">
<HeaderTemplate><ul></HeaderTemplate>
<ItemTemplate><li><asp:LinkButton ID="varLink" runat="server" CommandName="Click" CommandArgument='<%# Eval("variantID") %>'><%# Eval ("variant_name") %></asp:LinkButton></li></ItemTemplate>
<FooterTemplate></ul></FooterTemplate>
</asp:Repeater>
It is supposed to, when clicked Fire of an event backstage
protected void varLink_Click(object sender, EventArgs e)
{
ViewCollection views = prodView.Views;
}
Then set up some views. However I need the CommandArgument to go through as that holds the key to look up inside the prodView.Views
.
It doesn't have to be variantID
that is passed but could be and Int depending on which one, just need some indication as to what view to display!
I have all the views created at page init and added to the page dynamically at init.
I use the entity framework to query the database through a few views! (found that to be easier to get complex data from relational databases!)
I don't know how to link up all those LinkButtons so that I can programmatically switch views?
Upvotes: 0
Views: 864
Reputation: 31077
Here's more detailed code:
ASPX:
<asp:TextBox runat="server" ID="txtVariant" />
<asp:Repeater ID="variantRepeat" runat="server"
OnItemCommand="variantRepeat_ItemCommand">
<HeaderTemplate><ul></HeaderTemplate>
<ItemTemplate>
<li>
<asp:LinkButton ID="varLink" runat="server" CommandName="Click"
CommandArgument='<%# Eval("variantID") %>'>
<%# Eval ("variant_name") %></asp:LinkButton>
</li>
</ItemTemplate>
<FooterTemplate></ul></FooterTemplate>
</asp:Repeater>
Code behind:
[Serializable]
public class Variant
{
public Variant() { }
public int variantID { get; set; }
public string variant_name { get; set; }
}
public partial class _Default : System.Web.UI.Page
{
public Variant[] Variants
{
get
{
if (ViewState["Variants"] == null)
return new Variant[] { };
return (Variant[])ViewState["Variants"];
}
set { ViewState["Variants"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Variants = new Variant[] {
new Variant() { variantID = 1, variant_name = "T1" },
new Variant() { variantID = 2, variant_name = "T2" }
};
variantRepeat.DataSource = Variants;
variantRepeat.DataBind();
}
}
protected void variantRepeat_ItemCommand(object source, RepeaterCommandEventArgs e)
{
switch (e.CommandName)
{
case "Click":
var variant = Variants.FirstOrDefault(v => v.variantID.ToString() == e.CommandArgument.ToString());
if (variant != null)
{
txtVariant.Text = variant.variantID.ToString();
// show the right view
}
break;
}
}
}
The code is using the ViewState as the storing container, but you can also use Session.
Upvotes: 1