DROPDOWNLIST没有显示选择的值 [英] DropdownList is not showing the selected value

查看:287
本文介绍了DROPDOWNLIST没有显示选择的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在直放站使用DropDownList中。它从数据库中获取标识(管理员,经理,会员)。
用户名是正确显示,即使警报显示正确的数据。
但是dropdownlist.selecteditem没有效果。
两个用户显示在管理员的下拉列表中选择默认值,分贝其经理。

 保护无效ListRepeaterView_ItemDataBound(对象发件人,RepeaterItemEventArgs E)
{    DropDownList的选择列表= e.Item.FindControl(DropDownList1)作为DropDownList的;
    selectList.DataSource = ML.SelectAll();
    selectList.DataTextField =称号;
    selectList.DataValueField =雇员;    selectList.DataBind();
    HiddenField指定=(HiddenField)e.Item.FindControl(hdnDesignation);
   selectList.SelectedValue = Designation.Value.ToString();
   ScriptManager.RegisterClientScriptBlock(此,this.GetType(),alertMessage,警报(+ Designation.Value.ToString()+);,真);
}

cs文件

 < ASP:直放站ID =ListRepeaterView=服务器OnItemDataBound =ListRepeaterView_ItemDataBound>
&所述;的ItemTemplate>&下; TR>&下; TD>
< ASP:标签ID =EmpName=服务器文本='<%#的eval(EmployeeName)%>'>< / ASP:标签>
 ASP:HiddenField ID =hdnProductID值='<%#的eval(雇员)%>' =服务器/>
< / TD>< TD>
< ASP:HiddenField ID =hdnDesignation值='<%#的eval(说明)%GT;' =服务器/>
< ASP:DropDownList的ID =DropDownList1=服务器AppendDataBoundItems =真>
 < / ASP:DropDownList的>
< / TD>< / TR>
< BR />< BR />< BR />
< / ItemTemplate中>
< / ASP:直放站>


解决方案

您正在使用指定为文本字段,雇员作为选择值字段。然后,您尝试设置基于名称选择的值。这不能真正发挥作用。

你可以做的就是找​​到由文本缺省项:

  //添加一些错误处理的情况下,当项目无法找到
变种defaultText = Designation.Value.ToString();
selectList.Items.FindByText(defaultText).Selected = TRUE;

I have used DropDownList in Repeater. 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. Both users shows admin in dropdown as default selected value, in db its manager.

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

    <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. 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;

这篇关于DROPDOWNLIST没有显示选择的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆