I have a drop down list with auto repeat set to true. I want the user to confirm whether they really want to change the value that, when called back again, raises an event on the server side (selectedindexchanged).
I tried adding the onchange attribute "return confirm" ("Please click" OK "to change. Otherwise, click" CANCEL? ";"), But it will not return the result regardless of the confirmation, and the value in the list does not return if cancel is selected.
When I remove the onchange attribute from the DropdownList tag, the page postbacks. This does not happen when the onchange attribute is added. I still need to hook up an event handler (I'm in C # .Net 2.0).
Any hints would be helpful.
Thank!
Have you tried setting the onChange event to a javascript function and then displaying a javascript warning inside the function and using 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',''); } }
You can use the CustomValidator control for the validate drop-down list by calling the javascript function in which you execute confirmation ():
<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() :
confirm()
true
onchange
return false;
if (!confirm('Please click OK to change. Otherwise click CANCEL?')) return false;
onchange , AutoPostBack true, ASP.NET onchange script:
;setTimeout('__doPostBack(\'YourDropDown\',\'\')', 0)
AutoPostBack false, onchange "confirm and __doPostBack" script (. , ) , , , __doPostBack.
. OnSelectedIndexChanged , OK CANCEL.
, :
dropDown.SelectedIndexChanged += new EventHandler(dropDown_SelectedIndexChanged);
, . , .
dropDown.Attributes.Add("onchange", "javascript: return confirm('confirmation msg')");
<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>
inline "" , . .
Source: https://habr.com/ru/post/1696974/More articles:Where can I get a simple explanation of policy introduction? - enterprise-librarySilverlight Cross Domain Policies - silverlightHow to convert from location (address) String to YGeoPoint in the Google Maps API? - apiFTP client for .NET. - .netКоманды SMTP для "AUTH NTLM" - smtpПредварительный просмотр файла для всего файла .cs в каталоге и подкаталоге с помощью PowerShell - powershellWriting to a file is more than just text - fileHow to force restart the computer, and not turn off (XP) - windowsCan you have more than one ASP.NET state server in a cluster? - windows-server-2003Interactive world map - suggestions? - flashAll Articles