的CheckBoxList回路不工作 [英] Checkboxlist loop is not working
问题描述
我有一个DropDownList控制和asp.net页面的按钮。将DropDownList是从方法填充。如果我选择除第一项以外的项目,点击按钮后,我失去了DDL所选择的项目,它选择的第一个项目,也是我得到只有在按钮单击事件的第一个项目的价值。如何解决这个问题呢?
< ASP:DropDownList的ID =userDropDown=服务器DataTextField =客户名称DataValueField =客户ID>
< / ASP:DropDownList的>
保护无效的button1_Click(对象发件人,EventArgs的)
{
如果(!page.isPostBack)
{
userDropDown.DataSource = CC.GetCustomers();
userDropDown.DataBind();
}
}
这听起来像你正在你的DropDownList绑定到您的数据源在不断提出要求。相反,将其绑定只有当 Page.IsPostBack
为假像下面; (您可能不需要 ObjectDataSource控件
)
保护无效的Page_Load(对象发件人,EventArgs的)
{
如果(!Page.IsPostBack)
{
//这里绑定你的数据源(类似下图)
userDropDown.DataSource = GetCustomers的();
userDropDown.DataBind();
}
}
当的DataBind()方法被调用就会失去目标和对与firstItem发布的数据将被默认选中。
I have a dropdownlist control and a button in asp.net page. The dropdownlist is populated from a method. If I select any item other than the first item, after clicking the button, I lose the selected item in the DDL and it selects the first item and also I am getting the value of the first item only in the button click event. How can I fix the problem?
<asp:DropDownList ID="userDropDown" runat="server" DataTextField="CustomerName" DataValueField="CustomerId">
</asp:DropDownList>
protected void Button1_Click(object sender, EventArgs e)
{
if(!page.isPostBack)
{
userDropDown.DataSource = CC.GetCustomers();
userDropDown.DataBind();
}
}
It sounds like you are binding your DropdownList to your datasource at ever request. Instead bind it only if Page.IsPostBack
is false like below; (You may not need ObjectDataSource
)
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//bind your datasource here (something like below)
userDropDown.DataSource = GetCustomers();
userDropDown.DataBind();
}
}
As soon as DataBind() method is called it will lose posted data of that object and the FirstItem will be selected by default.
这篇关于的CheckBoxList回路不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!