问题
I have a DropDownList in a user control; however, no matter what I do I can't get any of the "selected" properties (SelectedItem, SelectedIndex, SelectedValue) to populate correctly. The value of all three of these properties is the first item in my list no matter which item was actually selected.
<asp:DropDownList ID="ParticipantsSelectList" runat="server">
<asp:ListItem Value="">Please select a team...</asp:ListItem>
<asp:ListItem value="{D37EFA0C-988A-4A2A-8D6E-80E3BAE00DEE}">Blue Team</asp:ListItem>
<asp:ListItem value="{7543E282-C9B8-435A-86A2-70E8E3BB38E5}">Green Team</asp:ListItem>
<asp:ListItem value="{F6BEF34A-215E-4179-9F4C-68F7C43D755F}">Orange Team</asp:ListItem>
<asp:ListItem value="{D50AD44A-686A-4BD3-B62E-D70ABF153AE5}">Red Team</asp:ListItem>
<asp:ListItem value="{DEFB7DA6-B0FF-4C36-A015-F3E8BC7AECA2}">Yellow Team</asp:ListItem>
<asp:ListItem value="{D37EFA0C-988A-4A2A-8D6E-80E3BAE00DEE},{7543E282-C9B8-435A-86A2-70E8E3BB38E5},{F6BEF34A-215E-4179-9F4C-68F7C43D755F},{D50AD44A-686A-4BD3-B62E-D70ABF153AE5},{DEFB7DA6-B0FF-4C36-A015-F3E8BC7AECA2}">All Teams</asp:ListItem>
</asp:DropDownList>
<asp:Button runat="server" Text="Submit" />
private void Page_Load(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(ParticipantsSelectList.SelectedValue)) // SelectedValue is always empty string (value of the first item in the list)
{
resultsPanel.Visible = true;
selectTeamLabel.Visible = false;
GenerateResultsTable();
}
else
{
resultsPanel.Visible = false;
selectTeamLabel.Visible = true;
}
}
回答1:
Try using !Page.IsPostBack
in Page_Load
private void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//Your Code....
}
}
来源:https://stackoverflow.com/questions/12567364/dropdownlist-selected-item-always-first-item-in-list