Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 23rd, 2007, 12:49 PM
Authorized User
 
Join Date: Sep 2006
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default Gridview Alt rows not using RowDataBound logic

Hello all!

I have what seems like a bit of an odd situation:

I am working on a gridview that is being used to both display & edit information. Within the gridview, I have several templated columns, one of which has a dropdownlist control that I am prepopulating and in which I need to have the correct value selected when the user clicks the "Edit" button for the row.

I have managed to get this working 50% of the time. That is: When the user clicks the "main" row, the logic is used, the dropdownlist is displayed with the appropriate entry preselected. However, when the same action is taken on an alternating row, the logic is ignored. I am *not* using an alternating row template for the alternating rows. In fact, the only indication that the rows are different is that I have the AlternatingRowStyle set to show a different background color.

In all cases, when the gridview is in normal mode, the correct information is displayed for the column. Therefore, I can only deduce that there is something "hinky" going on with the Gridview's RowDataBound functionality.

Any ideas on how I can get this working consistently?

I will post my code in the next posting in this thread for review.

Phil

 
Old January 23rd, 2007, 12:51 PM
Authorized User
 
Join Date: Sep 2006
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is the code being used for the Gridview itself:

    <asp:GridView ID="sqlDetailedView" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="sdsDetailedView" Width="590px">
      <Columns>
        <asp:BoundField DataField="InspectionID" ShowHeader="False" ReadOnly="True" SortExpression="InspectionID">
          <HeaderStyle Font-Underline="False" />
          <ControlStyle ForeColor="Transparent" />
          <ItemStyle Font-Size="XX-Small" ForeColor="White" Width="1px" />
        </asp:BoundField>
        <asp:BoundField DataField="PolicyNum" HeaderText="Policy #" ReadOnly="True" SortExpression="PolicyNum" />
        <asp:BoundField DataField="CoverageA" HeaderText="Cov. A" ReadOnly="True" SortExpression="CoverageA" />
        <asp:TemplateField HeaderText="Date Ordered" SortExpression="DateOrdered">
          <EditItemTemplate>
            <asp:TextBox ID="txtDateOrdered" runat="server" Text='<%# Bind("DateOrdered") %>'></asp:TextBox>
          </EditItemTemplate>
          <ControlStyle Font-Names="Arial" Font-Size="8pt" Width="60px" />
          <ItemTemplate>
            <asp:Label ID="lblDateOrdered" runat="server" Text='<%# Bind("DateOrdered") %>'></asp:Label>
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Inspection Company" SortExpression="InspectionCompany">
          <EditItemTemplate>
            <asp:DropDownList ID="ddlInspectionCoID" runat="server" Width="80px" Font-Size="8pt" Font-Names="Arial" DataSourceID="dsInspectionCompany" DataTextField="InspectionCompany" DataValueField="InspectionCoID" />
            <asp:HiddenField ID="hdnInspectionCoID" runat="server" Value='<%# Bind("InspectionCoID") %>' />
          </EditItemTemplate>
          <ItemTemplate>
            <asp:Label ID="lblInspectionCoID" runat="server" Text='<%# Bind("InspectionCompany") %>'></asp:Label>
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Rush" SortExpression="IsRush">
          <EditItemTemplate>
            <asp:CheckBox ID="chkIsRush" runat="server" Checked='<%# Bind("IsRush") %>' />
          </EditItemTemplate>
          <ItemTemplate>
            <asp:CheckBox ID="lblIsRush" runat="server" Checked='<%# Bind("IsRush") %>' Enabled="false" />
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="High Value" SortExpression="IsHighValue">
          <EditItemTemplate>
            <asp:CheckBox ID="chkIsHighValue" runat="server" Checked='<%# Bind("IsHighValue") %>' />
          </EditItemTemplate>
          <ItemTemplate>
            <asp:CheckBox ID="chkIsHighValue" runat="server" Checked='<%# Bind("IsHighValue") %>' Enabled="false" />
          </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Reorder" HeaderText="Reorder" ReadOnly="True" SortExpression="Reorder" />
          <asp:TemplateField HeaderText="Notes" SortExpression="Notes">
            <ItemTemplate>
              <asp:Label ID="txtNotes" runat="server" Font-Size="8pt" Font-Names="Arial" Width="150px" Enabled="false" Text='<%# Eval("Notes")%>' />
            </ItemTemplate>
            <EditItemTemplate>
              <asp:TextBox ID="txtNotes" runat="server" Font-Size="8pt" Font-Names="Arial" Width="125px" Text='<%# Bind("Notes")%>' TextMode="MultiLine" Rows="4" />
            </EditItemTemplate>
            <ItemStyle HorizontalAlign="Left" VerticalAlign="Top" />
          </asp:TemplateField>
        <asp:CommandField ShowEditButton="True" ButtonType="Button">
          <ControlStyle Font-Size="8pt" />
          <ItemStyle VerticalAlign="Top" />
        </asp:CommandField>
      </Columns>
      <RowStyle HorizontalAlign="Center" VerticalAlign="Top" />
      <AlternatingRowStyle BackColor="#FFFFC0" HorizontalAlign="Center" VerticalAlign="Middle" />
      <HeaderStyle BackColor="#E0E0E0" Font-Underline="False" />
      <EditRowStyle HorizontalAlign="Center" VerticalAlign="Top" />
    </asp:GridView>

...and here is what I have in my code-behind:

    Protected Sub sqlDetailedView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles sqlDetailedView.RowDataBound

        If (e.Row.RowState = DataControlRowState.Edit) Then
            Dim ICID As DropDownList = CType(e.Row.FindControl("ddlInspectionCoID"), DropDownList)
            Dim InspCompHdn As HiddenField = CType(e.Row.FindControl("hdnInspectionCoID"), HiddenField)
            ICID.Items.Insert(0, New ListItem("", ""))
            ICID.SelectedValue = InspCompHdn.Value
            lblMessages.Text = InspCompHdn.Value
        End If

    End Sub

 
Old January 24th, 2007, 01:57 PM
Authorized User
 
Join Date: Sep 2006
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

An additional issue I am having with this same gridview is that if I choose a dropdownlist item & click the "Update" button, the change is not being passed to the stored proc that is handling the Update side of my datasource. The following sub is handling the update:

    Protected Sub sqlDetailedView_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs ) Handles sqlDetailedView.RowCommand

        If e.CommandName = "Update" Then

            Dim index As Integer = Convert.ToInt32(e.CommandArgument)
            Dim CurrRow As GridViewRow = sqlDetailedView.Rows(index)
            Dim IID As Integer = CurrRow.Cells(0).Text ' ID of the Inspection being ordered.
            Dim DateOrdered As TextBox = CurrRow.Cells(3).FindControl("txtDateOrdered") ' The Date Ordered entered by the user.
            Dim InspComp As DropDownList = CurrRow.Cells(4).FindControl("ddlInspectionCoID") ' ID of the selected Inspection Company.
            Dim Rush As CheckBox = CurrRow.Cells(5).FindControl("chkIsRush")
            Dim HighVal As CheckBox = CurrRow.Cells(6).FindControl("chkIsHighValue")
            Dim Notes As TextBox = CurrRow.Cells(8).FindControl("txtNotes")

            sdsDetailedView.UpdateParameters.Clear()

            With sdsDetailedView.UpdateParameters
                .Add("InspectionID", TypeCode.Int32, IID)
                .Add("strModifiedBy", TypeCode.String, Page.User.Identity.Name)
                .Add("InspectionCoID", TypeCode.Int32, InspComp.SelectedItem.Value)
                .Add("DateOrdered", TypeCode.DateTime, DateOrdered.Text)
                .Add("IsRush", TypeCode.Boolean, Rush.Checked)
                .Add("IsHighValue", TypeCode.Boolean, HighVal.Checked)
                .Add("Notes", TypeCode.String, Notes.Text)
            End With

            sdsDetailedView.Update()

            ' lblMessages.Text = Convert.ToInt32(InspComp.SelectedItem.Value)

            lblMessages.CssClass = "informative"

        End If

    End Sub

Anyone have any ideas on what might be going on here?

 
Old January 25th, 2007, 12:18 PM
Authorized User
 
Join Date: Sep 2006
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, I finally have a solution for both of the issues described above.

Issue 1: it seems that the Gridview control's edit mode actually has TWO slightly different edit modes: one for the normal row & the other for the alternating row. In order to get the logic in my code below to work for both types of edit rows, the "if" statement in the code-behind needs to be changed to

If (e.Row.RowState = DataControlRowState.Edit) Or (e.Row.RowState = DataControlRowState.Alternate + DataControlRowState.Edit) Then

so that the gridview knows that the row being worked on is both an alternate row AND that it is in edit mode. (Don't ask me why it needs this, but it does.)

Issue 2: The actual databound control in the edititemtemplate (the hidden field control) still has the original value being passed in to the GV & that is what is being passed back to the Update procedure. If I update that control's value with the selected item value from the dropdown list, this works correctly.

Phil

 
Old February 16th, 2007, 12:29 AM
Registered User
 
Join Date: Jun 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the update Phil, you killed two of my birds with one stone!

 
Old March 22nd, 2007, 05:28 PM
Registered User
 
Join Date: Mar 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Phil - I was having the same problem with the alternate rows not processing code in the rowdatabound event.

What a complete balls-up by Microsoft. Regardless of whether it's an alternate row or not, it's either in edit or it's not! Argh!

And the syntax for the workaround seems very odd. You would think it would be ... (e.Row.RowState = DataControlRowState.Alternate And e.Row.RowState = DataControlRowState.Edit) ...

I've come across lots of problems like this with the .net framework. Why do so many people seem to think it's so great? It's a disgrace is what it is.

When I went looking in the Microsoft documentation to try to find a solution, http://msdn2.microsoft.com/en-us/lib...lrowstate.aspx had this to say, "The Alternate state can be combined with other states, such as Normal, Edit, or Insert, at any time. These rows might be affected by the AlternateRowStyle property of the data control, if set.". What the hell does that mean? These rows "MIGHT" be affected by the AlternateRowStyle property...?!?!?! That would be a good place for MS to provide the workaround code, but no mention of it on that page. Incidentally, does anyone else get lots of javascript errors on that page? (And all the other similar docs pages)

I just think that the .Net framework makes classic ASP look good.

Anyway, thanks again Phil for providing the solution.

Cheers,

Danik





Similar Threads
Thread Thread Starter Forum Replies Last Post
Editing all GridView rows Abbas ASP.NET 2.0 Professional 4 March 3rd, 2012 02:19 PM
Gridview and inserting rows thewoodchuck BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9 0 April 10th, 2008 09:55 AM
Highlight specific rows in a gridview rao965 ASP.NET 2.0 Professional 6 June 14th, 2007 03:20 AM
Editing all GridView rows Abbas C# 2005 0 November 29th, 2006 03:52 PM
Mixing Data access logic and business logic polrtex BOOK: Professional Jakarta Struts 0 December 15th, 2003 07:19 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.