Javascript确认gridview下拉列表反转结果

时间:2014-02-21 16:43:30

标签: c# javascript asp.net gridview

我有一个下拉列表如下:

<asp:DropDownList ToolTip="# of Records Per Page" ID="dropDownRecordsPerPage" runat="server" TabIndex="-1" OnInit="dropDownRecordsPerPage_Init" EnableViewState="true"
  AutoPostBack="true" OnSelectedIndexChanged="dropDownRecordsPerPage_SelectedIndexChanged" AppendDataBoundItems="true"
  Style="text-align: left; color: gray;">
  <asp:ListItem Value="10" Text="10" />
  <asp:ListItem Value="25" Text="25" />
  <asp:ListItem Value="50" Text="50" Selected="True" />
  <asp:ListItem Value="100" Text="100" />
  <asp:ListItem Value="100000" Text="All" />
</asp:DropDownList>

在Page_Load上我添加:

dropDownRecordsPerPage.Attributes.Add("onchange", "if(CheckDirty2()) return true;");

我的javascript函数是:

 function CheckDirty2() {
        var dirty = document.getElementById('<%= HiddenFieldDirty.ClientID %>').value;
        if (dirty.toLowerCase() == "yes") {
            return  confirm("Are you SURE you want to do this?");
         };
    };

这很有效。这意味着当隐藏字段值为= yes时,确认消息将显示OK和Cancel。这就是我想要发生的事情。

我希望如果用户单击“确定”,则会继续回发,如果用户单击“取消”,则会取消回发。

正在发生的事情恰恰相反。如果用户单击“确定”,则会取消回发。如果他们单击取消,则回发继续。

我确信我错过了一些简单的事情。我已经花了3个多小时来完成这一项,现在我已经彻底搞糊涂了。

任何帮助,有一些细节,将不胜感激。

修改

我通过@Andrei提出修改建议。见下文:

dropDownRecordsPerPage.Attributes.Add("onchange", "if(CheckDirty2()) return false;");

下拉列表生成HTML:

<select style="text-align: left; color: gray;" title="# of Records Per Page" tabindex="-1" 
    id="ctl00_main_dropDownRecordsPerPage" 
    onchange="if(CheckDirty2()) return false;setTimeout('__doPostBack(\'ctl00$main$dropDownRecordsPerPage\',\'\')', 0)" 
    name="ctl00$main$dropDownRecordsPerPage">

    ...

</select>

相同的结果...单击确定回发取消。单击取消后退继续。

作为FYI,这里是我选择的索引更改代码:

        if (!checkDropdownSelection()) return;
        DropDownList dropDownRecordsPerPage = (DropDownList)sender;
        int pagerValue = Convert.ToInt32(dropDownRecordsPerPage.SelectedValue);

        gvChargeEntry.PageSize = pagerValue;
        BindGrid();

1 个答案:

答案 0 :(得分:1)

DropDownList的AutoPostBack很奇怪。它将回发代码附加到onchange代码,而不是为事件分配另一个处理程序。因此,在你的情况下,onchange就像这样:

if(CheckDirty2()) return true;
__doPostBack(...

显然当CheckDirty2为真时,返回发生,代码永远不会到达回发函数。如果CheckDirty2为假,则会调用回发函数。

要解决此问题,您需要将条件修改为只有在确认失败时才会返回(无关紧要)的内容:

if(CheckDirty2() == false) return false;
相关问题