[英]OnSelectedIndexChanged don't seem to work
我在使代碼執行我想做的事情時遇到了一些麻煩,感謝您的幫助。
我想做的是:
在文本框中,我為產品添加了一個名稱,並使用該名稱創建了一個對象。
然后將產品對象添加到字典中。
然后,我想將此字典綁定到一個下拉列表。
如果更改所選項目,則要顯示所選產品的編號(創建產品對象時默認為0)。
問題是,當我嘗試更改下拉列表中的項目時,什么也沒有發生。
謝謝!
.aspx
<asp:TextBox ID="productText" runat="server"></asp:TextBox>
<asp:Button ID="newProductButton" runat="server" OnClick="newProduct_Click" />
<div>
<asp:DropDownList ID="ddlProducts" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlProducts_SelectedIndexChanged" >
</asp:DropDownList>
</div>
<asp:Label ID="productQuantity" runat="server"></asp:Label>
.aspx.cs
public partial class Pages_productPage : System.Web.UI.Page
{
string _productName = string.Empty;
public Dictionary<string, int> product
{
get
{
if (Page.Session["product"] == null)
{
Dictionary<string, int> product = new Dictionary<string, int>();
Page.Session["product"] = product;
}
return (Dictionary<string, int>)Page.Session["product"];
}
set
{
Page.Session["product"] = value;
}
}
protected string ProductName
{
get { return _productName; }
set { _productName = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//ddlProducts.Items.Insert(0, new ListItem("Products", "0"));
}
productLabel.Text = "Product name";
newProductButton.Text = "Add product";
ProductName = productText.Text;
}
public void newProduct_Click(object sender, EventArgs e)
{
Product prod = new Product(ProductName);
product.Add(prod.GetName(prod), prod.GetQuantity(prod));
BindDictionary();
}
private void BindDictionary()
{
dictonaryRepeater.DataSource = product;
dictonaryRepeater.DataBind();
ddlProducts.DataSource = product;
ddlProducts.DataValueField = "Value";
ddlProducts.DataTextField = "Key";
ddlProducts.DataBind();
//ddlProducts.Items.Insert(0, new ListItem("Products", "0"));
}
public void ddlProducts_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlProducts.DataValueField == "Banana")
{
productQuantity.Text = ddlProducts.SelectedItem.ToString();
productQuantity.Visible = true;
}
}
}
您的DataValueField永遠不等於香蕉。 它等於“值”。 您使用了錯誤的選擇器。 您要使用ddlProducts.SelectedValue。 當我再次查看它時,您似乎真的想使用文本時就在使用價值。
代碼建議:
public void ddlProducts_SelectedIndexChanged(object sender, EventArgs e)
{
if ("Banana".Equals(ddlProducts.SelectedItem.ToString(), StringComparison.OrdinalIgnoreCase)
{
productQuantity.Text = ddlProducts.SelectedValue.ToString();
productQuantity.Visible = true;
}
}
**從我從Skeet先生那里學到的東西添加了等值點。
編輯:
由於您說事件從未被觸發,因此autoeventwireup可能在第二次回發時失敗。 我不確定為什么會發生這種情況,但是我看到如果在xml中定義事件,則事件將失敗。 您可以嘗試在頁面加載事件中將事件聯結從xml端移到后面的cod。
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//ddlProducts.Items.Insert(0, new ListItem("Products", "0"));
}
productLabel.Text = "Product name";
newProductButton.Text = "Add product";
ProductName = productText.Text;
ddlProducts.SelectedIndexChanged += ddlProducts_SelectedIndexChanged;
}
ddlProducts.DataValueField將永遠不會是香蕉,因為您在BindDictionary方法中將其設置為“ Value”。
頁面聲明中是否設置了AutoEventWireup?
參見此處: msdn AutoEventWireUp
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.