locked
Gridview inside repeater. gridview command argument not being passed RRS feed

  • Question

  • User646364117 posted

    I have a gridview inside a repeater. The repeater has a blockid.

    I want to insert a row into the gridview. In the new row need to put a value in for the repeater's blockid.

    I have an insert button in a footer template of the gridview. When I try to access its command argument, which is supposed to be the repeaters blockid it is empty. That's my problem

    This is the markup for the gridview.

     <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:ImageButton ID="btn_Edit" runat="server"  CommandName ="EditRow" CommandArgument='<%#Eval("contentid") %>'  src="images/edit_20.png" />
    
                                        <asp:ImageButton ID="btn_Delete" runat="server" CommandName="DeleteRow" CommandArgument='<%#Eval("contentid") %>' src="images/delete_20.png" 
                                            OnClientClick="return confirm('Are you sure you want to delete this row?')" />
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:ImageButton ID="btn_Update" runat="server" CommandName="UpdateRow" CommandArgument='<%#Eval("contentid") %>' src="images/save_20.png" />
                                      
                                        <asp:ImageButton ID="btn_Cancel" runat="server" CommandName="CancelUpdate" CommandArgument='<%#Eval("contentid") %>' src="images/cancel_20.png" CausesValidation="False"/>
                                       
                                    </EditItemTemplate>
                                    <FooterTemplate>
                                        <asp:ImageButton ID="btn_Insert" runat="server" CommandName="InsertRow" 
                                                CommandArgument=<%#Eval("blockid")  %> src="images/addnew_20.png" CausesValidation="True"
                                                ValidationGroup="Insert"/>
                                    </FooterTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText=" Content ID" Visible="false">
                                    <ItemTemplate >
                                        <asp:TextBox ID="txt_id" runat="server" Value='<%#Eval("contentid") %>'></asp:TextBox>
                                    </ItemTemplate>
                                    <FooterTemplate >
                                  
                                        <asp:TextBox ID="txt_blockid" runat="server" Value='<%#Eval("blockid") %>'></asp:TextBox>
                                    </FooterTemplate>
                                </asp:TemplateField>

    This is the gridview rowcommand

       Protected Sub gvContent_RowCommand(sender As Object, e As GridViewCommandEventArgs)
            'https://www.youtube.com/watch?v=tONdC7ZPx7Q&feature=youtu.be
    
            Select Case (e.CommandName)
    
                Case "InsertRow"
                    Dim gv As GridView = CType(sender, GridView)
                    Dim shorttext As String = (CType(gv.FooterRow.FindControl("txt_insert_shorttext"), TextBox)).Text
                    Dim longtext As String = (CType(gv.FooterRow.FindControl("txt_insert_longtext"), TextBox)).Text
                    Dim blockid As Integer = Convert.ToInt16(e.CommandArgument)
    
                    EMailGen4.BlockContent.InsertContent(blockid, shorttext, longtext)

    Basically I want to get my hands in the repeaters block id associated with the gridview thaholds the insert button that was clicked so that I can include that blockid in the insert query.

    Tuesday, February 11, 2020 6:55 PM

Answers

  • User283571144 posted

    Hi sg48asp,

    Since footer does not bind to any data, using Eval here is meaningless. As you can see, a gridview control only has one footer row and eval() works for each data row.

    We can do work around on RowDataBound event if the data for footer is related to every data row, or simply populate the value after binding data to the gridview if the data is unique (your case is in this condition).

    More details, you can refer to the code:

    .aspx Page:

    <asp:Repeater ID="rptBlock" runat="server" OnItemDataBound="rptBlock_ItemDataBound">
                     <ItemTemplate>
    
                        <asp:Label ID="Label1" runat="server" Text="Block: "/>
                        <asp:Label ID="Label2" runat="server" Text='<%#Eval("blockid") %>' />
                        <asp:GridView ID="gvContent"
                            AutoGenerateColumns="false"
                            runat="server"
                            OnRowEditing="gvContent_RowEditing"
                            OnRowCancelingEdit="gvContent_RowCancelingEdit"
                            OnRowUpdating="gvContent_RowUpdating"
                            OnRowCommand="gvContent_RowCommand"
                            ShowFooter="true"
                            OnRowDataBound="gvContent_RowDataBound"
                            >
                            <Columns>
                                
                                <asp:TemplateField HeaderText="Text ID" Visible="false">
                                    <ItemTemplate >
                                        <asp:TextBox ID="txt_id" runat="server" Value='<%#Eval("id") %>'></asp:TextBox>
                                    </ItemTemplate>
    
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Block ID">
                                    <ItemTemplate >
                                        <asp:Label ID="lbl_blockid" runat="server" Text='<%#Eval("blockid") %>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txt_blockid" runat="server" Text='<%#Eval("blockid") %>'></asp:TextBox>
                                    </EditItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Short Text">
                                    <ItemTemplate >
                                        <asp:Label ID="lbl_shorttext" runat="server" Text='<%#Eval("shorttext") %>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txt_shorttext" runat="server" Text='<%#Eval("shorttext") %>'></asp:TextBox>
                                    </EditItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Long Text">
                                    <ItemTemplate>
                                        <asp:Label ID="lbl_longtext" runat="server" Text='<%#Eval("longtext") %>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txt_longtext" runat="server" Text='<%#Eval("longtext") %>'></asp:TextBox>
                                    </EditItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:Button ID="btn_Edit" runat="server" CommandArgument='<%# Container.DataItemIndex %>' CommandName="Edit" Text="Edit"/>
                                   
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:Button ID="btn_Update" runat="server" CommandArgument='<%# Container.DataItemIndex %>' CommandName="Update" Text="Update"/>
                                      
                                        <asp:Button ID="btn_Cancel" runat="server" CommandName="Cancel" Text="Cancel"/>
                                        
                                    </EditItemTemplate>
                                    <FooterTemplate>
                                        <asp:Button ID="btn_Insert" runat="server" CommandName="InsertRow" Text="Insert"/>
                                    </FooterTemplate>
                                </asp:TemplateField>
    
                            </Columns>
                        </asp:GridView>
    
                    </ItemTemplate>
                    <SeparatorTemplate>
                        <br />
                        <hr />
                        <br />
                    </SeparatorTemplate>
                </asp:Repeater>
                
                <asp:Label ID="Label3" runat="server" Text="Capture Block id: "></asp:Label>
                <asp:Label ID="Label4" runat="server"></asp:Label>

    Code-behind:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            If Not IsPostBack Then
                BindRepeater()
            End If
        End Sub
    
        Private Sub BindRepeater()
            Dim dt As DataTable = New DataTable()
            dt.Columns.Add("blockid", GetType(Integer))
    
            For i As Integer = 0 To 3 - 1
                dt.Rows.Add(i)
            Next
    
            rptBlock.DataSource = dt
            rptBlock.DataBind()
        End Sub
    
        Private Sub BindGridViewByBlockID(ByVal blockid As Integer, ByVal gd As GridView)
            Dim dt As DataTable = New DataTable()
            dt.Columns.Add("id", GetType(Integer))
            dt.Columns.Add("blockid", GetType(Integer))
            dt.Columns.Add("shorttext", GetType(String))
            dt.Columns.Add("longtext", GetType(String))
    
            For i As Integer = 0 To 4 - 1
                dt.Rows.Add(i, blockid, "shortext-" & i & "(blockid-" & blockid & ")", "longtext-" & i & "(blockid-" & blockid & ")")
            Next
    
            gd.DataSource = dt
            gd.DataBind()
            Dim btn As Button = CType(gd.FooterRow.FindControl("btn_Insert"), Button)
            btn.CommandArgument = blockid.ToString()
        End Sub
    
        Protected Sub rptBlock_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
            If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
                Dim gd As GridView = CType(e.Item.FindControl("gvContent"), GridView)
                Dim lbl As Label = CType(e.Item.FindControl("Label2"), Label)
                Dim blockid As Integer = Convert.ToInt32(lbl.Text)
                BindGridViewByBlockID(blockid, gd)
            End If
        End Sub
    
        Protected Sub gvContent_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
            Dim gd As GridView = CType(sender, GridView)
            gd.EditIndex = e.NewEditIndex
            Dim row As GridViewRow = gd.Rows(e.NewEditIndex)
            Dim blockid As Integer = Convert.ToInt32((CType(row.FindControl("lbl_blockid"), Label)).Text)
            BindGridViewByBlockID(blockid, gd)
        End Sub
    
        Protected Sub gvContent_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
            Dim gd As GridView = CType(sender, GridView)
            gd.EditIndex = -1
            BindRepeater()
        End Sub
    
        Protected Sub gvContent_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
            Dim gd As GridView = CType(sender, GridView)
            Dim row As GridViewRow = gd.Rows(e.RowIndex)
            Dim blockid As String = (CType(row.FindControl("txt_blockid"), TextBox)).Text
            Dim shorttext As String = (CType(row.FindControl("txt_shorttext"), TextBox)).Text
            Dim longtext As String = (CType(row.FindControl("txt_longtext"), TextBox)).Text
            Page.ClientScript.RegisterClientScriptBlock([GetType](), "js", "<script>alert('you have select row: blockid = " & blockid & ", shorttext = " & shorttext & ", longtext = " & longtext & "');</script>")
            gd.EditIndex = -1
            BindRepeater()
        End Sub
    
        Protected Sub gvContent_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
            Select Case e.CommandName
                Case "InsertRow"
                    Dim blockid As String = e.CommandArgument.ToString()
                    Label4.Text = blockid
                    Exit Select
            End Select
        End Sub
    

    Demo:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 12, 2020 8:11 AM

All replies

  • User283571144 posted

    Hi sg48asp,

    Since footer does not bind to any data, using Eval here is meaningless. As you can see, a gridview control only has one footer row and eval() works for each data row.

    We can do work around on RowDataBound event if the data for footer is related to every data row, or simply populate the value after binding data to the gridview if the data is unique (your case is in this condition).

    More details, you can refer to the code:

    .aspx Page:

    <asp:Repeater ID="rptBlock" runat="server" OnItemDataBound="rptBlock_ItemDataBound">
                     <ItemTemplate>
    
                        <asp:Label ID="Label1" runat="server" Text="Block: "/>
                        <asp:Label ID="Label2" runat="server" Text='<%#Eval("blockid") %>' />
                        <asp:GridView ID="gvContent"
                            AutoGenerateColumns="false"
                            runat="server"
                            OnRowEditing="gvContent_RowEditing"
                            OnRowCancelingEdit="gvContent_RowCancelingEdit"
                            OnRowUpdating="gvContent_RowUpdating"
                            OnRowCommand="gvContent_RowCommand"
                            ShowFooter="true"
                            OnRowDataBound="gvContent_RowDataBound"
                            >
                            <Columns>
                                
                                <asp:TemplateField HeaderText="Text ID" Visible="false">
                                    <ItemTemplate >
                                        <asp:TextBox ID="txt_id" runat="server" Value='<%#Eval("id") %>'></asp:TextBox>
                                    </ItemTemplate>
    
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Block ID">
                                    <ItemTemplate >
                                        <asp:Label ID="lbl_blockid" runat="server" Text='<%#Eval("blockid") %>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txt_blockid" runat="server" Text='<%#Eval("blockid") %>'></asp:TextBox>
                                    </EditItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Short Text">
                                    <ItemTemplate >
                                        <asp:Label ID="lbl_shorttext" runat="server" Text='<%#Eval("shorttext") %>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txt_shorttext" runat="server" Text='<%#Eval("shorttext") %>'></asp:TextBox>
                                    </EditItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Long Text">
                                    <ItemTemplate>
                                        <asp:Label ID="lbl_longtext" runat="server" Text='<%#Eval("longtext") %>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txt_longtext" runat="server" Text='<%#Eval("longtext") %>'></asp:TextBox>
                                    </EditItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:Button ID="btn_Edit" runat="server" CommandArgument='<%# Container.DataItemIndex %>' CommandName="Edit" Text="Edit"/>
                                   
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:Button ID="btn_Update" runat="server" CommandArgument='<%# Container.DataItemIndex %>' CommandName="Update" Text="Update"/>
                                      
                                        <asp:Button ID="btn_Cancel" runat="server" CommandName="Cancel" Text="Cancel"/>
                                        
                                    </EditItemTemplate>
                                    <FooterTemplate>
                                        <asp:Button ID="btn_Insert" runat="server" CommandName="InsertRow" Text="Insert"/>
                                    </FooterTemplate>
                                </asp:TemplateField>
    
                            </Columns>
                        </asp:GridView>
    
                    </ItemTemplate>
                    <SeparatorTemplate>
                        <br />
                        <hr />
                        <br />
                    </SeparatorTemplate>
                </asp:Repeater>
                
                <asp:Label ID="Label3" runat="server" Text="Capture Block id: "></asp:Label>
                <asp:Label ID="Label4" runat="server"></asp:Label>

    Code-behind:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            If Not IsPostBack Then
                BindRepeater()
            End If
        End Sub
    
        Private Sub BindRepeater()
            Dim dt As DataTable = New DataTable()
            dt.Columns.Add("blockid", GetType(Integer))
    
            For i As Integer = 0 To 3 - 1
                dt.Rows.Add(i)
            Next
    
            rptBlock.DataSource = dt
            rptBlock.DataBind()
        End Sub
    
        Private Sub BindGridViewByBlockID(ByVal blockid As Integer, ByVal gd As GridView)
            Dim dt As DataTable = New DataTable()
            dt.Columns.Add("id", GetType(Integer))
            dt.Columns.Add("blockid", GetType(Integer))
            dt.Columns.Add("shorttext", GetType(String))
            dt.Columns.Add("longtext", GetType(String))
    
            For i As Integer = 0 To 4 - 1
                dt.Rows.Add(i, blockid, "shortext-" & i & "(blockid-" & blockid & ")", "longtext-" & i & "(blockid-" & blockid & ")")
            Next
    
            gd.DataSource = dt
            gd.DataBind()
            Dim btn As Button = CType(gd.FooterRow.FindControl("btn_Insert"), Button)
            btn.CommandArgument = blockid.ToString()
        End Sub
    
        Protected Sub rptBlock_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
            If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
                Dim gd As GridView = CType(e.Item.FindControl("gvContent"), GridView)
                Dim lbl As Label = CType(e.Item.FindControl("Label2"), Label)
                Dim blockid As Integer = Convert.ToInt32(lbl.Text)
                BindGridViewByBlockID(blockid, gd)
            End If
        End Sub
    
        Protected Sub gvContent_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
            Dim gd As GridView = CType(sender, GridView)
            gd.EditIndex = e.NewEditIndex
            Dim row As GridViewRow = gd.Rows(e.NewEditIndex)
            Dim blockid As Integer = Convert.ToInt32((CType(row.FindControl("lbl_blockid"), Label)).Text)
            BindGridViewByBlockID(blockid, gd)
        End Sub
    
        Protected Sub gvContent_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
            Dim gd As GridView = CType(sender, GridView)
            gd.EditIndex = -1
            BindRepeater()
        End Sub
    
        Protected Sub gvContent_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
            Dim gd As GridView = CType(sender, GridView)
            Dim row As GridViewRow = gd.Rows(e.RowIndex)
            Dim blockid As String = (CType(row.FindControl("txt_blockid"), TextBox)).Text
            Dim shorttext As String = (CType(row.FindControl("txt_shorttext"), TextBox)).Text
            Dim longtext As String = (CType(row.FindControl("txt_longtext"), TextBox)).Text
            Page.ClientScript.RegisterClientScriptBlock([GetType](), "js", "<script>alert('you have select row: blockid = " & blockid & ", shorttext = " & shorttext & ", longtext = " & longtext & "');</script>")
            gd.EditIndex = -1
            BindRepeater()
        End Sub
    
        Protected Sub gvContent_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
            Select Case e.CommandName
                Case "InsertRow"
                    Dim blockid As String = e.CommandArgument.ToString()
                    Label4.Text = blockid
                    Exit Select
            End Select
        End Sub
    

    Demo:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 12, 2020 8:11 AM
  • User646364117 posted

    Brando,

    Thank you for taking the time to understand my code. You really got it. I was able to incorporate your solution by modifying the code following the location where I bound the 'nested' gridview.

    I simply added a command argument to the insert button there. Then everything worked like a charm.

    Your response was an immense help. I didn't know that eval wouldn't work in a footer row.

    Steve

    Protected Sub rptBlock_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptBlock.ItemDataBound
            If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
                Dim hf As HiddenField = CType(e.Item.FindControl("hfBlockID"), HiddenField)
                Dim blockId As Integer
                blockId = Convert.ToInt16(hf.Value)
    
                'Dim lbl1 As Label = CType(e.Item.FindControl("lblBlockType"), Label)
                Dim hf1 As HiddenField = CType(e.Item.FindControl("hfBlockType"), HiddenField)
                Dim blocktype As String
                blocktype = hf1.Value.ToString
                '
                Dim gv As GridView = CType(e.Item.FindControl("gvContent"), GridView)
    
                gv.DataSource = EMailGen3.GridviewContent.GetAllGridViewContents(blockId)
                gv.DataBind()
    
                If blocktype <> "Long Text" Then
                    gv.Columns(1).Visible = False
                    gv.DataBind()
                End If
                Dim btn As ImageButton = CType(gv.FooterRow.FindControl("btn_Insert"), ImageButton)
                btn.CommandArgument = blockId.ToString()
                Dim pnl As Panel = CType(e.Item.FindControl("Panel1"), Panel)
                pnl.Visible = False
            End If
    
    
        End Sub

    Wednesday, February 12, 2020 11:01 AM