我在DetailsView控件上更新事件时遇到问题。将值从下拉列表传递到UpdateParameters集合时,它始终将字段parentID设置为0。我知道它在事件中找到了控件但我甚至无法在我的示例中将默认值设置为“1”,因此它与UpdateParameters属性的关联可以告诉我我做错了什么以及如何解决它
protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
DropDownList forumGroup = DetailsView1.FindControl("DropDownList2") as DropDownList;
string value = forumGroup.SelectedValue.ToString();
ObjectDataSource3.UpdateParameters["parentID"].DefaultValue = "1" // always is 0;
}
public int UpdateForumByID(int forumID, string title, string url, string description,
string imageUrl, int parentID, int importance, bool moderated, bool active)
{
ObjectContext context = new ObjectContext("name=CMSEntities");
ObjectSet<CMSModel.Forums> forums = context.CreateObjectSet<CMSModel.Forums>();
// here forum is null
CMSModel.Forums forum = forums.FirstOrDefault(x => x.ForumID == forumID);
forum.Title = title;
forum.Url = url;
forum.Description = description;
forum.ImageUrl = imageUrl;
forum.ParentID = parentID;
forum.Importance = importance;
forum.Moderated = moderated;
forum.Active = active;
int affectedRows = context.SaveChanges();
return affectedRows;
}
标记:`
<asp:ObjectDataSource ID="ObjectDataSource3" runat="server"
SelectMethod="GetForumByID" TypeName="CMS.Framework.Forums"
UpdateMethod="UpdateForumByID">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="forumID"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="forumID" Type="Int32" />
<asp:Parameter Name="title" Type="String" />
<asp:Parameter Name="url" Type="String" />
<asp:Parameter Name="description" Type="String" />
<asp:Parameter Name="imageUrl" Type="String" />
<asp:Parameter Name="parentID" Type="Int32" />
<asp:Parameter Name="parentID" Type="Int32" />
<asp:Parameter Name="importance" Type="Int32" />
<asp:Parameter Name="active" Type="Boolean" />
</UpdateParameters>
</asp:ObjectDataSource>
<asp:DetailsView ID="DetailsView1" runat="server"
AutoGenerateRows="False"
DataKeyNames="ForumID"
DataSourceID="ObjectDataSource3"
DefaultMode="Insert"
OnDataBound="DetailsView1_DataBound"
OnItemCommand="DetailsView1_ItemCommand"
OnItemUpdated="DetailsView1_ItemUpdated">
<Fields>
<asp:BoundField DataField="ForumID" HeaderText="ForumID"
SortExpression="ForumID" ReadOnly="true" InsertVisible="false" />
<asp:BoundField DataField="AddedBy" HeaderText="AddedBy"
SortExpression="AddedBy" ReadOnly="true" InsertVisible="false" />
<asp:BoundField DataField="AddedDate" HeaderText="AddedDate"
SortExpression="AddedDate" ReadOnly="true" InsertVisible="false" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Url" HeaderText="Url" SortExpression="Url" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:BoundField DataField="ImageUrl" HeaderText="ImageUrl"
SortExpression="ImageUrl" />
<asp:TemplateField HeaderText="Forum Group:" InsertVisible="true" SortExpression="ForumGroup">
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server"
DataSourceID="ObjectDataSource4" DataTextField="Key" DataValueField="Value">
</asp:DropDownList>
<asp:ObjectDataSource ID="ObjectDataSource4" runat="server"
SelectMethod="GetDisplayForumGroups" TypeName="CMS.Framework.Forums">
</asp:ObjectDataSource>
</InsertItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server"
DataSourceID="ObjectDataSource5" DataTextField="Key" DataValueField="Value">
</asp:DropDownList>
<asp:ObjectDataSource ID="ObjectDataSource5" runat="server"
SelectMethod="GetDisplayForumGroups" TypeName="CMS.Framework.Forums">
</asp:ObjectDataSource>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Importance" HeaderText="Importance"
SortExpression="Importance" />
<asp:CheckBoxField DataField="Moderated" HeaderText="Moderated"
SortExpression="Moderated" />
<asp:CheckBoxField DataField="Active" HeaderText="Active"
SortExpression="Active" />
<asp:BoundField DataField="UpdatedBy" HeaderText="UpdatedBy"
SortExpression="UpdatedBy" ReadOnly="true" InsertVisible="false" />
<asp:BoundField DataField="UpdatedDate" HeaderText="UpdatedDate"
SortExpression="UpdatedDate" ReadOnly="true" InsertVisible="true" />
<asp:TemplateField>
<InsertItemTemplate>
<asp:LinkButton ID="lnkInsert" runat="server" CommandName="Insert" Text="Insert"></asp:LinkButton>
<asp:LinkButton ID="lnkCancel" runat="server" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</InsertItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lnkUpdate" runat="server" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="lnkCancel" runat="server" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
答案 0 :(得分:0)
你不能直接将参数绑定到你需要的选定值吗? The docs建议类似:
<asp:controlparameter name="anID" controlid="DropDownList1" propertyname="SelectedValue" />