我有一个数据列表,其中包含 datalistCity 中的两个 dropdownCountry 和一个用于动态添加 dropdownCountry 和 datalistCity 的按钮我想在 DropdownCountry 上填充 dropdowncity selectedindex 更改代码如下
aspx 代码
<asp:DropDownList ID="DDSubOffCity" OnSelectedIndexChanged='GetSelectedCityValue' AutoPostBack="false" runat="server" Width="355px"> <asp:ListItem Value="0">Select</asp:ListItem> </asp:DropDownList>
<asp:DropDownList ID="DDSubOffCountry" runat="server" OnSelectedIndexChanged='GetSelectedCountryValue' AutoPostBack="true" Width="355px"> <asp:ListItem Value="0">Select</asp:ListItem> </asp:DropDownList>
数据列表项绑定背后的代码
DDSubOffCountry.Attributes.Add("onChange", "javascript:NewBufferDDCountryValue('" + DDSubOffCountry.ClientID + "','" + DDSubOffCountry.SelectedValue + "');");
DDSubOffCity.Attributes.Add("onChange", "javascript:NewBufferDDCityValue('" + DDSubOffCity.ClientID + "', '" + DDSubOffCity.SelectedValue + "');");}
javascript代码
function NewBufferDDCountryValue(objddCountry)
{
try {
if (objddCountry != 'null') {
var ctlDDl = objddCountry.toString();
var parts = ctlDDl.split('_', 4);
var indexId = parts[3];
var objHidden = document.getElementById('ContentPlaceHolder1_DataListOffices_hiddenDDSubOffCountry_' + indexId);
objHidden.value = document.getElementById(objddCountry).value;
}
} catch (e) { alert(e); }
}
function NewBufferDDCityValue(objddCity)
{
try {
if (objddCity != 'null') {
var ctlDDl = objddCity.toString();
var parts = ctlDDl.split('_', 4);
var indexId = parts[3];
var objHidden = document.getElementById('ContentPlaceHolder1_DataListOffices_hiddenDDSubOffCity_' + indexId);
objHidden.value = document.getElementById(objddCity).value;
}
} catch (e) { alert(e); }
}
当 onselectedindexchange 事件发生时将在后面的代码上调用的函数
public void GetSelectedCountryValue(object sender, EventArgs e)
{
DropDownList drp = (DropDownList)sender;
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "javascript:NewBufferDDCountryValue('" + drp.ClientID + "');", true);
var city = new City();
string indexer = drp.ClientID;
indexer=indexer.Split(Convert.ToChar("_"))[3];
string senderCity ="ContentPlaceHolder1_DataListOffices_DDSubOffCity_" + indexer;
DropDownList drpCity;
/*****************/
如果其客户端 ID 仅为已知,如何查找控件。或如何将字符串转换为要填充的 dropdownCity drpCity = FindControl(senderCity);
/********************/
city.populateCity(drpCity, drp.SelectedValue);
}
在 GetSelectedCountryValue 函数中,您可以看到我有 dropdownCountry 的索引,现在我知道控件的名称,但无法弄清楚如何仅通过其 ClientId 找到该控件,任何帮助表示赞赏。