GridView RowDataBound not called after postback

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • danc

    GridView RowDataBound not called after postback

    I am trying to modify the layout of a row based on the value in some data (I
    am creating a subtotal line). This works fine when the page is first loaded,
    but when it's reloaded, the wrong column is restored.

    <asp:GridView ID="grdvOrder" runat="server" AutoGenerateCol umns="False"
    OnRowCommand="g rdvOrder_RowCom mand" OnRowDataBound= "grdvOrder_RowD ataBound"
    OnRowCreated="g rdvOrder_RowCre ated">
    <Columns>
    <asp:TemplateFi eld ShowHeader="Fal se">
    <ItemTemplate >
    <asp:ImageButto n ID="ImageButton 1" runat="server"
    CausesValidatio n="false" CommandName="De leteLine"
    ImageUrl="~/Images/btn_delete.gif" Text="" />
    </ItemTemplate>
    </asp:TemplateFie ld>
    <asp:TemplateFi eld HeaderText="Qua ntity">
    <ItemTemplate >
    <asp:TextBox ID="txtQty" runat="server" Columns="4"
    Text='<%# Bind("QtyOrdere d") %>'></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateFie ld>
    <asp:HyperLinkF ield DataNavigateUrl Fields="Sku"
    DataNavigateUrl FormatString="p roduct/{0}"
    DataTextField=" ProductName" HeaderText="Pro duct" />
    <asp:BoundFie ld DataField="Prod uctId" HeaderText="Id" />
    <asp:BoundFie ld DataField="Pric e" HeaderText="Pri ce"
    DataFormatStrin g="{0:c}" HtmlEncode="Fal se" />
    <asp:BoundFie ld DataField="ExtP rice" HeaderText="Ext . Price"
    DataFormatStrin g="{0:c}" HtmlEncode="Fal se" />
    </Columns>
    </asp:GridView>


    protected void grdvOrder_RowDa taBound(object sender, GridViewRowEven tArgs e)
    {
    if (e.Row.RowType == DataControlRowT ype.DataRow)
    {
    // Check for sub-total line
    DataRowView rowView = (DataRowView)e. Row.DataItem;
    if (Convert.ToInt3 2(rowView["OrderLineI d"]) == 0)
    {
    // Remove columns 0, 1, 3, 4
    e.Row.Cells.Rem oveAt(0);
    e.Row.Cells.Rem oveAt(0);
    e.Row.Cells.Rem oveAt(1);
    e.Row.Cells.Rem oveAt(1);
    // Make column 2 span columns 1-4
    e.Row.Cells[0].ColumnSpan = 5;
    // Remove the hyperlink from the subtotal line
    HyperLink lnk = (HyperLink) e.Row.Cells[0].Controls[0];
    lnk.NavigateUrl = null;
    }
    }
    }

    On postback, RowDataBound is not called and the columns are all wrong.

    I tried the RowCreated event, which did get called on postbacks, but then
    the HyperLink control was not defined so setting the NavigateUrl to null had
    no effect, and on postback got a null for (DataRowView)e. Row.DataItem,
    causing an exception.

    Any recommendations on how I can adjust the contents based on the data in
    the row?

    Thanks in advance,

    Dan
Working...