我已经在这方面工作了一段时间了。我只是想访问数据列表中的内容。我可以从 PageLoad 中很好地获取项目,甚至设置一些动态项目.. 但我无法从我的按钮单击处理程序访问控件。我尝试过其他变体,例如
ListView1.FindControl("DropDownList1") as DropDownList;
这给出了一个NULL。我试过了
<asp:LinkButton id="addPro" runat="server" CommandArgument='<%# DropDownList1.SelectedItem.Text %>' onCommand ="addPro_Click">Add To Cart</asp:LinkButton>
这表示它无法在范围内找到数据控制。
<asp:ListView ID="ListView1" runat="server"
DataKeyNames="Expr7,Expr1,productNo" DataSourceID="SqlDataSource1">
<AlternatingItemTemplate>
<span style="">
<asp:Label ID="productNameLabel" runat="server"
Text='<%# Eval("productName") %>' />
<br />
<asp:Image runat="server" height = "300" ImageUrl='<%# Eval("img") %>'></asp:Image>
<br />
Description:<br />
<asp:Label ID="itemNotesLabel" runat="server" Text='<%# Eval("itemNotes") %>' />
<br />
stock:
<asp:Label ID="stockLabel" runat="server" Text='<%# Eval("stock") %>' />
<br />
price:
<asp:Label ID="priceLabel" runat="server" Text='<%# "$"+ Eval("price")+".00" %>' />
<br />
Quantitiy: <asp:DropDownList id="DropDownList1" runat="server"> </asp:DropDownList>
<br />
<asp:LinkButton id="addPro" runat="server" CommandArgument='<%# Eval("productNo") %>' onCommand ="addPro_Click">Add To Cart</asp:LinkButton>
<br /><br /><br />
<br /></span>
</AlternatingItemTemplate>
点击时
protected void addPro_Click(Object sender, CommandEventArgs e)
{
string addr = "";
string v = Request.QueryString["cat"];
if (v != null)
{
v = "cat=" + v+"&";
}
else
{
v = "";
}
DropDownList stockDD = (DropDownList) FindControl("DropDownList1");
if (stockDD != null)
addr = "~/product.aspx/?" + v + "add=" + e.CommandArgument.ToString() + "&quant=" + stockDD.SelectedItem.Text;
else
addr = "ERROR!";
Response.Redirect(addr+e.CommandArgument.ToString());
ListView1.ItemDataBound += (sa, ea) =>
{
DropDownList stockD = ea.Item.FindControl("DropDownList1") as DropDownList;
Label l = ea.Item.FindControl("Lable12") as Label;
l.Text = stockD.SelectedItem.Text;
addr = "~/product.aspx/?" + v + "add=" + e.CommandArgument.ToString() + "&quant=" + stockD.SelectedItem;
};
// Response.Redirect(addr);
}
任何朝着正确方向的帮助都会大有帮助!谢谢!