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>

この小さな例のために、リピーターのヘッダーを「Helloworld」で更新してみます。ただし、最後の行に到達すると、「オブジェクト参照がオブジェクトのインスタンスに設定されていません」という恐ろしいエラーが発生します。

    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クリックサブからこのリピーターを参照する方法を教えてもらえますか?私はいくつか試しましたが、Page.FindControl( "RptsSubmissionDetail")は私の最新の試みにすぎません。

4

2 に答える 2

2

FindControlGridViewのRowCommandでは、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

または、特定の行でrepearterを検索する場合は、次のように検索できます。

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 に答える