Results 1 to 9 of 9

Thread: [RESOLVED] Gridview with find control question

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Resolved [RESOLVED] Gridview with find control question

    Dear all,
    I can set all of the target button to disable, but I cannot change it to enable however I think my code must be right! Does anyone face the same question before?

    aspx
    Code:
    <asp:GridView ID="gvEditEvent" OnRowEditing="gvEditEvent_Editing" runat="server" AutoGenerateColumns="False" CellPadding="4"
                                            DataSourceID="ObjectDataSource1" ForeColor="#333333" GridLines="None" OnRowUpdated="gvEditEvent_Updated" OnRowDataBound="gvEditEvent_RowDataBound">
                                            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                                            <RowStyle BackColor="#EFF3FB" />
                                            <Columns>
                                                <asp:TemplateField ShowHeader="False">
                                                   <ItemTemplate>  
                                                        <asp:LinkButton ID="btnEdit" runat="server" CausesValidation="False" CommandName="Edit"  
                                                            Text="Edit"></asp:LinkButton>
                                                        <asp:LinkButton ID="btnUpdate" runat="server" CausesValidation="False" CommandName="Update"
                                                            Text="Update"></asp:LinkButton>
                                                        <asp:LinkButton ID="btnCancel" Visible="false" runat="server" CausesValidation="False" CommandName="Cancel"
                                                            Text="Cancel"></asp:LinkButton>
                                                            <asp:HiddenField ID="hfID" runat="server" Value='<&#37;# Bind("ID") %>'/> 
                                                    </ItemTemplate>  
                                                </asp:TemplateField>           
                                                <asp:BoundField DataField="NAME" HeaderText="NAME" SortExpression="NAME" />                                        
                                                </Columns>                                        
                                            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                                            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                                            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                                            <EditRowStyle BackColor="#2461BF" />
                                            <AlternatingRowStyle BackColor="White" />
                                        </asp:GridView>
    .vb
    Code:
        
        Protected Sub gvEditEvent_Editing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
            mpeEdit.Show()
            CType(gvEditEvent.Rows(e.NewEditIndex).Cells(0).FindControl("btnUpdate"), LinkButton).Enabled = True
        End Sub
        
        Protected Sub gvEditEvent_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
            For i As Integer = 0 To gvEditEvent.Rows.Count - 1
                CType(gvEditEvent.Rows(i).Cells(0).FindControl("btnUpdate"), LinkButton).Enabled = False
            Next
        End Sub

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Gridview with find control question

    There are few things are to be noted here...

    There is no need to loop through the Gridview Rows in gvEditEvent_RowDataBound Event as the RowDataBound will be fired for each rows .. And this code could be shortened

    Code:
    Protected Sub gvEditEvent_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
            For i As Integer = 0 To gvEditEvent.Rows.Count - 1
                CType(gvEditEvent.Rows(i).Cells(0).FindControl("btnUpdate"), LinkButton).Enabled = False
            Next
        End Sub
    to

    Code:
    Protected Sub gvEditEvent_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
                CType(e.Row.FindControl("btnUpdate"), LinkButton).Enabled = False
        End Sub
    And
    Code:
    Protected Sub gvEditEvent_Editing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
            mpeEdit.Show()
            CType(gvEditEvent.Rows(e.NewEditIndex).Cells(0).FindControl("btnUpdate"), LinkButton).Enabled = True
        End Sub
    to

    Code:
        Protected Sub GridView1_RowEditing(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
            'Bind the Data
            'Set the Enable using
    
            CType(GridView1.Rows(e.NewEditIndex).FindControl("btnUpdate"), LinkButton).Enabled = True
    
    
        End Sub
    Please mark you thread resolved using the Thread Tools as shown

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Re: Gridview with find control question

    Thx for the advice!!
    but the debug tool tell me that CType(e.Row.FindControl("btnUpdate"), LinkButton).Enabled = False is NullReferenceException
    and even I change the code like
    Code:
    CType(gvEditEvent.FindControl("btnUpdate"), LinkButton).Enabled = False
    also, I have copied this code correctly to my vb
    Code:
    Protected Sub gvEditEvent_Editing(ByVal sender As Object, ByVal e As GridViewEditEventArgs) Handles gvEditEvent.RowEditing
            mpeEdit.Show()
            CType(gvEditEvent.Rows(e.NewEditIndex).FindControl("btnUpdate"), LinkButton).Enabled = True
        End Sub
    after I clicked edit link button, but my btnUpdate button cannot be set to enable again...any idea?

  4. #4
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Gridview with find control question

    Does the other part worked ?

    Code:
    Protected Sub gvEditEvent_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
                CType(e.Row.FindControl("btnUpdate"), LinkButton).Enabled = False
        End Sub
    Please mark you thread resolved using the Thread Tools as shown

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Re: Gridview with find control question

    no, this gives out NullReferenceException, is there something wrong? but unless I can prove there is an object by using for loop

  6. #6
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Gridview with find control question

    Oh Missed one part. You need to check like is it a DataRow.

    Code:
    Protected Sub GridView1_RowDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
            If (e.Row.RowType = DataControlRowType.DataRow) Then
                Dim linkButton As LinkButton = e.Row.FindControl("btnUpdate")
                If Not linkButton Is Nothing Then
                    linkButton.Enabled = True
                End If
            End If
        End Sub
    Please mark you thread resolved using the Thread Tools as shown

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Re: Gridview with find control question

    Ya, it's working perfect!
    But the main function of when clicking the edit button, the target btnUpdate is not enable, is that one of the bug in asp.net?

    Code:
    Protected Sub gvEditEvent_Editing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
            mpeEdit.Show()'modal popup extender
            CType(gvEditEvent.Rows(e.NewEditIndex).Cells(0).FindControl("btnUpdate"), LinkButton).Enabled = True    
        End Sub
    Last edited by newpat; Jul 13th, 2009 at 09:02 PM.

  8. #8
    Hyperactive Member dnanetwork's Avatar
    Join Date
    Oct 2007
    Location
    Mumbai
    Posts
    349

    Re: Gridview with find control question


  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Re: Gridview with find control question

    Something I forgot to say...I have a RequiredFieldValidator in my gridview so I need to make the update link button being not causesvalidation...here is my adviced solution, create a save button and make a cookie to save yr editing index:
    Code:
    Dim c As New HttpCookie("editRow", e.NewEditIndex)
    so when u click the update button
    Code:
    Dim editRow As Integer = Request.Cookies("editRow").Value.ToString
    gvEditEvent.UpdateRow(editRow, False)
    btnSaveEditEvent.Enabled = False
    simple!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width