Trushant04
Trushant04

Reputation: 81

DropdownList is not showing the selected value

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>

Upvotes: 1

Views: 2760

Answers (1)

Marian Polacek
Marian Polacek

Reputation: 2925

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;

Upvotes: 3

Related Questions