2008-09-16 114 views
3

我有一个autodostback设置为true的下拉列表。我希望 用户确认他们是否真的要更改值 ,该值在发回服务器端事件(selectedindexchanged)时触发。客户端确认后的DropdownList autoposback

我试过添加一个onchange属性“return confirm('请点击OK来改变,否则点击CANCEL?';”)但是不会回发,无论确认结果如何,列表中的值都不会恢复返回如果取消 选中。

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

任何潜在客户都将有所帮助。

谢谢!

回答

8

你试过设置onChange事件到javascript函数,然后在函数内部显示javascript警报,并使用__doPostback函数,如果它通过?


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

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

此解决方案工作。我喜欢它,因为它是最简单的。从来没有想过我会需要调用__doPostBack(()。 谢谢! – Aamir 2008-09-16 18:40:29

0

确保您的活动是有线:

dropDown.SelectedIndexChanged += new EventHandler(dropDown_SelectedIndexChanged); 

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

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

您可以通过调用该你做的确认(JavaScript函数)利用CustomValidator控件来“验证”下拉菜单:

 <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" /> 
+0

我认为这是我见过的最好的解决方案,经过多次搜索这是我第一次看到它,我不知道为什么更多的人不建议它。 – 2012-07-25 17:08:27

1

目前,你总是返回confirm()的结果,因此即使它返回true,你还是会阻止事件的执行前回传能火。你应该onchangereturn false;confirm()呢,太像这样:

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

重写onchange属性,如果你有必须的AutoPostBack设置为true,因为ASP.NET会始终添加以下到年底将无法正常工作您的onchange脚本:

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

如果你设置的AutoPostBack为false,然后覆盖平变化用“确认和__doPostBack”类型的脚本(见上文,呃..下面),将工作,但您可能需要手动创建__doPostBack功能。

5

下工作时,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};"); 
1
if (!confirm('Please click OK to change. Otherwise click CANCEL?')) return false; 

总是返回所以下拉列表的OnSelectedIndexChanged事件触发用户是否点击确定或取消。

0
&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; 

写入函数内联,并没有一个“回报”您想要回发的条件。这工作,并按照标准。