我有两个下拉列表 ddlCountry 和 ddlState 以及一个按钮提交。
虽然 ddlstate 在更新面板中。
在提交数据库中两个下拉存储的按钮值时。
我在中继器控件中显示数据。(在 HTML 表结构中)ASPX 代码
<table id="tablelist" class="csstablelist" cellspacing="1" cellpadding="1">
<tr>
<td class="csstablelisttoptr">
ddlCountryID
</td>
<td class="csstablelisttoptr">
ddlCountryText
</td>
<td class="csstablelisttoptr">
ddlstateText
</td>
</tr>
<asp:Repeater ID="repeaterList" runat="server" OnItemDataBound="repeaterList_ItemDataBound">
<ItemTemplate>
<tr onclick="selectRow(this);">
<td class="csstablelisttd" style="display: none">
<asp:Label ID="ddlCountryID" runat="server" Text='<%#Eval("ddlCountryID")%>'></asp:Label>
</td>
<td class="csstablelisttd">
<asp:Label ID="ddlCountryText" runat="server" Text='<%#Eval("ddlCountryText")%>'></asp:Label>
</td>
<td class="csstablelisttd">
<asp:Label ID="ddlstateText" runat="server" Text='<%#Eval("ddlstateText")%>'></asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
<asp:DropDownList ID="ddlCountry" runat="server" CssClass="csstextbox" Width="207px" AutoPostBack="true" OnSelectedIndexChanged="ddlCountry_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" />
ddlCountryID | ddlCountryText | ddlstateText
1 | USA | XYZ
2 |India | PQR
我在下面写的 TR 的 Onclick (SelectRow(this)) 函数在 javascript 中突出显示转发器值和下拉值匹配并被选中。
<script type="text/javascript">
function selectRow(objTR)
{
var ddlCountry =document.getElementById('<%=ddlCountry.ClientID %>');
var ddlState =document.getElementById('<%=ddlState .ClientID %>');
for (i = 0; i < ddlCountry .options.length; i++)
{
if (ddlCountry .options[i].text == objTR.cells[1].innerText.trim())
break;
}
ddlCountry .options[i].selected = true;
__doPostBack(ddlCountry .id, objTR.cells[2].innerText.trim());
}
</script>
我在后面的代码中写了 ddlCountry SelectedIndexChangedEvent。
从 Javascript 我正在触发 __doPostBack() 并将 ddlCountry 作为事件目标 ddlStateText 作为事件参数传递给 SelectedIndexChangedEvent 并在这样的事件中获取值。
string stateDescription = Request["__EVENTARGUMENT"];
ddlState .Items.FindByText(stateDescription ).Selected = true;//for highliting the repeater value and dropdown value match and selected
pageLoad上的绑定国家
if(!Ispostback)
protected Void BindCountry()
{
strSQL = @"SELECT countryID,Country from Country_Master";
DataTable dataTableCountry = null;
dataTableCountry = objSqlDbComm.ExecuteDatasetQuery(strSQL).Tables[0];
int countryID;
string Country;
var dictioneryCountry = new Dictionary<int, string>();
foreach(DataRow dr in dataTableCountry.Rows)
{
countryID = Convert.ToInt32(dr["countryID"]);
Country= dr["Country"].ToString();
dictioneryCountry.Add(countryID,Country);
}
ddlCountry.Items.Clear();
ddlCountry.DataTextField = "Value";
ddlCountry.DataValueField = "Key";
ddlCountry.DataSource = dictioneryCountry;
ddlCountry.DataBind();
ddlCountry.Items.Insert(0, new ListItem("[Select]", "-1"));
ddlCountry.Items[0].Selected = true;
}
我的问题是我是否有以下转发器数据。
ddlCountryID | ddlCountryText | ddlstateText
1 | USA | XYZ
2 |India | PQR
2 |India | MNO
当我选择具有国家印度和国家 mno 的第 3 行时,__dopostback() 方法被触发。
当我转到第 1 行时,__dopostback() 方法被触发。
当我从第 1 行到第 3 行时,方法是正确的触发方式,但是当从第 3 行到第 2 行具有相同的国家/地区 id 时,__dopostback() 方法不会触发并且未从 ddlstate 中选择状态。