[英]How to get value of dropdown in javascript when it is in update panel
I'm binding ddlstate
on selectedindechanged
event of ddlCountry
only. 我结合ddlstate
上selectedindechanged
的事件ddlCountry
只。 ddlState
dropdown value checking in javascript if it is "-1" then generate alert. 在javascript中检查ddlState
下拉值是否为“ -1”,然后生成警报。 When I check the value in javascript, it shows me a blank value, "". 当我检查javascript中的值时,它显示了一个空白值“”。 When I use Postbacktrigger
for ddlState
, I can get the value with javascript, but the smoothness of the page using async is better than Postbacktrigger. 当我将Postbacktrigger
用于ddlState
,可以使用javascript获取值,但是使用async进行页面的平滑性要比Postbacktrigger更好。 That is why I use async trigger. 这就是为什么我使用异步触发器。 My main problem is ddlState
's value is not getting in javascript when I use async trigger while I can get it using Postback trigger. 我的主要问题是使用异步触发器时ddlState
的值未进入javascript,而我可以使用回发触发器来获取它。
JavaScript validation: 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 code: 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" />
Code Behind: 背后的代码:
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;
}
First of all I think you cannot call "ddlCountry.value" directly, that id is for the server control! 首先,我认为您不能直接调用“ ddlCountry.value”,该ID用于服务器控件! You need to use, to get the client id. 您需要使用获取客户端ID。
document.getElementById('ddlCountry').value;
Another thing if you want to fire event on javascript when you change the dropdownlist you can add onchange client event like this, this will call a javascript function without posting the page (Refresh): 另一件事,如果您想在更改下拉列表时在javascript上触发事件,则可以添加onchange客户端事件,如下所示,这将调用javascript函数而不发布页面(刷新):
<asp:DropDownList ID="ddlState " runat="server"
CssClass="csstextbox" Width="177px" onchange="validateForm()">
Then you can remove the eventtrigger and the updatepanel if you want. 然后,您可以根据需要删除eventtrigger和updatepanel。
Hope it helps. 希望能帮助到你。
//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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.