我仅在ddlstate
selectedindechanged
事件上绑定ddlCountry
。 {j}中的ddlState
下拉值检查如果为“-1”则生成警报。当我检查javascript中的值时,它会显示一个空值,“”。当我使用Postbacktrigger
作为ddlState
时,我可以使用javascript获取值,但使用async的页面的平滑度优于Postbacktrigger。这就是我使用异步触发器的原因。我的主要问题是当我使用异步触发器时ddlState
的值没有进入javascript,而我可以使用Postback触发器来获取它。
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;
}
ASPX代码:
<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;
}
答案 0 :(得分:0)
首先,我认为你不能打电话给#34; ddlCountry.value&#34;直接,该id用于服务器控件!您需要使用,以获取客户端ID。
document.getElementById('ddlCountry').value;
另一件事如果你想在更改下拉列表时在javascript上触发事件,你可以像这样添加onchange客户端事件,这将调用javascript函数而不发布页面(刷新):
<asp:DropDownList ID="ddlState " runat="server"
CssClass="csstextbox" Width="177px" onchange="validateForm()">
然后,您可以根据需要删除eventtrigger和updatepanel。
希望它有所帮助。
答案 1 :(得分: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;
}