简体   繁体   English

DropdownList未显示所选值

[英]DropdownList is not showing the selected value

I have used DropDownList in Repeater. 我在Repeater中使用了DropDownList。 It fetches Designation (Admin,Manager,Member) from Database. 它从数据库中获取名称(管理员,经理,成员)。 User Name is Displayed correctly, even alert is showing the correct data. 用户名显示正确,即使警报显示正确的数据。 but dropdownlist.selecteditem has no effect. 但dropdownlist.selecteditem无效。 Both users shows admin in dropdown as default selected value, in db its manager. 两个用户都显示admin在下拉列表中作为默认选择值,在db其管理器中。

protected void ListRepeaterView_ItemDataBound(object sender, RepeaterItemEventArgs e)
{



    DropDownList selectList = e.Item.FindControl("DropDownList1") as DropDownList;
    selectList.DataSource = ML.SelectAll();
    selectList.DataTextField = "Designation";
    selectList.DataValueField = "EmployeeID";

    selectList.DataBind();


    HiddenField Designation = (HiddenField)e.Item.FindControl("hdnDesignation");
   selectList.SelectedValue = Designation.Value.ToString();
   ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert(" + Designation.Value.ToString() + ");", true);




}

.cs file .cs文件

    <asp:Repeater ID="ListRepeaterView" runat="server" OnItemDataBound="ListRepeaterView_ItemDataBound">
<ItemTemplate><tr><td>
<asp:Label ID="EmpName" runat="server" Text='<%# Eval("EmployeeName") %>'></asp:Label>
 asp:HiddenField ID="hdnProductID" Value='<%# Eval("EmployeeID") %>' runat="server" />
</td><td>
<asp:HiddenField ID="hdnDesignation" Value='<%# Eval("Designation") %>' runat="server" />
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true" >
 </asp:DropDownList>
</td></tr>
<br /><br /><br />
</ItemTemplate>
</asp:Repeater>

You are using "Designation" as text field and "EmployeeID" as value field in select. 您在“选择”中将“指定”用作文本字段,将“EmployeeID”用作值字段。 And then you try to set selected value based on "Designation". 然后尝试根据“指定”设置所选值。 This cannot really work. 这真的不行。

What you can do is to locate default item by text: 你可以做的是按文字定位默认项目:

// add some error handling for cases when item cannot be found
var defaultText = Designation.Value.ToString();
selectList.Items.FindByText(defaultText).Selected = true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM