我有一个绑定到车辆数据的表格视图。顾问将能够更改覆盖级别或从gridview中删除车辆,然后单击按钮以重新计算保费。这应该在不更改数据库的情况下即时进行。
其中一列是下拉列表。填充gridview之后,将在RowDataBound事件中填充下拉列表。在我需要为每个下拉菜单设置选定的值/文本之前,所有这些工作都很好-这些值来自数据表。我面临的问题是未设置所选值(尽管填充了正确的值)。
这是我所做的: Aspx:
<ajax:UpdatePanel ID="upVehicles" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
<ContentTemplate>
<table width="100%" cellspacing="2" cellpadding="0" border="0" summary="Step3" align="center">
<tr>
<td>
<asp:GridView ID="grdVehicles" runat="server" AutoGenerateColumns="False" Width="100%" CellPadding="0" GridLines="None" DataKeyNames="RecordNumber" OnRowDataBound="grdVehicles_RowDataBound">
<HeaderStyle CssClass="NormalBold" VerticalAlign="Middle" HorizontalAlign="Left" BackColor="#FFFF80" Font-Underline="True" Height="18px" />
<RowStyle CssClass="Normal" HorizontalAlign="Left" />
<AlternatingRowStyle CssClass="DataGrid_AlternateItem" />
<EditRowStyle CssClass="NormalTextBox" />
<SelectedRowStyle CssClass="NormalRed" />
<FooterStyle CssClass="DataGrid_Footer" />
<PagerStyle CssClass="DataGrid_Pager" />
<Columns>
<asp:TemplateField HeaderText="Remove">
<ItemTemplate>
<asp:CheckBox ID="cbRemove" runat="server" AutoPostBack="true" OnCheckedChanged="cbRemove_CheckedChanged" ToolTip="Vehicle to remove" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="RegNo" HeaderText="Reg No" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="VehicleYear" HeaderText="Year" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="Make" HeaderText="Make" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="Model" HeaderText="Model" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="Series" HeaderText="Series" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="vVariant" HeaderText="Variant" HeaderStyle-HorizontalAlign="Left" />
<asp:TemplateField HeaderText="Rental">
<ItemTemplate>
<asp:CheckBox ID="cbRental" Checked='<%# Eval("RentalAfterAccident") %>' runat="server" AutoPostBack="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cover Level">
<ItemTemplate> <%--SelectedValue='<%# Eval("CoverLevelType") %>'--%>
<asp:DropDownList ID="cbCoverLevel" Width="220px" AppendDataBoundItems="true" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
<br />
</table>
</ContentTemplate>
</ajax:UpdatePanel>
填充gridview:
Private Sub FillVehicleDetails(ByRef vehicleDetail As SSVehicle(), ByVal paymentFrequency As String)
Dim totPremium As Double = 0
Dim objDT As New DataTable("dtVehicles")
dtCoverLevelVehicles = objDT
ViewState("dtVehicles") = Nothing
objDT.Columns.Add("RecordNumber")
objDT.Columns.Add("RegNo")
objDT.Columns.Add("VehicleYear")
objDT.Columns.Add("Make")
objDT.Columns.Add("Model")
objDT.Columns.Add("Series")
objDT.Columns.Add("vVariant")
objDT.Columns.Add("RentalAfterAccident")
objDT.Columns.Add("CoverLevelType")
For Each vehicle In vehicleDetail
Dim dr As DataRow = objDT.NewRow
dr("RecordNumber") = vehicle.RecordNumber
dr("RegNo") = vehicle.RegNo
dr("VehicleYear") = vehicle.VehicleYear
dr("Make") = vehicle.Make
dr("Model") = vehicle.Model
dr("Series") = vehicle.Series
dr("vVariant") = vehicle.vVariant
dr("RentalAfterAccident") = vehicle.RentalAfterAccident
dr("CoverLevelType") = vehicle.Cover.ToString()
objDT.Rows.Add(dr)
totPremium += vehicle.PremiumDetail.TotalPremium
Next
ViewState("dtVehicles") = objDT
grdVehicles.DataSource = CType(ViewState("dtVehicles"), DataTable)
grdVehicles.DataBind()
SetVehicleOptions()
upVehicles.Update()
End Sub
Private Sub SetVehicleOptions()
Dim cbCheckBox As CheckBox
For i As Integer = 0 To grdVehicles.Rows.Count - 1
cbCheckBox = CType(grdVehicles.Rows(i).FindControl("cbRemove"), CheckBox)
cbCheckBox.Enabled = (grdVehicles.Rows.Count > 1)
cbCheckBox.ToolTip = "Remove Vehicle"
Next
End Sub
这是RowDataBound,并尝试关联选定的值:
Protected Sub grdVehicles_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim cboCoverLevelType As DropDownList = CType(e.Row.FindControl("cbCoverLevel"), DropDownList)
objLU.FillCombo("COVERLEVEL", cboCoverLevelType, True)
Dim recordNumber As String = grdVehicles.DataKeys(e.Row.RowIndex).Values(0).ToString()
Dim drRecordNumber As String
Dim drCoverLevel As String
'Check if DataTable has any rows
If dtCoverLevelVehicles.Rows.Count > 0 Then
'Loop through datatable
For i As Integer = 0 To dtCoverLevelVehicles.Rows.Count - 1
' check and compare here
drRecordNumber = dtCoverLevelVehicles.Rows(i).Item("RecordNumber").ToString()
If recordNumber = drRecordNumber Then
drCoverLevel = dtCoverLevelVehicles.Rows(i).Item("CoverLevelType").ToString()
'cboCoverLevel.Items.FindByText(drCoverLevel).Selected = True
'cboCoverLevel.SelectedValue = drCoverLevel
' cboCoverLevel.Text = drCoverLevel
' cboCoverLevel.SelectedValue = DataBinder.Eval(e.Row.DataItem, "CoverLevelType").ToString()
End If
Next
End If
End If
End Sub