views:

24

answers:

0

I have a 2 tiered nested GridView in ASP.NET and I'm trying to insert a new record into the child GridView. I am able to Add/Remove/Edit records in the parent table but the child table is connected to my SQL database through the code-behind, so I am unable to use the SqlDataSource control like with the parent table.

I have searched around the web for a solution and have found a couple of similar scenarios, but none of them apply directly to my specific problem. Could someone please take a look at my code and help me figure this one out?

ASP.NET GridView Code:

<asp:GridView ID="GridView1" runat="server"
                AllowPaging="True"
                AllowSorting="True"
                AutoGenerateColumns="False"
                DataKeyNames="imageId"
                DataSourceID="SqlDataSource1"
                PageSize="20"
                OnRowDataBound="GridView1_RowDataBound" 
                CellPadding="4" 
                ForeColor="#333333" 
                GridLines="None">
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                <a href="javascript:ShowChildGrid('div<%# Eval("imageId") %>');">
                    <img id="imgdiv<%# Eval("imageId") %>" 
                        alt="Click to show/hide updates" 
                        border="0" 
                        src="images/arrowright.jpg"/>
                </a>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="imageId" HeaderText="ID" SortExpression="imageId" />
            <asp:TemplateField HeaderText="Image">
                <ItemTemplate>
                    <asp:Image ID="thumbnail" Width="100px" runat="server" ImageUrl='<%# Eval("imageId", "Thumbnail.ashx?imageId={0}")%>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="name" HeaderText="Image Name" SortExpression="name" />
            <asp:BoundField DataField="originUrl" HeaderText="Origin URL" SortExpression="originUrl" />
            <asp:BoundField DataField="metatags" HeaderText="Metatags" />
            <asp:TemplateField>
                <ItemTemplate>
                    </td>
                </tr>
                <tr>
                    <td colspan="100%">
                        <div id="div<%# Eval("imageId") %>" style="display: none;position: relative;left: 25px;">
                            <asp:GridView ID="GridView2" runat="server"
                                        AutoGenerateColumns="false"
                                        AutoGenerateDeleteButton="True"
                                        DataKeyNames="updateId"
                                        EmptyDataText="No updates for this image."
                                        Width="96%" 
                                        ShowFooter="true"
                                        CellPadding="4" 
                                        ForeColor="#333333" 
                                        GridLines="Both">
                            <HeaderStyle BackColor="#82A2C9" Font-Bold="True" ForeColor="White" />
                            <FooterStyle BackColor="#ACACAC" Font-Bold="True" ForeColor="White" />
                            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                            <Columns>
                                <asp:TemplateField HeaderText="Project URL">
                                    <ItemTemplate>
                                        <%#Eval("projectUrl")%>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtProjectUrl" Width="98%" runat="Server" Text='<%# Eval("projectUrl") %>'></asp:TextBox>
                                    </EditItemTemplate>
                                    <FooterTemplate>
                                        <asp:TextBox ID="txtProjectUrl" Width="98%" runat="Server"></asp:TextBox>
                                    </FooterTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Added By:">
                                    <ItemTemplate>
                                        <%#Eval("firstName")%>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:DropDownList ID="ddlUserId" Width="100%" runat="server" SelectedValue='<%# Eval("userId").ToString() %>'>
                                            <asp:ListItem Text="User1" Value="1"></asp:ListItem>
                                            <asp:ListItem Text="User2" Value="2"></asp:ListItem>
                                            <asp:ListItem Text="User3" Value="3"></asp:ListItem>
                                            <asp:ListItem Text="User4" Value="4"></asp:ListItem>
                                            <asp:ListItem Text="User5" Value="5"></asp:ListItem>
                                            <asp:ListItem Text="User6" Value="6"></asp:ListItem>
                                            <asp:ListItem Text="User7" Value="7"></asp:ListItem>
                                        </asp:DropDownList>
                                    </EditItemTemplate>
                                    <FooterTemplate>
                                        <asp:DropDownList ID="ddlUserId" Width="100%" runat="server">
                                            <asp:ListItem Text="User1" Value="1"></asp:ListItem>
                                            <asp:ListItem Text="User2" Value="2"></asp:ListItem>
                                            <asp:ListItem Text="User3" Value="3"></asp:ListItem>
                                            <asp:ListItem Text="User4" Value="4"></asp:ListItem>
                                            <asp:ListItem Text="User5" Value="5"></asp:ListItem>
                                            <asp:ListItem Text="User6" Value="6"></asp:ListItem>
                                            <asp:ListItem Text="User7" Value="7"></asp:ListItem>
                                        </asp:DropDownList>
                                    </FooterTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Date">
                                    <ItemTemplate>
                                        <%#Eval("date", "{0:MMM dd yyyy}")%>
                                    </ItemTemplate>
                                    <FooterTemplate>
                                        <asp:Button ID="btnInsert" runat="Server" Text="Insert" />
                                        </FooterTemplate>
                                </asp:TemplateField>
                            </Columns>
                            </asp:GridView>
                        </div>
                    </td>
                </tr>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>        
        </asp:GridView>
        </table>

        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString='<%$ ConnectionStrings:imageboxConnectionString %>' 
            SelectCommand="SELECT imageId,name,imagePath,originUrl,metatags FROM [Image] ORDER BY imageId DESC">
        </asp:SqlDataSource>

VB Code-Behind (To display values in child GridView):

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

    If e.Row.RowType = DataControlRowType.DataRow Then

        Dim gv As GridView = DirectCast(e.Row.FindControl("GridView2"), GridView)
        Dim dbSrc As New SqlDataSource()

        dbSrc.ConnectionString = ConfigurationManager.ConnectionStrings("imageboxConnectionString").ConnectionString
        dbSrc.SelectCommand = "SELECT a.updateId, a.userId, a.imageId, a.projectURL, a.date, u.userId, u.firstName FROM [AddUpdate] a, [User] u WHERE a.userId = u.userId AND a.imageId = '" + GridView1.DataKeys(e.Row.RowIndex).Value.ToString & "' ORDER BY a.updateId DESC"
        dbSrc.DeleteCommand = "DELETE from AddUpdate WHERE updateId = @updateId"
        gv.DataSource = dbSrc
        gv.DataBind()

    End If

End Sub

Thank you for any help.