Dav*_*ave 5 javascript c# asp.net postback webforms
我发现__doPostBack存在问题,并找到了解决方法。我正在寻找原因的解释和/或比我的工作更好的解决方案。
场景: 我在下拉列表中填充了值;“-选择-”,“一个”和“两个”。如果用户选择“一个”,则执行客户端脚本。如果用户选择“两个”,则执行服务器端脚本。
问题: 客户端脚本通过调用__doPostBack来发起回发。但是,除非页面上没有LinkButton,Calendar或WizardStep控件,否则实际上不会发生任何回发。我实际上检查了Visual Studio Toolbox中的所有标准工具,并对它们全部进行了测试。它必须是这三个之一。
解决方法: 添加一个链接按钮,该按钮周围环绕一个跨度,且显示设置为无。
<span style="display:none;">
<asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>
</span>
Run Code Online (Sandbox Code Playgroud)
问题:有人可以提供这种行为的解释,或者提供比我的“变通方法”更好的解决方案吗?
来源-Javascript(我将其放置在head标签之间)
<script language="javascript" type="text/javascript">
function DropDownList1_change(elementRef) {
var selectedIndex = elementRef.selectedIndex;
if (selectedIndex > 0) {
var selectedValue = elementRef.options[selectedIndex].value;
if (selectedValue == "One") {
alert("Because you selected 'One', special javascript code will be executed");
// Special javascript code goes here
return;
}
else if (selectedValue == "Two") {
// Special server code gets executed on server DropDownList1_SelectedIndexChanged
__doPostBack('DropDownList1', '');
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
来源-ASPX控件
<asp:DropDownList ID="DropDownList1" runat="server" onchange="DropDownList1_change(this)" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>-Select-</asp:ListItem>
<asp:ListItem>One</asp:ListItem>
<asp:ListItem>Two</asp:ListItem>
</asp:DropDownList>
<br />
<!-- For some unknown reason __doPostBack only works if there is a LinkButton, Calendar or WizardStep control on the page -->
<span style="display:none;">
<asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>
</span>
Time of last Post Back: <asp:Label ID="Label1" runat="server"></asp:Label><br />
Time of OnSelectedIndexChanged: <asp:Label ID="Label2" runat="server"></asp:Label>
Run Code Online (Sandbox Code Playgroud)
源代码-后台代码
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToLongTimeString();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Label2.Text = DateTime.Now.ToLongTimeString();
}
Run Code Online (Sandbox Code Playgroud)
其他资源 -发布此问题后,我找到了以下文章。这是一篇非常古老的Microsoft文章,也是我发现的唯一一篇Microsoft文章,其中提到了DropDowns,返回值和回发的特定限制。我还没有深入研究他们的解决方案,并且不确定时间是否允许我这样做。主要是发布它,以防我的解决方案失败或对其他人不起作用。
直观上,您可能会认为为DropDownList添加确认对话框与为Button Web控件添加此类对话框相同。也就是说,只需将DropDownList的客户端onchange属性设置为类似以下内容:return Confirm(...);。使用:DropDownListID.Attributes(“ onchange”)=“返回确认(...);” 不幸的是,这将无法正常工作,因为AutoPostBack DropDownList的onchange属性将设置为会导致回发的JavaScript的一部分,即对客户端__doPostBack函数的调用。当您自己以编程方式设置onchange属性时,最终结果是呈现的客户端onchange事件处理程序同时具有您的代码和对__doPostBack的调用:
这篇文章很长,因此请搜索“使用AutoPostBack DropDownLists进行确认”
有2个解决方案。
解决方案1:与添加由隐藏的span标签包围的链接按钮相比, 一种更好的解决方法是在页面加载事件中添加以下内容。这样可以确保__doPostBack函数可用。
protected void Page_Load(object sender, EventArgs e)
{
Page.ClientScript.GetPostBackEventReference(this, string.Empty);
}
Run Code Online (Sandbox Code Playgroud)
__doPostBack仅当表单中的控件需要执行回发时才生成该函数。这包括LinkButton之类的控件以及其他AutoPostBack设置为的控件true。实际上,只有Button和ImageButton控件可以执行不执行回发操作__doPostBack(请参阅本文)。例如,我们可以在HTML输出中看到以这种方式呈现LinkButton:
<a id="lnk" href="javascript:__doPostBack('lnk','')">My link</a>
Run Code Online (Sandbox Code Playgroud)
在当前情况下,您可以设置AutoPostBack="true"DropDownList:
<asp:DropDownList AutoPostBack="true" onchange="if (!confirmPostBack(this)) return false;" ... >
Run Code Online (Sandbox Code Playgroud)
当您想防止回发时,onchange事件处理程序将返回false。Javascript函数可能是这样的:
function confirmPostBack(ddl)
{
if (condition) {
...
return true;
}
else {
...
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
重要说明:onchange事件处理程序不应返回任何东西以允许回发发生。您可以使用以下语法:
onchange="if (!confirmPostBack(this)) return false;"
Run Code Online (Sandbox Code Playgroud)
由于问题中提到的文章中可能解释的原因,以下语法不起作用。返回true仍然可以防止回发。
onchange="return confirmPostBack(this);" // Does not work!
Run Code Online (Sandbox Code Playgroud)