我无法从 DropDownList 中获取用户选择的值。我发现我需要回发才能重置我的值并能够重新分配。(我在页面控件中尝试过诸如 ClientIDMode="static" 之类的操作并执行 "var dropDownList= $(<%= ddlNewCaseFiles.ClientID %>).val() )但是似乎没有任何效果。所以我在我的标题:
<script runat="server">
protected void ddlCaseFilesNew_SelectedIndexChanged(object sender, EventArgs e)
{
hidNewCaseFile.Value = ddlCaseFilesNew.SelectedItem.Value;
}
</script>
它似乎正在选择更新的值。但是由于某种原因,当我单击某个项目时,它会将我发送到“找不到资源”页面。我猜如果我在我的 DropDownList 中添加一个 Updatepanel 它可能会修复它?(因为它只是回发我的 dropDownList)。但我遇到了麻烦。
这是我的下拉列表:
<div id="ddlFiles">
<label>
Select new CaseFile:</label>
<asp:DropDownList runat="server" ID="ddlCaseFilesNew" DataSourceID="dsCaseFiles"
DataTextField="Display" DataValueField="FileID"
OnPreRender="ddl_PreRender" Width="300px" AutoPostBack="true"
onselectedindexchanged="ddlCaseFilesNew_SelectedIndexChanged"/>
</div>
我试图在这里实现它,但我得到一个“UpdatePanel 没有名为“dropDownList”的属性名称”。任何帮助都会非常感激。:
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<asp:DropDownList runat="server" ID="ddlCaseFilesNew" DataSourceID="dsCaseFiles"
DataTextField="Display" DataValueField="FileID" OnPreRender="ddl_PreRender" Width="300px"
AutoPostBack="true" onselectedindexchanged="ddlCaseFilesNew_SelectedIndexChanged" />
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlCaseFilesNew" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
按要求预渲染:
protected void ddl_PreRender(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
try
{
if (ddl.Items[0].Value != "-1")
ddl.Items.Insert(0, new ListItem("--Select--", "-1"));
}
catch
{
ddl.Items.Insert(0, new ListItem("--Select--", "-1"));
}
}