我只ddlstate
对selectedindechanged
事件有约束力ddlCountry
。ddlState
如果它是“-1”,则在 javascript 中检查下拉值,然后生成警报。当我检查 javascript 中的值时,它会显示一个空白值“”。当我使用Postbacktrigger
forddlState
时,我可以使用 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;
}