包含javascript返回false的dropdownlist的onchange不会停止调用dropdownlist _SelectedIndexChange

时间:2015-11-06 14:36:17

标签: javascript c# jquery asp.net c#-4.0

我在转发器中放置了一个下拉列表

<asp:Repeater ID="rptCommissionDistribution" runat="server" OnItemDataBound="rptCommissionDistribution_ItemDataBound"
                OnItemCreated="rptCommissionDistribution_ItemCreated" OnItemCommand="rptCommissionDistribution_ItemCommand">

<asp:DropDownList ID="ddlSplitType" runat="server" DataTextField="SplitTypeName" OnChange="return  confirm_change(this.id,this.value);" DataValueField="SplitTypeId" AutoPostBack="True">
</asp:DropDownList>
<asp:CheckBox ID="chkOverride" runat="server" AutoPostBack="true" OnChange="setdirty();" />
</asp:Repeater>

放置了javascript

<script type="text/javascript">
function confirm_change(id, value) {
    var dropdownid = id;
    var checkboxid = dropdownid.replace("ddlSplitType", "chkOverride");

    if (document.getElementById(checkboxid).checked == true) {          
        alert('clicked');
        return true;
    }
    else {            
        alert('not clicked');
        return false;
    }
}
</script>

在.cs页面上我们有

protected void ddlSplitType_SelectedIndexChanged(object sender, EventArgs e)
 {  
DropDownList ddlSplitType = sender as DropDownList;
RepeaterItem rptCommissionDistributionItem = ddlSplitType.Parent as RepeaterItem;       
CommissionDistributionController objCommissionDistributionController = new CommissionDistributionController();
***-----------And all Stuff--------------***
} 

我想要的是当javascript“返回false”时不应该调用.cs页面代码“ddlSplitType_SelectedIndexChanged”,当javascript“返回true时”那个时候应该调用我的cs页面代码“ddlSplitType_SelectedIndexChanged”。

2 个答案:

答案 0 :(得分:0)

这应该这样做。 (由于您在转发器中使用了DDL,因此必须先找到它)

背后的代码

        protected void Page_Load(object sender, EventArgs e)
        {
             //Only if the event argument was from my drop down add the event
                if (Request.Params["__EVENTARGUMENT"] != null && Request.Params["__EVENTARGUMENT"].Equals("ddlSplitTypechange"))
                    ddlSplitType_SelectedIndexChanged(sender, e);
                if (!Page.IsPostBack)
                {
                    ddlSplitType.Attributes.Add("onchange", "confirm_change();");
            }
        }
        protected void ddlSplitType_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

在你的JS中

function confirm_change(id, value) {
    var dropdownid = id;
    var checkboxid = dropdownid.replace("ddlSplitType", "chkOverride");

    if (document.getElementById(checkboxid).checked == true) {          
        alert('clicked');
    __doPostBack("ctl00$MainContent$ddlSplitType","ddlSplitTypechange");
    }
    else {            
        alert('not clicked');
        return false;
    }

    }

你也可以试试这个

     <asp:DropDownList ID="ddlSplitType" runat="server" 
 AutoPostBack="true" onchange="javascript: return confirm_change();" OnSelectedIndexChanged="ddlSplitType_SelectedIndexChanged"></asp:DropDownList>

希望这有帮助!

答案 1 :(得分:0)

试试这个:

<强> ASP.NET:

<asp:DropDownList
    ID="ddlTest1"
    runat="server"
    onChange="if (!jsFunction()) return false;"
    OnSelectedIndexChanged="ddlTest1_SelectedIndexChanged"
    AutoPostBack="True">
</asp:DropDownList>

<强>使用Javascript:

function jsFunction() 
{
    return confirm("Yes or No?");
}

代码背后(C#):

protected void ddlTest1_SelectedIndexChanged(object sender, EventArgs e)
{
    // Do Something
}