我有一个自动回发设置为 true 的下拉列表。 我想要
用户确认他们是否确实想要更改该值,
在回发时会触发服务器端事件(selectedindexchanged)。
我尝试添加 onchange 属性“返回确认('请单击确定进行更改。否则单击取消?';”),但无论确认如何,它都不会回发
如果取消,结果和列表中的值不会恢复回来
已选择。
当我从 DropdownList 标记中删除 onchange 属性时,页面会进行回发。 添加 onchange 属性后就不会了。 我是否还需要连接事件处理程序(我使用的是 C# .Net 2.0 )。
任何线索都会有帮助。
谢谢!
I have a dropdownlist with the autopostback set to true. I want the
user to confirm if they really want to change the value,
which on post back fires a server side event (selectedindexchanged).
I have tried adding an onchange attribute "return confirm('Please click OK to change. Otherwise click CANCEL?';") but it will not postback regardless of the confirm
result and the value in the list does not revert back if cancel
selected.
When I remove the onchange attribute from the DropdownList tag, the page does postback. It does not when the onchange attribute is added. Do I still need to wire the event handler (I'm on C# .Net 2.0 ).
Any leads will be helpful.
Thanks!
发布评论
评论(8)
始终返回,因此无论用户单击“确定”还是“取消”,都会触发下拉列表的 OnSelectedIndexChanged 事件。
Always returns so dropdownlist's OnSelectedIndexChanged event fires whether user clicks OK or CANCEL.
确保您的事件已连接:
您还可以应用客户端属性来返回确认。 如果取消则相应地设置索引。
Make sure your event is wired:
You can also apply a client-side attribute to return the confirmation. Set the index accordingly if cancelled.
内联编写函数,并且对于您想要回发的条件没有“返回”。 这有效并且符合标准。
Write the function inline and dont have a "return" for the condition in which you want a post back. This works and is as per the standards.
当 DropDownList 触发部分回发时,以下内容有效:
The following works when the DropDownList is triggering partial postbacks:
目前,您始终返回
confirm()
的结果,因此即使它返回true
,您仍然会在回发触发之前停止事件的执行。 仅当confirm()
也这样做时,您的onchange
才应该返回 false;
,如下所示:Currently, you're always returning the result of the
confirm()
, so even if it returnstrue
, you'll still stop execution of the event before the postback can fire. Youronchange
shouldreturn false;
only when theconfirm()
does, too, like this:如果将 AutoPostBack 设置为 true,则覆盖 onchange 属性将不起作用,因为 ASP.NET 始终将以下内容附加到 onchange 脚本的末尾:
如果将 AutoPostBack 设置为 false,则使用“confirm and __doPostBack”类型覆盖 onchange脚本(参见上面,错误..下面)将起作用,但您可能必须手动创建 __doPostBack 函数。
Overriding the onchange attribute will not work if you have have AutoPostBack set to true because ASP.NET will always append the following to the end of your onchange script:
If you set AutoPostBack to false, then overriding onchange with a "confirm and __doPostBack" type script (see above, err.. below) will work but you may have to manually create the __doPostBack function.
您是否尝试过将 onChange 事件设置为 javascript 函数,然后在函数内部显示 javascript 警报并在通过时使用 __doPostback 函数?
IE
Have you tried to set the onChange event to a javascript function and then inside the function display the javascript alert and utilize the __doPostback function if it passes?
i.e.
您可以利用 CustomValidator 控件通过调用执行确认()的 JavaScript 函数来“验证”下拉列表:
You can utilize the the CustomValidator control to "validate" dropdown by calling a javascript function in which you do the confirm():