dropdownlist选择值的时候,靠,明明选择的是第三项, 点击后回发下,就变成了第二项了
贴代码吧
这是前台代码,直接用的数据源绑定
<asp:DropDownList ID="DDLpro" runat="server" DataSourceID="SqlDataSource2" DataTextField="PSNAME"
DataValueField="PID" Width="140px" οnchange="SetPId(this);">
</asp:DropDownList>
<input id="hSelectIndex" type="hidden" runat="server" />
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand='SELECT "PSNAME", "PID" FROM "BSC_PRODUCTSPEC" WHERE ("ACTIVEFLAG" = :ACTIVEFLAG)'>
<SelectParameters>
<asp:Parameter DefaultValue="2" Name="ACTIVEFLAG" Type="Decimal" />
</SelectParameters>
</asp:SqlDataSource>
利用JavaScript可以解决,
先要加上一个 <input id="hSelectIndex" type="hidden" runat="server" />隐藏控件
如下是js代码:
onchage="SetPid(this)"
function SetPId(item){
document.getElementById("<%=hSelectIndex.ClientID %>").value=item.selectedIndex;
document.getElementById('<%=txtpid.ClientID %>').value=item.options[item.selectedIndex].value;
}
然后再后台代码协上
if (!string.IsNullOrEmpty(this.hSelectIndex.Value))
{
this.DDLpro.SelectedIndex = Convert.ToInt32(this.hSelectIndex.Value);
}
ddlpro是我的dropdownlist控件名: