客户确认后 DropdownList 自动返回

发布于 2024-07-04 03:03:50 字数 350 浏览 14 评论 0 原文

我有一个自动回发设置为 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

玩心态 2024-07-11 03:03:50
if (!confirm('Please click OK to change. Otherwise click CANCEL?')) return false;

始终返回,因此无论用户单击“确定”还是“取消”,都会触发下拉列表的 OnSelectedIndexChanged 事件。

if (!confirm('Please click OK to change. Otherwise click CANCEL?')) return false;

Always returns so dropdownlist's OnSelectedIndexChanged event fires whether user clicks OK or CANCEL.

太阳公公是暖光 2024-07-11 03:03:50

确保您的事件已连接:

dropDown.SelectedIndexChanged += new EventHandler(dropDown_SelectedIndexChanged);

您还可以应用客户端属性来返回确认。 如果取消则相应地设置索引。

dropDown.Attributes.Add("onchange", "javascript: return confirm('confirmation msg')");

Make sure your event is wired:

dropDown.SelectedIndexChanged += new EventHandler(dropDown_SelectedIndexChanged);

You can also apply a client-side attribute to return the confirmation. Set the index accordingly if cancelled.

dropDown.Attributes.Add("onchange", "javascript: return confirm('confirmation msg')");
玩物 2024-07-11 03:03:50
<asp:DropDownList runat="server" ID="ddlShailendra"  AutoPostBack="True" OnSelectedIndexChanged="ddlShailendra_SelectedIndexChanged" onchange="javascript: { if(confirm('Click ok to prevent post back, Cancel to make a postback'))return true;} " >
          <asp:ListItem Text="tes" Value="1" ></asp:ListItem>
            <asp:ListItem Text="test" Value="-1"></asp:ListItem>
            </asp:DropDownList>

内联编写函数,并且对于您想要回发的条件没有“返回”。 这有效并且符合标准。

<asp:DropDownList runat="server" ID="ddlShailendra"  AutoPostBack="True" OnSelectedIndexChanged="ddlShailendra_SelectedIndexChanged" onchange="javascript: { if(confirm('Click ok to prevent post back, Cancel to make a postback'))return true;} " >
          <asp:ListItem Text="tes" Value="1" ></asp:ListItem>
            <asp:ListItem Text="test" Value="-1"></asp:ListItem>
            </asp:DropDownList>

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.

深爱成瘾 2024-07-11 03:03:50

当 DropDownList 触发部分回发时,以下内容有效:

// caching selected value at the time the control is clicked
MyDropDownList.Attributes.Add(
    "onclick",
    "this.currentvalue = this.value;");

// if the user chooses not to continue then restoring cached value and aborting by returning false
MyDropDownList.Attributes.Add(
    "onchange",
    "if (!confirm('Do you want to continue?')) {this.value = this.currentvalue; return false};");

The following works when the DropDownList is triggering partial postbacks:

// caching selected value at the time the control is clicked
MyDropDownList.Attributes.Add(
    "onclick",
    "this.currentvalue = this.value;");

// if the user chooses not to continue then restoring cached value and aborting by returning false
MyDropDownList.Attributes.Add(
    "onchange",
    "if (!confirm('Do you want to continue?')) {this.value = this.currentvalue; return false};");
无人问我粥可暖 2024-07-11 03:03:50

目前,您始终返回 confirm() 的结果,因此即使它返回 true,您仍然会在回发触发之前停止事件的执行。 仅当 confirm() 也这样做时,您的 onchange 才应该返回 false;,如下所示:

if (!confirm('Please click OK to change. Otherwise click CANCEL?')) return false;

Currently, you're always returning the result of the confirm(), so even if it returns true, you'll still stop execution of the event before the postback can fire. Your onchange should return false; only when the confirm() does, too, like this:

if (!confirm('Please click OK to change. Otherwise click CANCEL?')) return false;
乱了心跳 2024-07-11 03:03:50

如果将 AutoPostBack 设置为 true,则覆盖 onchange 属性将不起作用,因为 ASP.NET 始终将以下内容附加到 onchange 脚本的末尾:

;setTimeout('__doPostBack(\'YourDropDown\',\'\')', 0)

如果将 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:

;setTimeout('__doPostBack(\'YourDropDown\',\'\')', 0)

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.

静若繁花 2024-07-11 03:03:50

您是否尝试过将 onChange 事件设置为 javascript 函数,然后在函数内部显示 javascript 警报并在通过时使用 __doPostback 函数?

IE

   
drpControl.Attributes("onChange") = "DisplayConfirmation();"

function DisplayConfirmation() {
  if (confirm('Are you sure you want to do this?')) {
    __doPostback('drpControl','');
  }
}

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.

   
drpControl.Attributes("onChange") = "DisplayConfirmation();"

function DisplayConfirmation() {
  if (confirm('Are you sure you want to do this?')) {
    __doPostback('drpControl','');
  }
}
眼趣 2024-07-11 03:03:50

您可以利用 CustomValidator 控件通过调用执行确认()的 JavaScript 函数来“验证”下拉列表:

        <asp:DropDownList ID="TestDropDown" runat="server" AutoPostBack="true" CausesValidation="true"
            ValidationGroup="Group1"
            OnSelectedIndexChanged="TestDropDown_SelectedIndexChanged">
            <asp:ListItem Value="1" Text="One" />
            <asp:ListItem Value="2" Text="Two" />
        </asp:DropDownList>
       <script type="text/javascript">
            function ConfirmDropDownValueChange(source, arguments) {
                arguments.IsValid = confirm("Are you sure?");
            }
        </script>
        <asp:CustomValidator ID="ConfirmDropDownValidator" runat="server"
            ClientValidationFunction="ConfirmDropDownValueChange" Display="Dynamic" ValidationGroup="Group1"  />

You can utilize the the CustomValidator control to "validate" dropdown by calling a javascript function in which you do the confirm():

        <asp:DropDownList ID="TestDropDown" runat="server" AutoPostBack="true" CausesValidation="true"
            ValidationGroup="Group1"
            OnSelectedIndexChanged="TestDropDown_SelectedIndexChanged">
            <asp:ListItem Value="1" Text="One" />
            <asp:ListItem Value="2" Text="Two" />
        </asp:DropDownList>
       <script type="text/javascript">
            function ConfirmDropDownValueChange(source, arguments) {
                arguments.IsValid = confirm("Are you sure?");
            }
        </script>
        <asp:CustomValidator ID="ConfirmDropDownValidator" runat="server"
            ClientValidationFunction="ConfirmDropDownValueChange" Display="Dynamic" ValidationGroup="Group1"  />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文