我有一个自动回发设置为 true 的下拉列表。我希望用户确认他们是否真的想更改该值,后者在后面触发服务器端事件(SelectedIndexChanged)。

我尝试添加 onchange 属性“return recognize('请单击“确定”进行更改。否则单击取消?';''))但是,无论确认结果如何,它都不会发回,并且如果选择取消,则列表中的值不会恢复。

当我从 DropdownList 标记中删除 onchange 属性时,页面会进行回发。添加 onchange 属性后就不会了。我是否还需要连接事件处理程序(我使用的是 C# .Net 2.0 )。

任何线索都会有帮助。

谢谢!

有帮助吗?

解决方案

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

IE。

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

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

其他提示

您可以利用 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"  />

当 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};");

目前,您总是返回结果 confirm(), ,所以即使返回 true, ,您仍然会在回发触发之前停止事件的执行。你的 onchange 应该 return false; 仅当 confirm() 也是这样的:

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

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

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

如果将 AutoPostBack 设置为 false,则使用“confirm and __doPostBack”类型脚本覆盖 onchange(见上文,错误..下面)可以工作,但您可能必须手动创建 __doPostBack 函数。

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

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

确保您的活动已有线连接:

dropDown.SelectedIndexChanged += new EventHandler(dropDown_SelectedIndexChanged);

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

dropDown.Attributes.Add("onchange", "javascript: return confirm('confirmation msg')");
&lt;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;} " &gt;
          &lt;asp:ListItem Text="tes" Value="1" >&lt;/asp:ListItem&gt;
            &lt;asp:ListItem Text="test" Value="-1"&gt;&lt;/asp:ListItem&gt;
            &lt;/asp:DropDownList&gt;

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top