比如有用户和角色这2个对象是多对多关系,而linq2sql是不支持多对多的,这个时候就要自己手工写代码了.

public partial class frmUserEdit : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request["AddNew"] == "1")
{
DetailsView1.DefaultMode = DetailsViewMode.Insert;
DetailsView1.AutoGenerateInsertButton = true;
}
else
{
DetailsView1.DefaultMode = DetailsViewMode.Edit;
}
BindRoleList();
}
}
private void BindRoleList()
{
CheckBoxList cblRole = DetailsView1.FindControl("cblRole") as CheckBoxList;
cblRole.DataTextField = "RoleName";
cblRole.DataValueField = "RoleID";
cblRole.DataSourceID = RolesDataSource.ID;
cblRole.DataBind();
}
private List<long> GetSelectedRoles()
{
CheckBoxList cblRole = DetailsView1.FindControl("cblRole") as CheckBoxList;
List<long> result = new List<long>();
foreach (ListItem item in cblRole.Items)
{
if (item.Selected)
result.Add(long.Parse(item.Value));
}
return result;
}
//roles data bound,check many-to-many to check/uncheck selected
protected void cblRole_DataBound(object sender, EventArgs e)
{
User usr = DetailsView1.DataItem as User;
if (null == usr) return;
CheckBoxList cblRole = sender as CheckBoxList;
foreach (Role r in usr.RoleList)
{
foreach (ListItem item in cblRole.Items)
{
if (r.RoleID.ToString() == item.Value)
item.Selected = true;
}
}
}
}
private void ShowMsg(string ExceptionMsg)
{
lblErr.Text = ExceptionMsg;
}
protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
User usr = new User();
usr.UserID = long.Parse(Request["ID"]);
usr.UserName = e.NewValues["UserName"].ToString();
UserService.Save(usr, GetSelectedRoles(), false);
e.Cancel = true;
ShowMsg("Save Successfully");
}
}
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request["AddNew"] == "1")
{
DetailsView1.DefaultMode = DetailsViewMode.Insert;
DetailsView1.AutoGenerateInsertButton = true;
}
else
{
DetailsView1.DefaultMode = DetailsViewMode.Edit;
}
BindRoleList();
}
}
private void BindRoleList()
{
CheckBoxList cblRole = DetailsView1.FindControl("cblRole") as CheckBoxList;
cblRole.DataTextField = "RoleName";
cblRole.DataValueField = "RoleID";
cblRole.DataSourceID = RolesDataSource.ID;
cblRole.DataBind();
}
private List<long> GetSelectedRoles()
{
CheckBoxList cblRole = DetailsView1.FindControl("cblRole") as CheckBoxList;
List<long> result = new List<long>();
foreach (ListItem item in cblRole.Items)
{
if (item.Selected)
result.Add(long.Parse(item.Value));
}
return result;
}
//roles data bound,check many-to-many to check/uncheck selected
protected void cblRole_DataBound(object sender, EventArgs e)
{
User usr = DetailsView1.DataItem as User;
if (null == usr) return;
CheckBoxList cblRole = sender as CheckBoxList;
foreach (Role r in usr.RoleList)
{
foreach (ListItem item in cblRole.Items)
{
if (r.RoleID.ToString() == item.Value)
item.Selected = true;
}
}
}
}
private void ShowMsg(string ExceptionMsg)
{
lblErr.Text = ExceptionMsg;
}
protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
User usr = new User();
usr.UserID = long.Parse(Request["ID"]);
usr.UserName = e.NewValues["UserName"].ToString();
UserService.Save(usr, GetSelectedRoles(), false);
e.Cancel = true;
ShowMsg("Save Successfully");
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?