0

我只ddlstateselectedindechanged事件有约束力ddlCountryddlState如果它是“-1”,则在 javascript 中检查下拉值,然后生成警报。当我检查 javascript 中的值时,它会显示一个空白值“”。当我使用PostbacktriggerforddlState时,我可以使用 javascript 获取值,但是使用 async 的页面的流畅度要优于 Postbacktrigger。这就是我使用异步触发器的原因。我的主要问题是ddlState,当我使用异步触发器时,它的值没有进入 javascript,而我可以使用回发触发器来获取它。

JavaScript 验证:

function validateForm()
{
    var ddlCountry = document.getElementById('<%=ddlCountry.ClientID%>');
    var ddlState = document.getElementById('<%=ddlState.ClientID%>');
    if (ddlCountry .value == "-1")
    {
        alert("Country  should not be blank.");
        ddlCountry .focus();
        return false;
    }
    if (ddlState .value == "-1")
    {
        alert("State should not be blank.");
        ddlState .focus();
        return false;
    }

    return true;
}

ASP 代码:

<asp:DropDownList ID="ddlAcqModalityList" runat="server" CssClass="csstextbox" Width="207px" AutoPostBack="true" OnSelectedIndexChanged="ddlAcqModalityList_SelectedIndexChanged">
</asp:DropDownList>
<asp:UpdatePanel ID="updatePanelState" runat="server">
    <ContentTemplate>
        <asp:DropDownList ID="ddlState " runat="server" CssClass="csstextbox" Width="177px">
        </asp:DropDownList>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="ddlCountry" EventName="SelectedIndexChanged" />
    </Triggers>
</asp:UpdatePanel>
<asp:Button ID="btnSave" runat="server" Width="80px" OnClientClick="return validateForm();" Text="Save" CssClass="cssbutton" OnClick="btnSave_Click" />

代码背后:

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        BindCountry()
        {
            strSQL = @"SELECT Country_ID,Country_Desc
                       FROM Country_Master";

            DataTable dataTableState = null;
            dataTableState = objSqlDbComm.ExecuteDatasetQuery(strSQL).Tables[0];

            var dictioneryCountry = new Dictionary<int, string>();
            foreach(DataRow dr in dataTableStudy.Rows)
            {
                dictioneryCountry .Add(Convert.ToInt32(dr["Country_ID"]), dr["Country_Desc"].ToString());
            }

            ddlCountry.DataTextField = "Value";
            ddlCountry.DataValueField = "Key";
            ddlCountry.DataSource = dictioneryCountry;
            ddlCountry.DataBind();
            ddlCountry.Items.Insert(0, new ListItem("[Select]", "-1"));
            ddlCountry.Items[0].Selected = true;
        }
    }
}

protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
    int countryID = Convert.ToInt32(ddlCountry.SelectedItem.Value);

    ifcountryID == -1)
    {
        return;
    }

    strSQL = @"SELECT State_ID,State_Desc
               FROM State_Master
               WHERE countryID = '" + countryID + @"';

    DataTable dataTableState = null;
    dataTableState = objSqlDbComm.ExecuteDatasetQuery(strSQL).Tables[0];

    var dictioneryState = new Dictionary<int, string>();
    foreach(DataRow dr in dataTableStudy.Rows)
    {
        dictioneryState .Add(Convert.ToInt32(dr["State_ID"]), dr["State_Desc"].ToString());
    }

    ddlState.DataTextField = "Value";
    ddlState.DataValueField = "Key";
    ddlState.DataSource = dictioneryState;
    ddlState.DataBind();
    ddlState.Items.Insert(0, new ListItem("[Select]", "-1"));
    ddlState.Items[0].Selected = true;
}
4

2 回答 2

0

首先,我认为您不能直接调用“ddlCountry.value”,该 ID 用于服务器控制!您需要使用,来获取客户端 ID。

document.getElementById('ddlCountry').value;

另一件事,如果您想在更改下拉列表时在 javascript 上触发事件,您可以像这样添加 onchange 客户端事件,这将调用 javascript 函数而不发布页面(刷新):

<asp:DropDownList ID="ddlState " runat="server" 
CssClass="csstextbox" Width="177px" onchange="validateForm()">

然后,您可以根据需要删除事件触发器和更新面板。

希望能帮助到你。

于 2012-05-17T13:05:59.747 回答
0
        //javascript code
var hidStateValue=document.getElementByID('<%=hidStateValue.ClientID%>');
        if (hidStateValue.value == "-1")
                    {
                        alert("State should not be blank.");                
                        return false;
                    }

        //aspx code
        <asp:DropDownList ID="ddlState" AutoPostBack="true"  runat="server" CssClass="csstextbox" Width="177px" onselectedindexchanged="ddlState_SelectedIndexChanged">                                                                </asp:DropDownList>
            <asp:HiddenField ID="hidStateValue" runat="server" />

        //Code behind

          protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
         {
            //code
            hidStateValue.Value = ddlState.SelectedItem.Value;
         }
        protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
            {       
                hidStateValue.Value="";
                hidStateValue.Value = ddlState.SelectedItem.Value;      
            }
于 2012-05-18T07:13:53.937 回答