嵌套的DetailsView插入方法的参数
本文关键字:参数 方法 DetailsView 嵌套 插入 | 更新日期: 2023-09-27 18:13:07
我在GridView中嵌套了DetailsView,我不知道如何传递值(如GridView中项目的ID和会话值)到插入方法的参数。
<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。
正确的解决方案是什么?谢谢你。
我已经找到了解决方案:
<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 item,然后在点击按钮后,根据该命令参数设置insert参数