Populate GridView template control (label) when LinkButton clicked

ASP .Net 2.0

Scenario:
I have a gridview, paging is enabled, page size 2
In the gridview  <Columns> is a <asp:TemplateField> containing an <ItemTemplate>
In the <ItemTemplate> there are various LinkButton controls wired to VB Subs, each having CommandNames and CommandArguments

The ItemTemplate uses HTML table with a <TFOOT> element  
In the TFOOT I have an asp:Label with an ID of LabelErrorMessage, see code snippet # 1

The problem:
When my LInkButton fires and the associated code is invoked, (see code snipet #2)
I want to populate the associated  Label in the GridViewRow with any message returned by code that is fired when the LinkButton is clicked

Currently my code returns a msgbox  output saying "objGV is Nothing"

I had it working yesterday, but it twas the Label in the first ROW of the GridView Page that was being populated, even if I clicked the LinkButton in the second ROW, hmmm.

Anyhow hope this has not been too long-winded...

Any suggestions?

Alan ";0)
Snippet # 1
========================================
<ItemTemplate>
  <table>
   <tfoot>
    <tr>
      <td colspan=6 nowrap=nowrap style="color:DarkRed;font-size:small;background-color:#fffff4;border:solid 1px Red; text-align:center;">
        <asp:Label 
        ID = "lblErrorMessage"
        runat = "server"
        EnableViewState = "False"
        Text = "Label"
        Visible = "False"
            >
        </asp:Label>
      </td>
    </tr>
    </tfoot>
  </table>
</ItemTemplate>              
</asp:TemplateField>
========================================
 
Snippet # 2
========================================
    Sub LinkBtn_SVK_Click(ByVal sender As Object, ByVal e As CommandEventArgs)
 
        Dim objLabel As Label = Nothing
        Dim strErrMsg As String = String.Empty
        Dim objGV As GridView = CType(FindControl("GridViewBrowseMemberProfiles"), GridView)
 
 
        If objGV IsNot Nothing Then
            Dim objGVRows As GridViewRowCollection = objGV.Rows
            For Each objRow As GridViewRow In objGVRows
                objLabel = CType(FindControl("lblErrorMessage"), Label)
                If objLabel IsNot Nothing Then
                    strErrMsg = "Hello world"
                    With objLabel
                        .Text = strErrMsg
                        .Visible = True
                    End With
                Else
                    MsgBox("objLabel is Nothing")
                End If
            Next
        Else
            MsgBox("objGV is Nothing")
        End If
 
 
        objLabel = Nothing
 
 
    End Sub
====================================

Open in new window

LVL 26
Alan WarrenAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
NazoUKConnect With a Mentor Commented:
That would work but it seems a little inefficient to loop through the entire thing to find the row you want.
You can achieve this more easily using the rowcommand event of your gridview, take out the oncommand element of the linkbutton and add this:

Protected Sub GridViewBrowseMemberProfiles_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridViewBrowseMemberProfiles.RowCommand
        Dim row As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)
    End Sub

Row then contains the gridviewrow your control was in and you can find the label using findcontrol as normal.
0
 
Alan WarrenAuthor Commented:
It's ok folks, I sussed it out, took the old belt and braces approach and exposed everyhing progressively, until I found the control I needed. If you are interested, see the code snippet.

Alan ";0)
    Sub LinkBtn_SVK_Click(ByVal sender As Object, ByVal e As CommandEventArgs)
 
        On Error GoTo ReportError
        Dim objLabel As Label = Nothing
        Dim strErrMsg As String = String.Empty
        Dim strMessage As String = String.Empty
 
        If Not HttpContext.Current.Request.IsLocal Then
            ' Do stuff
        Else
            strMessage = "Can't do that in local mode, dohhhh..."
            GoTo ReportError
        End If
 
 
ExitProcedure:
        On Error Resume Next
        Exit Sub
 
ReportError:
 
        Dim objGV As GridView = CType(ControlsUtil.FindControlIterative(Me, "GridViewBrowseMemberProfiles", strErrMsg), GridView)
        If objGV IsNot Nothing Then
            Dim objGVRows As GridViewRowCollection = objGV.Rows
            If objGVRows IsNot Nothing Then
                For Each objRow As GridViewRow In objGVRows
                    Dim objLinkButton As LinkButton = DirectCast(objRow.FindControl("LinkButtonVKiss"), LinkButton)
                    If objLinkButton IsNot Nothing Then
                        With objLinkButton
                            If .CommandArgument.ToString() = e.CommandArgument.ToString Then
                                objLabel = DirectCast(objRow.FindControl("lblErrorMessage"), Label)
                                If objLabel IsNot Nothing Then
                                    With objLabel
                                        .Text = strErrMsg & vbCrLf & vbCrLf & strMessage
                                        .Visible = True
                                    End With
                                End If
                            End If
                        End With
                    End If
                Next
            End If
        End If
 
        Resume ExitProcedure

Open in new window

0
 
Alan WarrenAuthor Commented:
Hi NazoUK,
thanks for that, it works like a charm. Once I worked out that the RowCommand event fires after the LinkButon command event, then I was able to pass any errors encoutered to the RowCommand via a temporary session variable, for reporting.

Who would have thought of using the RowCommand event of the gridview as a page scope central reporting util. A usefull tip, resulting in re-usable code, less code, less object instantiation, less resource  intensive and less overhead.

Thankyou

Alan ";0)
    Sub LinkBtn_SVK_Click(ByVal sender As Object, ByVal e As CommandEventArgs)
 
        On Error GoTo ReportError
 
        Dim objLabel As Label = Nothing
        Dim strErrMsg As String = String.Empty
 
        If Not HttpContext.Current.Request.IsLocal Then
 
            ' Do stuff...
        Else
            strMessage = "Can't do that in local mode, dohhhh..."
            GoTo ReportError
        End If
 
ExitProcedure:
        On Error Resume Next
        Exit Sub
 
ReportError:
 
        ' Populate the session variable Session("ErrMsg")
        ' The GridViewBrowseMemberProfiles_RowCommand event 
        ' will parse the Session("ErrMsg") and report the error.
        If strErrMsg <> String.Empty Then
            Session("ErrMsg") = strErrMsg
        End If
 
        Resume ExitProcedure
 
    End Sub
 
 
    Protected Sub GridViewBrowseMemberProfiles_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridViewBrowseMemberProfiles.RowCommand
 
        If Session("ErrMsg") <> String.Empty Then
 
            Dim objRow As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)
            If objRow IsNot Nothing Then
                Dim objLabel As Label = CType(objRow.FindControl("lblErrorMessage"), Label)
                If objLabel IsNot Nothing Then
                    With objLabel
                        .Text = Session("ErrMsg")
                        .Visible = True
                    End With
                End If
            End If
        End If
 
        ' Clear the session variable
        Session("ErrMsg") = String.Empty
 
    End Sub

Open in new window

0
All Courses

From novice to tech pro — start learning today.