更改包含返回false的javascript的dropdownlist不会停止调用dropdownlist_Select

本文关键字:dropdownlist 调用 Select false 返回 javascript 包含 | 更新日期: 2023-09-27 18:20:48

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

<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"return false;"时,不应调用.cs页面代码"ddlSplitType_SelectedIndexChanged",而当javascript"returns true;"时应调用我的cs页面代码"ddlSplitType_SelectedIndex Changed"

更改包含返回false的javascript的dropdownlist不会停止调用dropdownlist_Select

这样就可以了。(因为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>

希望这能有所帮助!

试试这个:

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
}