我正在使用代码隐藏功能动态绑定我的下拉列表,当用户更改下拉列表并提交购买时,selectedvalue始终为空。
我已经尝试了两个ddl.SelectedItem.ToString();和ddl.SelectedValue.ToString();但没有工作。同样对于下面这两个函数的代码,我似乎无法使用void方法而不是需要返回值和参数的函数,无论如何使用没有参数的void方法?任何建议表示赞赏。
感谢。
<%# FormattedSize((string)Eval("Size")) %>
<%# FormattedGetSize((string)Eval("Size")) %>
直列:
<asp:DropDownList ID="DropDownList1" runat="server" OnDataBinding='<%# FormattedSize((string)Eval("Size")) %>'></asp:DropDownList>
<a href='AddToCart.aspx?CategoryId=<%# Eval("CategoryId") %>&&ProductId=<%# Eval("ProductId" ) %>&&Size=<%# FormattedGetSize((string)Eval("Size")) %>' style="border: 0 none white;">
代码背后:
protected string FormattedSize(string size)
{
if (size.Contains("s"))
{
DropDownList ddl = (DropDownList)FormView_Product.Row.Cells[0].FindControl("DropDownList1");
ddl.Items.Add("S");
}
if (size.Contains("m"))
{
DropDownList ddl = (DropDownList)FormView_Product.Row.Cells[0].FindControl("DropDownList1");
ddl.Items.Add("M");
}
if (size.Contains("f"))
{
DropDownList ddl = (DropDownList)FormView_Product.Row.Cells[0].FindControl("DropDownList1");
ddl.Items.Add("Freesize");
}
return null;
}
protected string FormattedGetSize(String Size)
{
DropDownList ddl = (DropDownList)FormView_Product.Row.Cells[0].FindControl("DropDownList1");
string selectedSize = ddl.SelectedItem.ToString();
return selectedSize;
}
答案 0 :(得分:1)
它不起作用的原因是因为......“你做错了”。您期望<a href=..
将根据用户交互进行更改,而是在用户收到页面时生成href
。如果您希望链接根据下拉列表进行更改,则必须具有:
答案 1 :(得分:0)
你在Page_Load方法中做了什么?您是否检查当前请求是否为回发(使用IsPostBack)? 如果是这样,请检查IsPostBack并仅在获取请求时将DropDownList绑定到基础数据源。