![](/img/trans.png)
[英]How to get dropdown list selected value in Label without using autopostback and update panel in asp.net
[英]How to get value of dropdown in javascript when it is in update panel
我結合ddlstate
上selectedindechanged
的事件ddlCountry
只。 在javascript中檢查ddlState
下拉值是否為“ -1”,然后生成警報。 當我檢查javascript中的值時,它顯示了一個空白值“”。 當我將Postbacktrigger
用於ddlState
,可以使用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;
}
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;
}
首先,我認為您不能直接調用“ ddlCountry.value”,該ID用於服務器控件! 您需要使用獲取客戶端ID。
document.getElementById('ddlCountry').value;
另一件事,如果您想在更改下拉列表時在javascript上觸發事件,則可以添加onchange客戶端事件,如下所示,這將調用javascript函數而不發布頁面(刷新):
<asp:DropDownList ID="ddlState " runat="server"
CssClass="csstextbox" Width="177px" onchange="validateForm()">
然后,您可以根據需要刪除eventtrigger和updatepanel。
希望能幫助到你。
//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.