0

我在 GridView 中嵌套了 DetailsView,但我不知道如何将值(如 GridView 中的项目 ID 和会话中的值)传递给 insert 方法的参数。

             <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>

我尝试以与这样的选择参数相同的方式执行此操作:

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();
        }
    }

但仅设置了 userId 参数值(来自 Session),postId 始终为 0。

什么是正确的解决方案?谢谢你。

4

1 回答 1

1

我找到了解决方案:

<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>

代码隐藏:

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();
        }
    }

数据绑定时,它会找到按钮并将其命令参数设置为父gridview的id项,然后在点击按钮后,根据该命令参数设置插入参数

于 2013-05-14T07:12:59.363 回答