简体   繁体   English

在gridview中下拉更改时发出警报

[英]Alert on dropdown change in gridview

I have a gridview with dropdown and whenever a change is made in a grid dropdown and the user navigates to another page without clicking save button I display an alert stated in the JS below. 我有一个带有下拉菜单的gridview,并且每当在网格下拉菜单中进行更改时,只要用户导航到另一个页面而无需单击保存按钮,我都会在下面的JS中显示警告。 If the user clicks ok it allows you to navigate to another page and this scenario workd fine. 如果用户单击“确定”,则允许您导航到另一个页面,此方案工作正常。

Problem part: 问题部分:

If the user clicks cancel in the displayed alert it will not allow to navigate to another page and will remain in the same page. 如果用户在显示的警报中单击“取消”,则它将不允许导航到另一页面,并将保留在同一页面中。 And now if I navigate to another page without hitting save the alert doesn't come, the second time I am not able to give an alert on cancel action. 现在,如果我没有找到“保存”就导航到另一个页面,则不会出现警报,这是我第二次无法对取消操作发出警报。

Save Button: 保存按钮:

 <asp:Button ID="btnSave" runat="server"  OnClick="Save_Click" Text="Save"
       CssClass="button" OnClientClick="MySuperDuper.globalChange = false;" />

Grid Dropdown: 网格下拉列表:

<asp:TemplateField HeaderText="Product">
<ItemTemplate>
<asp:DropDownList ID="ddlProduct" runat="server" Enabled="True"
DataTextField="Prod" DataValueField="Prod" OnSelectedIndexChanged="ddlProduct_Change"
onchange="MySuperDuper.globalChange = true;">
</asp:DropDownList>
</asp:TemplateField>

Javascript: 使用Javascript:

 <script type="text/javascript">

    var MySuperDuper = new function () {
        var _this = this;

        this.saving;
        this.navigateAwayMessage = "You have made some changes which" +
                           " will be lost if you leave this page. " +
                           "Do you want to leave this page?";

        this.checkForChanges = function () {
            if (!_this.saving && typeof (_this.globalChange) !=
               "undefined" && _this.globalChange) {
                if (document.all && event) {
                    MySuperDuper.globalChange = false;
                    event.returnValue = _this.navigateAwayMessage;
                }
                else {
                    MySuperDuper.globalChange = false;
                    return _this.navigateAwayMessage;
                }
            }
        }
    }

    window.onbeforeunload = function () {
        return MySuperDuper.checkForChanges();
    }

</script>

Why are you setting your MySuperDuper.globalChange to false on the click? 为什么在点击时将MySuperDuper.globalChange设置为false It would seem, by doing this, the only way you can set it to true again would be to change the dropdown - which sets it to true . 通过执行此操作,似乎可以再次将其设置为true的唯一方法是更改dropdown -将其设置为true

Have you tried calling the following instead: 您是否尝试过调用以下内容:

<asp:Button ID="btnSave" runat="server"  OnClick="Save_Click" Text="Save"
       CssClass="button" OnClientClick="return MySuperDuper.checkForChanges()" />

This is asking your button to return the result of your method. 这是在要求您的按钮返回方法的结果。 If true then your button will continue its normal course - in this case Save_Click - if false it won't. 如果为true则您的按钮将继续其正常过程-在这种情况下为Save_Click如果为false ,则不会继续。

And in your function, instead of returning a message return the result of a confirmation box (which would be true or false )- show the confirmation box in your method. 并且在函数中,不返回消息,而是返回确认框的结果(可能为truefalse )-在方法中显示确认框。

To return the result of a confirmation box you would simply do something like: 要返回确认框的结果,您只需执行以下操作:

return = confirm(this.navigateAwayMessage);

Either that, or simply remove the OnClientClick event and let your current onbeforeunload method take care of the rest. 或者,或者只是删除OnClientClick事件,然后让您当前的onbeforeunload方法处理其余的事情。

protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (GridViewRow gvRow in GridView2.Rows)
{
    Control ctrl = gvRow.FindControl("ddl_jd");
    DropDownList ddl = ctrl as DropDownList;
    if (ddl != null)
        ddl.SelectedIndex = DropDownList3.SelectedIndex;
}
}

Also make sure to set AutoPostBack="true" for DropDownList3. 还要确保为DropDownList3设置AutoPostBack =“ true”。

Another approach (that is not very clean or simple) is to add the following code into the Page_Load method (and remove the script block containing onJDSelection function from the .aspx file): 另一种方法(不是很干净或不简单)是将以下代码添加到Page_Load方法中(并从.aspx文件中删除包含onJDSelection函数的脚本块):

if (!Page.IsPostBack)
{
    string functionContent = "<script language=javascript> function onJDSelection()" + 
        "{ var selectedIndex = document.getElementById('" + DropDownList3.ClientID + "').selectedIndex;" + 
        "var grid = document.getElementById('" + GridView2.ClientID + "');  " +
        "for (var i = 1; i < grid.rows.length; i++) " +
            "{ var selObj = grid.rows[i].cells[0].getElementsByTagName(\"*\")[0]; selObj[selectedIndex].selected = true;} "+
        "}</script>";
    Page.RegisterStartupScript("MyScript", functionContent);
    DropDownList3.Attributes.Add("onchange", "onJDSelection()");
}.

Note that is this case the ID used for retrieving DropDownList3 and GridView2 in javascript are sent from code behind as is not very safe to rely on server control ID's generated by ASP .NET. 请注意,在这种情况下,用于从JavaScript中检索DropDownList3和GridView2的ID是从后面的代码发送的,因为依靠ASP .NET生成的服务器控件ID并不是很安全。 In case you want to save the dropdownlists values (that are changed using javascript) you will also need additional code. 如果您要保存下拉列表值(使用javascript进行更改),则还需要其他代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM