Hej!
Har problem att trigga OnSelectedIndexChanged i en DropDownList i en repeater. Får inga fel utan allt som sker är en postback utan att något händer.
<asp:Repeater ID="rptCart" runat="server" OnItemCommand="rptCart_ItemCommand" OnItemDataBound="rptCart_ItemDataBound">
<ItemTemplate>
<%# Eval("ProductName") %>
<%# Eval("ManufacturerName") %>
<asp:Repeater ID="rptAttributes" runat="server">
<ItemTemplate>
<%# Eval("AttributeValue") %>
</ItemTemplate>
<SeparatorTemplate>
/
</SeparatorTemplate>
</asp:Repeater>
[B]<asp:DropDownList ID="ddlQty" runat="server" OnSelectedIndexChanged="ddlQty_SelectedIndexChanged" EnableViewState="true" AutoPostBack="true" />[/B]
<%# Eval("ProductPrice", "{0:c}") %>
<%# Eval("Subtotal", "{0:c}")%>
<asp:Literal ID="ltrId" runat="server" Text='<%# Eval("ProductID") %>' Visible="false" />
<asp:Button ID="btnRemove" Text="Ta bort" runat="server" />
</ItemTemplate>
</asp:Repeater>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class ShoppingCart : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
PopulateControls();
}
}
private void PopulateControls()
{
DataTable dt = ShoppingCartAccess.GetItems();
if (dt.Rows.Count == 0)
{
lblStatus.Text = "Kundvagnen är tom.";
rptCart.Visible = false;
}
else
{
rptCart.DataSource = dt;
rptCart.DataBind();
}
}
protected void rptCart_ItemCommand(object source, RepeaterCommandEventArgs e)
{
string productId = ((Literal)e.Item.FindControl("ltrId")).Text.ToString();
bool success = ShoppingCartAccess.RemoveItem(productId);
PopulateControls();
}
protected void rptCart_ItemDataBound(object source, RepeaterItemEventArgs e)
{
if (!Page.IsPostBack)
{
//------------------------------------------------------------------------------------------------
//Populate repeater with attributes
if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
{
return;
}
Repeater rptAttributes = (Repeater)e.Item.FindControl("rptAttributes");
Literal ltrId = (Literal)e.Item.FindControl("ltrId");
string productId = ltrId.Text;
rptAttributes.DataSource = ShoppingCartAccess.GetProductAttributes(productId);
rptAttributes.DataBind();
//------------------------------------------------------------------------------------------------
//Populate ddl with stock quantity
int stockQty = Int32.Parse(DataBinder.Eval(e.Item.DataItem, "ProductStock").ToString());
int cartQty = Int32.Parse(DataBinder.Eval(e.Item.DataItem, "Quantity").ToString());
DropDownList ddl = (DropDownList)e.Item.FindControl("ddlQty");
for (int i = 0; i < stockQty; i++)
{
ddl.Items.Insert(i, (i + 1).ToString());
ddl.Items[i].Value = productId;
if (i == cartQty)
{
ddl.SelectedIndex = i - 1;
}
}
//------------------------------------------------------------------------------------------------
}
}
[B]public void ddlQty_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect("default.aspx");
/*
int qty = Int32.Parse(((DropDownList)sender).SelectedItem.Text.ToString());
string productId = ((DropDownList)sender).SelectedItem.Value.ToString();
bool success = ShoppingCartAccess.UpdateItem(productId, qty);
*/
}[/B]
}
Någon som vet vad jag gör för fel?