目前我有一个绑定到集合的gridview。在'RowCreated'事件中,我正在操纵html(向某些字段添加删除线)。
这一切都适用于页面加载,但每当我对一个列进行排序时,RowCreated事件似乎都没有被触发。
我该如何解决这个问题?
编辑:这是我在RowCreated方法中所做的事情。
protected void gvResults_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.DataItem != null)
{
Property currentProperty = (Property)e.Row.DataItem;
e.Row.Attributes.Add("id", currentProperty.Id.ToString());
Document propertyDoc = new Document(currentProperty.Id);
if (currentProperty.Price == 0)
{
e.Row.Cells[5].Controls.Clear();
e.Row.Cells[5].Controls.Add(new Literal() { Text = "N/A" });
}
if (propertyDoc.getProperty("floorPlan").Value == null || propertyDoc.getProperty("floorPlan").Value.ToString() == "")
{
e.Row.Cells[6].Controls.Clear();
e.Row.Cells[6].Controls.Add(new Literal() { Text = "View" });
}
if (propertyDoc.getProperty("propertyImage").Value == null || propertyDoc.getProperty("propertyImage").Value.ToString() == "")
{
e.Row.Cells[7].Controls.Clear();
e.Row.Cells[7].Controls.Add(new Literal() { Text = "View" });
}
if (currentProperty.Reserved == true)
{
e.Row.ForeColor = System.Drawing.ColorTranslator.FromHtml("#999999");
e.Row.Cells[1].Text = currentProperty.DevelopmentName;
e.Row.Cells[2].Text = currentProperty.LocationDisplayText;
e.Row.Cells[3].Text = currentProperty.PropertyTypeName;
e.Row.Cells[4].Text = currentProperty.BedroomsDisplayText;
e.Row.Cells[5].Controls.Clear();
e.Row.Cells[5].Controls.Add(new Literal() { Text = "RESERVED" });
//e.Row.Cells[6].Controls.Clear();
//e.Row.Cells[6].Controls.Add(new Literal() { Text = "<del>View</del>" });
//e.Row.Cells[7].Controls.Clear();
//e.Row.Cells[7].Controls.Add(new Literal() { Text = "<del>View</del>" });
}
else if (currentProperty.Sold == true)
{
e.Row.ForeColor = System.Drawing.ColorTranslator.FromHtml("#999999");
e.Row.Cells[1].Text = currentProperty.DevelopmentName;
e.Row.Cells[2].Text = currentProperty.LocationDisplayText;
e.Row.Cells[3].Text = currentProperty.PropertyTypeName;
e.Row.Cells[4].Text = currentProperty.BedroomsDisplayText;
e.Row.Cells[5].Controls.Clear();
e.Row.Cells[5].Controls.Add(new Literal() { Text = "SOLD" });
//e.Row.Cells[6].Controls.Clear();
//e.Row.Cells[6].Controls.Add(new Literal() { Text = "<del>View</del>" });
//e.Row.Cells[7].Controls.Clear();
//e.Row.Cells[7].Controls.Add(new Literal() { Text = "<del>View</del>" });
}
else if (currentProperty.Released == true)
{
e.Row.ForeColor = System.Drawing.ColorTranslator.FromHtml("#999999");
e.Row.Cells[1].Text = currentProperty.DevelopmentName;
e.Row.Cells[2].Text = currentProperty.LocationDisplayText;
e.Row.Cells[3].Text = currentProperty.PropertyTypeName;
e.Row.Cells[4].Text = currentProperty.BedroomsDisplayText;
e.Row.Cells[5].Controls.Clear();
e.Row.Cells[5].Controls.Add(new Literal() { Text = "TO BE RELEASED" });
//e.Row.Cells[6].Controls.Clear();
//e.Row.Cells[6].Controls.Add(new Literal() { Text = "<del>View</del>" });
//e.Row.Cells[7].Controls.Clear();
//e.Row.Cells[7].Controls.Add(new Literal() { Text = "<del>View</del>" });
}
}
}
}
这是前端代码:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="FullSearchResults.ascx.cs" Inherits="MyControls.FullSearchResults" %>
<div id="search-results">
<asp:GridView ID="gvResults" runat="server" AutoGenerateColumns="false" DataSourceID="odsProperties"
AllowSorting="true" Width="700px" OnRowCommand="gvResults_RowCommand" OnRowCreated="gvResults_RowCreated">
<HeaderStyle CssClass="gv-header" ForeColor="#ffffff" />
<RowStyle BackColor="White" ForeColor="#333333" />
<AlternatingRowStyle BackColor="#e6e6e6" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="Plot" HeaderText="Plot" SortExpression="Plot" />
<asp:TemplateField HeaderText="Development" SortExpression="Development">
<ItemTemplate>
<%# Eval("DevelopmentName").ToString() %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location" SortExpression="Location">
<ItemTemplate>
<%# Eval("LocationDisplayText").ToString() %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Type" SortExpression="Type">
<ItemTemplate>
<%# Eval("PropertyTypeName").ToString() %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Beds" SortExpression="Bedrooms">
<ItemTemplate>
<%# Eval("BedroomsDisplayText").ToString() %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price" SortExpression="Price">
<ItemTemplate>
<%# Eval("PriceDisplayText").ToString() %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Plans">
<ItemTemplate>
<asp:LinkButton ID="btnViewFloorplans" runat="server" CommandName="View" Text="View"
CommandArgument='<%# (int)Eval("Id") %>' CssClass="view-floorplan" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:LinkButton ID="btnViewImage" runat="server" CommandName="View" Text="View"
CommandArgument='<%# (int)Eval("Id") %>' CssClass="view-floorplan" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="odsProperties" runat="server" TypeName="MyControls.DataAccess"
SelectMethod="GetSearchResultsForAllDevelopments" SortParameterName="_sortExpression">
<SelectParameters>
<asp:QueryStringParameter Name="_county" QueryStringField="loc" Type="Int16" DefaultValue="-1" />
<asp:QueryStringParameter Name="_bedrooms" QueryStringField="rooms" Type="Int16"
DefaultValue="-1" />
<asp:QueryStringParameter Name="_price" QueryStringField="price" Type="Int16" DefaultValue="-1" />
<asp:QueryStringParameter Name="_propertyType" QueryStringField="type" Type="Int16"
DefaultValue="-1" />
</SelectParameters>
</asp:ObjectDataSource>
</div>
答案 0 :(得分:1)
考虑使用RowDataBound
事件代替RowCreated
事件来执行HTML自定义(即向某些字段添加删除线)。
每当网格视图反弹时(即RowDataBound
被调用),都会调用GridView.DataBind()
事件。 RowDataBound
事件也可以访问行中呈现的实际数据,因此通常是人们想要做的事情,因为大多数行更改都与该行中包含的数据相关。
RowCreated
主要关注构建行的结构(即网格标记中定义的控件),并在页面Init
上触发并回发。
RowDataBound
主要关注将数据源中的数据绑定到RowCreated
事件中创建的控件。