zoukankan      html  css  js  c++  java
  • 使用linq2sql 的DetailView 如何保存多对多关系

    比如有用户和角色这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");

            }

        }


  • 相关阅读:
    Mybatis配置文件中Insert 元素标签添加配置有哪些呢?
    Mybatis配置文件中Select元素标签输入参数有多少种输入方式呢?
    Mybatis配置文件如何进行配置呢?
    Centos安装 Apache Benchmark
    本地连接阿里云上的mysql centos
    python 导出项目需要的库
    Nginx报错:nginx: [error] OpenEvent("Global gx_reload_14944") failed (2: The system cannot find the file specified)
    windows安装uwsgi报错 AttributeError: module 'os' has no attribute 'uname'
    P3346 [ZJOI2015]诸神眷顾的幻想乡(广义后缀自动机)
    P6139 【模板】广义后缀自动机(广义 SAM)
  • 原文地址:https://www.cnblogs.com/zitjubiz/p/linq2sql_detailview_manytomany.html
Copyright © 2011-2022 走看看