Ejemplo n.º 1
0
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            using (var conn = new SqlConnection(DataAccess.ConnectString))
            {
                conn.Open();
                var trans = conn.BeginTransaction();

                try
                {
                    if (string.IsNullOrEmpty(ddlGroupLeague.SelectedValue) ||
                        !lbLeagueTeam.Items.Cast<ListItem>().Any(x => x.Selected))
                    { throw new Exception("请选择联赛分类与分组球队"); }

                    var g = new Group();

                    if (GroupGuid != Guid.Empty)
                    {
                        g = _repo.Single<Group>(GroupGuid);
                    }
                    else
                    {
                        g.ID = new Guid(tbGroupGuid.Text);
                    }

                    g.GroupName = tbGroupName.Text;
                    g.GroupOrder = Convert.ToInt16(tbGroupOrder.Text);
                    g.RankMethod = (RankMethodType)Enum.Parse(typeof(RankMethodType), rblRankMethod.SelectedValue);
                    g.IsTable = cbIsTable.Checked;

                    g.LeagueGuid = new Guid(ddlGroupLeague.SelectedValue);

                    #region ListBox Multiple Value for RelationGroupTeam

                    if (GroupGuid != Guid.Empty)
                    {
                        var rgts = RelationGroupTeam.QueryByGroupGuid(GroupGuid);

                        if (rgts.Count > 0)
                        {
                            foreach (var rgt in rgts)
                            {
                                rgt.Delete(trans);
                            }
                        }
                    }

                    foreach (ListItem item in lbLeagueTeam.Items)
                    {
                        Guid tGuid;
                        if (item.Selected && Guid.TryParse(item.Value, out tGuid))
                        {
                            var gt = new RelationGroupTeam
                            {
                                GroupGuid = g.ID,
                                TeamGuid = tGuid
                            };

                            gt.Insert(trans);
                        }
                    }

                    #endregion

                    if (GroupGuid != Guid.Empty)
                    {
                        _repo.Update(g, trans);
                        trans.Commit();

                        ClientScript.RegisterClientScriptBlock(typeof(string), "success", "alert('更新分组成功');", true);
                    }
                    else
                    {
                        _repo.Insert(g, trans);
                        trans.Commit();

                        ClientScript.RegisterClientScriptBlock(typeof(string), "success", "alert('添加分组成功');", true);
                    }
                }
                catch (Exception ex)
                {
                    trans.Rollback();

                    ClientScript.RegisterClientScriptBlock(typeof(string), "failed", $"alert('{ex.Message}')", true);
                }
            }
        }