嵌套DetailsView插入方法的参数

时间:2013-05-12 12:18:43

标签: c# asp.net

我将DetailsView嵌套在GridView中,我无法弄清楚如何将值(如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。

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

1 个答案:

答案 0 :(得分: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项,然后点击按钮后,插入参数根据该命令参数设置