动态 DropDownList 索引事件不触发
Dynamic DropDownList index event does not trigger
在 ASP.Net 应用程序中,我有一个用户可以购买商品的页面。为此,他们使用两个 DropDownLists:一个用于查找他们的 itemID,第二个用于选择他们想要购买的物品数量,命名为 Amount 和 PurchasedItemIDs。每件物品都有单独的草药/宝石价格。
我想更改一个名为 TotalPrice 的标签,以便在用户更改所选索引时计算药草和宝石的总价。
我做了一个活动:
protected void Price_Changed_Event(object sender, EventArgs e)
{
int HerbPrice = -1;
int GemPrice = -1;
int AmountPurchased = int.Parse(Amount.SelectedValue);
//Code to get the price out of the items. If it's relevant, ask for it.
foreach (shopItem t in Items)
{
if (t.ID == int.Parse(PurchaseItemIDs.SelectedValue))
{
HerbPrice = t.herbCost*AmountPurchased;
GemPrice = t.gemCost * AmountPurchased;
}
}
if (HerbPrice == -1 || GemPrice == -1)
throw new Exception("ItemID not found.");
else
TotalPrice.Text = "Herbs: "+ HerbPrice + ", Gems: " + GemPrice;
}
我编辑了下拉列表:
<asp:DropDownList ID="PurchaseItemIDs" runat="server" Width="120px"
BackColor="#F6F1DB" ForeColor="#7d6754" Font-Names="Andalus" CssClass="ddl"
onselectedindexchanged="Price_Changed_Event"
ontextchanged="Price_Changed_Event">
</asp:DropDownList>
<asp:DropDownList ID="Amount" runat="server" Width="120px" BackColor="#F6F1DB"
ForeColor="#7d6754" Font-Names="Andalus" CssClass="ddl"
onselectedindexchanged="Price_Changed_Event"
ontextchanged="Price_Changed_Event">
</asp:DropDownList>
尽管如此,当我更改要购买的商品数量时,标签的文本与默认值相比并没有发生任何变化。我在事件开始时设置了一个断点 - 它没有被触发。
编辑:
页面加载内容:
if (!Page.IsPostBack)
{
for (int i = 1; i <= 100; i++)
{
ListItem t = new ListItem();
t.Value = "" + i;
t.Text = "" + i;
Amount.Items.Add(t);
}
foreach (int id in from item in getItemsForUser(((User)Session["User"]).Username) select item.ID)
{
ListItem itm = new ListItem();
itm.Text = "" + id;
itm.Value = "" + id;
PurchaseItemIDs.Items.Add(itm);
}
TotalPrice.Text = "Pick an item and an amount to see the price.";
}
//irrelevant stuff.
我做错了什么?
将 AutoPostBack="true" 添加到您的下拉列表控件:
<asp:DropDownList ID="PurchaseItemIDs" AutoPostBack="true" runat="server" Width="120px"
BackColor="#F6F1DB" ForeColor="#7d6754" Font-Names="Andalus" CssClass="ddl"
onselectedindexchanged="Price_Changed_Event"
ontextchanged="Price_Changed_Event">
</asp:DropDownList>
<asp:DropDownList ID="Amount" AutoPostBack="true" runat="server" Width="120px" BackColor="#F6F1DB"
ForeColor="#7d6754" Font-Names="Andalus" CssClass="ddl"
onselectedindexchanged="Price_Changed_Event"
ontextchanged="Price_Changed_Event">
</asp:DropDownList>
在 ASP.Net 应用程序中,我有一个用户可以购买商品的页面。为此,他们使用两个 DropDownLists:一个用于查找他们的 itemID,第二个用于选择他们想要购买的物品数量,命名为 Amount 和 PurchasedItemIDs。每件物品都有单独的草药/宝石价格。
我想更改一个名为 TotalPrice 的标签,以便在用户更改所选索引时计算药草和宝石的总价。
我做了一个活动:
protected void Price_Changed_Event(object sender, EventArgs e)
{
int HerbPrice = -1;
int GemPrice = -1;
int AmountPurchased = int.Parse(Amount.SelectedValue);
//Code to get the price out of the items. If it's relevant, ask for it.
foreach (shopItem t in Items)
{
if (t.ID == int.Parse(PurchaseItemIDs.SelectedValue))
{
HerbPrice = t.herbCost*AmountPurchased;
GemPrice = t.gemCost * AmountPurchased;
}
}
if (HerbPrice == -1 || GemPrice == -1)
throw new Exception("ItemID not found.");
else
TotalPrice.Text = "Herbs: "+ HerbPrice + ", Gems: " + GemPrice;
}
我编辑了下拉列表:
<asp:DropDownList ID="PurchaseItemIDs" runat="server" Width="120px"
BackColor="#F6F1DB" ForeColor="#7d6754" Font-Names="Andalus" CssClass="ddl"
onselectedindexchanged="Price_Changed_Event"
ontextchanged="Price_Changed_Event">
</asp:DropDownList>
<asp:DropDownList ID="Amount" runat="server" Width="120px" BackColor="#F6F1DB"
ForeColor="#7d6754" Font-Names="Andalus" CssClass="ddl"
onselectedindexchanged="Price_Changed_Event"
ontextchanged="Price_Changed_Event">
</asp:DropDownList>
尽管如此,当我更改要购买的商品数量时,标签的文本与默认值相比并没有发生任何变化。我在事件开始时设置了一个断点 - 它没有被触发。
编辑: 页面加载内容:
if (!Page.IsPostBack)
{
for (int i = 1; i <= 100; i++)
{
ListItem t = new ListItem();
t.Value = "" + i;
t.Text = "" + i;
Amount.Items.Add(t);
}
foreach (int id in from item in getItemsForUser(((User)Session["User"]).Username) select item.ID)
{
ListItem itm = new ListItem();
itm.Text = "" + id;
itm.Value = "" + id;
PurchaseItemIDs.Items.Add(itm);
}
TotalPrice.Text = "Pick an item and an amount to see the price.";
}
//irrelevant stuff.
我做错了什么?
将 AutoPostBack="true" 添加到您的下拉列表控件:
<asp:DropDownList ID="PurchaseItemIDs" AutoPostBack="true" runat="server" Width="120px"
BackColor="#F6F1DB" ForeColor="#7d6754" Font-Names="Andalus" CssClass="ddl"
onselectedindexchanged="Price_Changed_Event"
ontextchanged="Price_Changed_Event">
</asp:DropDownList>
<asp:DropDownList ID="Amount" AutoPostBack="true" runat="server" Width="120px" BackColor="#F6F1DB"
ForeColor="#7d6754" Font-Names="Andalus" CssClass="ddl"
onselectedindexchanged="Price_Changed_Event"
ontextchanged="Price_Changed_Event">
</asp:DropDownList>