我正在写一个SharePoint应用程序,并有一个下拉列表的问题。它的SelectedIndex没有改变。我读了很多问题和答案,他们建议在true
中使用IsPostBack
或EnableViewState
。我两个都做了,但都没有成功。
ASP代码:
<div class="value">
<asp:DropDownList ID="groupingDropDownList" runat="server" EnableViewState="True" OnSelectedIndexChanged="groupingDropDownList_SelectedIndexChanged" AutoPostBack="true" />
</div>
C#代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
groupingDropDownList.DataSource = sourceList.Select(x => new { x.Name, Value = x });
groupingDropDownList.DataTextField = "Name";
groupingDropDownList.DataValueField = "Value";
groupingDropDownList.DataBind();
}
}
事件SelectedIndexChanged
未激发,因为索引未更改。我在if (!Page.IsPostBack)
行设置了一个断点,它总是0。
发布于 2019-05-23 21:16:10
我知道我错在哪里,是那句台词:
groupingDropDownList.DataSource = sourceList.Select(x => new { x.Name, Value = x });
我总是设置等于类名的值,因为值没有改变,控件没有注意到索引的变化。
发布于 2019-05-23 20:59:29
在!Page.IsPostBack块中,您是第一次填充dropdown。您似乎正在尝试检测正在加载的下拉选择中的更改-这是不可能的。
因此,若要测试它,应在页的回发中或在groupingDropDownList_SelectedIndexChanged事件处理程序中进行检查。
if (!Page.IsPostBack)
{
groupingDropDownList.DataSource
= sourceList.Select(x => new { x.Name, Value = x });
groupingDropDownList.DataTextField = "Name";
groupingDropDownList.DataValueField = "Value";
groupingDropDownList.DataBind();
}
// Actual postback
else
{
var test = groupingDropDownList.SelectedValue;
}
https://stackoverflow.com/questions/56275426
复制相似问题