vb.net中有一个下表,用于显示使用网格视图控件创建的帐户。
如您所见,我使用RowDataBound
更改标题颜色并隐藏编辑和删除。
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
Try
For i As Integer = 0 To Me.GridView1.Rows.Count - 1
If (TryCast(GridView1.Rows(i).FindControl("hflvl"), HiddenField).Value = 2) Then
Me.GridView1.Rows(i).BackColor = Drawing.Color.LightBlue
Me.GridView1.Rows(i).Cells(2).Text = ""
Me.GridView1.Rows(i).Cells(3).Text = ""
Else
Me.GridView1.Rows(i).BackColor = Drawing.Color.White
End If
Next
Catch ex As Exception
'lblMsg.Text = ex.Message & ex.StackTrace
End Try
End Sub
一切正常,下面是编辑代码
Protected Sub GridView1_RowEditing(sender As Object, e As GridViewEditEventArgs) Handles GridView1.RowEditing
GridView1.EditIndex = e.NewEditIndex
PopTransDataGrid()
End Sub
Protected Sub GridView1_RowCancelingEdit(sender As Object, e As GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
GridView1.EditIndex = -1
PopTransDataGrid()
End Sub
但是当我单击一行中的“编辑”时,该编辑行下的所有行都将使用默认样式,并且行数据绑定代码无效。
甚至删除,并且显示主要帐户的修改。
这是我的gridview代码,
<asp:GridView ID="gvCustomerDeplst" runat="server" AutoGenerateColumns="False"
DataKeyNames="AccountNo" Width="99%" BackColor="White" BorderColor="#CCCCCC"
BorderStyle="Solid" BorderWidth="1px" CellPadding="4" ForeColor="Black" ShowFooter="True" PageSize="50" Font-Names="Verdana"
Font-Size="11px">
<AlternatingRowStyle BackColor="#f2f2f0" />
<Columns>
<asp:TemplateField HeaderText="AccountName" SortExpression="AccountName">
<EditItemTemplate>
<asp:TextBox ID="txtAccountName" runat="server" Height="19px" Text='<%# Bind("AccountName")%>' Width="200px"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("AccountName")%>'></asp:Label>
<asp:HiddenField ID="hflvl" Value='<%# Bind("lvl")%>' runat="server" />
</ItemTemplate>
<FooterStyle HorizontalAlign="Left" />
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:BoundField DataField="Category" HeaderText="Category" ReadOnly="True">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="return confirm('Are you sure want to delete?')" Text="Delete"
CommandArgument='<%# Container.DataItemIndex %>' CommandName="Delete" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True">
<ItemStyle ForeColor="#0066FF" Width="100px" />
</asp:CommandField>
<asp:BoundField DataField="lvl" HeaderText="Lvl" ReadOnly="True">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
</Columns>
<FooterStyle BackColor="#006699" ForeColor="White" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Left" />
<RowStyle BackColor="White" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#4B4B4B" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#242121" />
</asp:GridView>
如何在不丢失样式的情况下编辑gridview?
答案 0 :(得分:1)
这是因为应用了条件样式,条件是HiddenField
事件中的RowDataBound
存在,值为2,但是,在ASPX页中,HiddenField
仅出现在ItemTemplate
中,而不出现在EditItemTemplate
中,因此当数据绑定在EditItemTemplate
内时,事件将以Else
条件应用样式,因为它找不到HiddenField
。
编辑:澄清一下-HiddenField
必须在EditItemTemplate
中,事件才能正确应用样式,例如根据您的ASPX:
<EditItemTemplate>
<asp:TextBox ID="txtAccountName" runat="server" Height="19px" Text='<%# Bind("AccountName")%>' Width="200px"></asp:TextBox>
<asp:HiddenField ID="hflvl" Value='<%# Bind("lvl")%>' runat="server" />
</EditItemTemplate>