简体   繁体   中英

Nested DetailsView insert method's parameters

I have DetailsView nested in GridView, and I can't figure out how to pass values(like ID of item in GridView and value from session) to parameters for insert method.

             <asp:DetailsView ID="DetailsViewPostLikes" runat="server"
                AutoGenerateRows="false" DataSourceID="odsPostLikes" 
                DataKeyNames="id" GridLines="None">
                <Fields>
                <asp:CommandField ShowInsertButton="true" InsertText="Like" newtext="Like" />
                    <asp:TemplateField HeaderText="" SortExpression="Nickname">
                        <ItemTemplate>
                            <asp:Label id="nicknamelikesCount" runat="server" Text='<%# Bind("LikeCount") %>'></asp:Label> people likes this.                                
                        </ItemTemplate>
                    </asp:TemplateField>
                </Fields>
              </asp:DetailsView>
              <asp:ObjectDataSource ID="odsPostLikes" runat="server" 
                TypeName="SocWebApp.Database.PostLikeTable"
                SelectMethod="Select" InsertMethod="Insert" UpdateMethod="Update">
                <SelectParameters>
                    <asp:Parameter Name="postId" Type="Int32" />
                </SelectParameters>
                <InsertParameters>
                    <asp:Parameter Name="postId" Type="Int32" />
                    <asp:SessionParameter Name="userId" SessionField="User_id" Type="Int32" />
                </InsertParameters>
              </asp:ObjectDataSource>

I've tried to do this same way as for select parameter like this:

protected void posts_ItemDatabound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)//(GridView)e.Row.FindControl("odsComments") != null)
        {
            ObjectDataSource s1 = (ObjectDataSource)e.Row.FindControl("odsComments");
            s1.SelectParameters["postId"].DefaultValue = DataBinder.Eval(e.Row.DataItem, "Id").ToString();

            ObjectDataSource s2 = (ObjectDataSource)e.Row.FindControl("odsPostLikes");
            s2.InsertParameters["postId"].DefaultValue = DataBinder.Eval(e.Row.DataItem, "Id").ToString();

            ObjectDataSource s3 = (ObjectDataSource)e.Row.FindControl("odsPostLikes");
            s3.InsertParameters["userId"].DefaultValue = Session["User_id"].ToString();
        }
    }

but only userId parameter value is set(from Session), postId is always 0.

What is the proper solution? Thank you.

I've found the solution:

<asp:DetailsView ID="DetailsViewPostLikes" runat="server"
                AutoGenerateRows="false" DataSourceID="odsPostLikes"
                DataKeyNames="id" GridLines="None" OnDataBound="postLikeInsert_DataBound" OnItemCommand="postLikeInsert_ItemCommand">
                <Fields>
                <asp:CommandField ShowInsertButton="false" InsertText="Like" newtext="Like" />
                    <asp:TemplateField HeaderText="" SortExpression="Nickname">
                        <ItemTemplate>
                            <asp:LinkButton ID="Like" runat="server" CommandName="Insert" Text="Like" />
                            <asp:Label id="likesCount" runat="server" Text='<%# Bind("LikeCount") %>'></asp:Label> people likes this.
                        </ItemTemplate>
                    </asp:TemplateField>
                </Fields>
              </asp:DetailsView>
              <asp:ObjectDataSource ID="odsPostLikes" runat="server"
                TypeName="SocWebApp.Database.PostLikeTable"
                SelectMethod="Select" InsertMethod="Insert" UpdateMethod="Update">
                <SelectParameters>
                    <asp:Parameter Name="postId" Type="Int32" />
                </SelectParameters>
                <InsertParameters>
                    <asp:Parameter Name="postId" Type="Int32" />
                    <asp:SessionParameter Name="userId" SessionField="User_id" Type="Int32" />
                </InsertParameters>
              </asp:ObjectDataSource>

code-behind:

protected void postLikeInsert_DataBound(object sender, EventArgs e)
    {
        DetailsView d = (DetailsView)sender;
        GridViewRow row = (GridViewRow)d.NamingContainer;
        int idx = row.RowIndex;
        GridView grid = (GridView)row.NamingContainer;
        string strGoalIndicatorID = grid.DataKeys[idx]["id"].ToString();

        LinkButton b = (LinkButton)d.FindControl("Like");
        b.CommandArgument = strGoalIndicatorID;

    }

    protected void postLikeInsert_ItemCommand(object sender, DetailsViewCommandEventArgs e)
    {
        if (e.CommandName == "Insert")
        {
            DetailsView d = (DetailsView)sender;
            d.ChangeMode(DetailsViewMode.Insert);
            ObjectDataSource o = (ObjectDataSource)d.NamingContainer.FindControl("odsPostLikes");
            o.InsertParameters["postId"].DefaultValue = e.CommandArgument.ToString();
        }
    }

when data bound, it will find the button and set his command argument to id item of parent gridview, then after hitting the button, insert parameter is set based on that command argument

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM