0

我有一个空白中继器嵌套在 GridView 的列中,并希望使用 GridView 中的按钮通过后面的代码按需填充中继器。我在引用按钮的 onCommand 子中的中继器时遇到了困难。

这是相关的标记:

<asp:GridView ID="Submission" runat="server" AllowPaging="true" AllowSorting="true" />
            <Columns>
                ..........
                <asp:TemplateField HeaderText="Action">
                    <ItemTemplate>
                        <asp:ImageButton ID="AdminEditSubmission" runat="server" ImageUrl="edit_15.png"
                            alt="" OnCommand="loadDetails" CommandArgument="X" />
                    </ItemTemplate>
                </asp:TemplateField>
                ..........
                <asp:TemplateField>
                    <ItemTemplate>
                                <asp:Repeater ID="RptSubmissionDetail" runat="server">
                                </asp:Repeater>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

为了这个小例子,我将尝试用“Hello world”更新转发器的标题。但是,当它到达最后一行时,我得到了可怕的“对象引用未设置为对象的实例”错误。

    Sub loadDetails(sender As Object, e As CommandEventArgs)
        Dim rpt As Repeater = CType(Page.FindControl("RptsSubmissionDetail"), Repeater)
        Dim tmpHdr As TemplateBuilder = New TemplateBuilder
        tmpHdr.AppendLiteralString("Hello World")
        rpt.HeaderTemplate = tmpHdr
    End Sub

谁能告诉我如何从我的 ImageButton click sub 中引用这个中继器?我已经尝试了几个,Page.FindControl("RptsSubmissionDetail")只是我最近的尝试。

4

2 回答 2

2

在 GridView 的 RowCommand 中,您需要FindControl使用GridViewRow

Sub Submission_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
    If e.CommandName = "loadDetails" Then
      ' Convert the row index stored in the CommandArgument
      ' property to an Integer.
      Dim index = Convert.ToInt32(e.CommandArgument)  
      ' Retrieve the row that contains the button clicked 
      ' by the user from the Rows collection.
      Dim row = Submission.Rows(index)  
      Dim repeater = DirectCast(row.FindControl("RptSubmissionDetail"), Repeater)
    End If
End Sub

从 ImageButton 的点击事件来看,它几乎是一样的。将sender参数转换为 ,ImageButton并将其NamingContainer属性转换为GridViewRow。然后FindControl如上图使用:

Sub ImageButton_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim img = DirectCast(sender, ImageButton)
    Dim row = DirectCast(img.NamingContainer, GridViewRow)
    Dim repeater = DirectCast(row.FindControl("RptSubmissionDetail"), Repeater)
End Sub
于 2012-08-28T20:19:33.177 回答
0

在您的 ImageButton Click 事件中编写以下代码

For Each row As GridViewRow In Submission.Rows
Dim r As Repeater = DirectCast(row.FindControl("RptsSubmissionDetail"), Repeater)           
If r IsNot Nothing Then
          ' do your work
End If
Next

或者,如果您想在特定行中找到中继器,您可以将其查找为

Dim r As Repeater = DirectCast(Submission.Rows[0].FindControl("RptsSubmissionDetail"), Repeater)  ' Give the right index            
If r IsNot Nothing Then
          ' do your work
End If
于 2012-08-28T20:21:54.683 回答