我使用下面的代码填充一个ASP.Net下拉列表,其中包含来自Ajax调用Json Response的值。
$.ajax({
type: "POST",
url: "Default.aspx/SelectMoveToFund",
data: JSON.stringify(params),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
$('select').select2('destroy');
var ddl = $("#" + fundId);
ddl.empty().append('<option selected="selected" value="0">Please select</option>');
$.each(data.d, function () {
ddl.append($("<option></option>").val(this['Value']).html(this['Text']));
});
$('select').select2();
},
error: function (data) {
console.log("error :" + data);
console.log(data);
}
});
这适用于填充ASP.Net Webforms页面上数据网格中保存的目标下拉列表。
我遇到的问题是当页面回发并迭代遍历数据网格的每一行时,它不会传递列表中所选项目的值。
For Each item As DataGridItem In dgUnsuitableSwitch.Items
Dim ddlFund As DropDownList = DirectCast(item.FindControl("ddlFund"), DropDownList)
Next
Datagrid是这样组成的,其中一部分是从已知数据的数据表中填充的,其余部分由客户信息组成。
<asp:DataGrid runat="server" CssClass="tab table table-bordered table-striped table-hover " ID="dgUnsuitableSwitch" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="Fund Name" HeaderText="Fund Name" HeaderStyle-CssClass="centre" ItemStyle-CssClass="centre"></asp:BoundColumn>
<asp:BoundColumn DataField="Fund Risk Profile" HeaderText="Fund Risk Profile" HeaderStyle-CssClass="centre" ItemStyle-CssClass="centre"></asp:BoundColumn>
<asp:BoundColumn DataField="Customer Name" HeaderText="Customer Name" HeaderStyle-CssClass="centre" ItemStyle-CssClass="centre"></asp:BoundColumn>
<asp:BoundColumn DataField="Investment Date" HeaderText="Investment Date" HeaderStyle-CssClass="centre" ItemStyle-CssClass="centre"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Years Held" HeaderStyle-CssClass="centre" ItemStyle-CssClass="centre">
<ItemTemplate>
<asp:TextBox runat="server" CssClass="form-control" placeholder="Years" ID="txtUsYears"></asp:TextBox>
<ajaxToolkit:FilteredTextBoxExtender ID="filtertxtYears" runat="server"
Enabled="True" TargetControlID="txtUsYears" FilterType="Custom" ValidChars="0123456789"></ajaxToolkit:FilteredTextBoxExtender>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Amount (£)" HeaderStyle-CssClass="centre" ItemStyle-CssClass="centre">
<ItemTemplate>
<asp:TextBox runat="server" CssClass="form-control" placeholder="Amount" ID="txtUsAmount"></asp:TextBox>
<ajaxToolkit:FilteredTextBoxExtender ID="filtertxtsAmount" runat="server"
Enabled="True" TargetControlID="txtUsAmount" FilterType="Custom" ValidChars=".-0123456789"></ajaxToolkit:FilteredTextBoxExtender>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Switching to Product" HeaderStyle-CssClass="centre" ItemStyle-CssClass="centre">
<ItemTemplate>
<asp:DropDownList runat="server" Width="200px" CssClass="centre ddlMoveToProduct" ID="ddlProduct">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Switching to Fund" HeaderStyle-CssClass="centre ddlMoveToFund" ItemStyle-CssClass="centre">
<ItemTemplate>
<asp:DropDownList runat="server" Width="200px" CssClass="centre ddlFund" ID="ddlFund">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Switching to Amount (£)" HeaderStyle-CssClass="centre" ItemStyle-CssClass="centre">
<ItemTemplate>
<asp:TextBox runat="server" CssClass="form-control" placeholder="Amount" ID="txtSwitchToAmount"></asp:TextBox>
<ajaxToolkit:FilteredTextBoxExtender ID="filtertxtSwitchToAmount" runat="server"
Enabled="True" TargetControlID="txtSwitchToAmount" FilterType="Custom" ValidChars=".-0123456789"></ajaxToolkit:FilteredTextBoxExtender>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Switching to Lump Sum Amount (£)" HeaderStyle-CssClass="centre" ItemStyle-CssClass="centre">
<ItemTemplate>
<asp:TextBox runat="server" CssClass="form-control" placeholder="Amount" ID="txtSwitchToLumpSumAmount"></asp:TextBox>
<ajaxToolkit:FilteredTextBoxExtender ID="filtertxtSwitchToLumpSumAmount" runat="server"
Enabled="True" TargetControlID="txtSwitchToLumpSumAmount" FilterType="Custom" ValidChars=".-0123456789"></ajaxToolkit:FilteredTextBoxExtender>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
非常感谢任何和所有帮助。
由于
西蒙
答案 0 :(得分:0)
我现在找到了一个有效的解决方案,
Protected Sub ddlProduct_Change(sender As Object, e As EventArgs)
Dim ddl As DropDownList = DirectCast(sender, DropDownList)
Dim rowdIdx = Convert.ToInt16(ddl.ClientID.Remove(0, ddl.ClientID.Length - 1))
Dim ddlFund As DropDownList = CType(dgUnsuitableSwitch.Items(rowdIdx).Cells(0).FindControl("ddlUsFund"), DropDownList)
Dim dt = GetFundsToMoveTo(ddl.SelectedValue)
For Each row As DataRow In dt.Rows
ddlFund.Items.Add(row(0).ToString())
Next
End Sub