C# - 相同的按钮但返回不同的值

时间:2015-08-15 09:37:27

标签: c# asp.net loops button gridview

目前我的结果如下:

enter image description here

当我按组1的“发送请求”按钮时,它将组2的值存储到数据库中。它假设存储组1的值,例如组号和全名。我想我的循环错了。有谁愿意帮助我?

的Page_Load

protected void Page_Load(object sender, EventArgs e)
        {
            Invite invite = new Invite();
            List<Invite> inviteList = new List<Invite>();
            InviteManager inviteManager = new InviteManager();
            SystemUser account = new SystemUser();
            SystemUserManager accountManager = new SystemUserManager();
            List<Project> projectList = new List<Project>();
            ProjectManager projectManager = new ProjectManager();
            Group group = new Group();
            Group groupX = new Group();
            List<Group> groupList = new List<Group>();
            List<Group> groupListX = new List<Group>();
            List<Group> groupListY = new List<Group>();
            GroupManager groupManager = new GroupManager();
            DataTable dt = new DataTable();

            SystemUser accountA = new SystemUser();
            SystemUser accountB = new SystemUser();
            bool status = false;
            string emailAddress;

            try
            {
                groupList = groupManager.getAvailableGroup();

                if (groupList.Count == 0)
                {
                    divMessage.InnerHtml = "There is no available groups.";
                }
                else
                {
                    for (int i = 0; i < groupList.Count; i++)
                    {
                        group = groupList[i];

                        Label lblOne = new Label();
                        lblOne.Text = "Group No.: " + group.groupNo + "</br>Group Leader: " + group.userName + "</br>Group Members:</br>";
                        form1.Controls.Add(lblOne);

                        groupListX = groupManager.getGroupMember(group.groupNo);

                        GridView grdMember = new GridView();
                        grdMember.AutoGenerateColumns = false;

                        BoundField btnFAdmissionNo = new BoundField();
                        btnFAdmissionNo.HeaderText = "Admission No";
                        btnFAdmissionNo.DataField = "userAdminNo";
                        grdMember.Columns.Add(btnFAdmissionNo);

                        BoundField btnFFullName = new BoundField();
                        btnFFullName.HeaderText = "Full Name";
                        btnFFullName.DataField = "userName";
                        grdMember.Columns.Add(btnFFullName);

                        grdMember.DataSource = groupListX;
                        grdMember.DataBind();
                        form1.Controls.Add(grdMember);

                        Label lblTwo = new Label();
                        lblTwo.Text = "Project selection:</br>";
                        form1.Controls.Add(lblTwo);

                        groupListY = groupManager.getGroupChoice(group.groupNo);

                        GridView grdProjectSelection = new GridView();
                        grdProjectSelection.AutoGenerateColumns = false;

                        BoundField btnFFirstChoice = new BoundField();
                        btnFFirstChoice.HeaderText = "First Choice";
                        btnFFirstChoice.DataField = "firstChoiceX";
                        grdProjectSelection.Columns.Add(btnFFirstChoice);

                        BoundField btnFSecondChoice = new BoundField();
                        btnFSecondChoice.HeaderText = "Second Choice";
                        btnFSecondChoice.DataField = "secondChoicex";
                        grdProjectSelection.Columns.Add(btnFSecondChoice);

                        BoundField btnFThirdChoice = new BoundField();
                        btnFThirdChoice.HeaderText = "Third Choice";
                        btnFThirdChoice.DataField = "thirdChoiceX";
                        grdProjectSelection.Columns.Add(btnFThirdChoice);

                        BoundField btnFForthChoice = new BoundField();
                        btnFForthChoice.HeaderText = "Forth Choice";
                        btnFForthChoice.DataField = "forthChoiceX";
                        grdProjectSelection.Columns.Add(btnFForthChoice);

                        grdProjectSelection.DataSource = groupListY;
                        grdProjectSelection.DataBind();
                        form1.Controls.Add(grdProjectSelection);

                        Button btnSendRequest = new Button();
                        btnSendRequest.Text = "Send Request";
                        btnSendRequest.Click += delegate
                        {
                            emailAddress = HttpContext.Current.User.Identity.Name;

                            groupX = groupManager.getGroupLeader(group.groupNo);
                            accountB = accountManager.getAccInfoByAdminNo(groupX.userAdminNo);

                            invite.senderEmailAddress = emailAddress;
                            invite.groupNo = groupX.groupNo;
                            invite.userName = accountB.userName;
                            invite.userAdminNo = Convert.ToString(accountB.userAdminNo);
                            invite.recipientEmailAddress = accountB.recipientEmailAddress;

                            status = inviteManager.sendInvite(invite);
                        };
                        form1.Controls.Add(btnSendRequest);

                        Label lblThree = new Label();
                        lblThree.Text = "</br></br>";
                        form1.Controls.Add(lblThree);
                    }
                }
            }
            catch (Exception)
            {
                divMessage.InnerHtml = "Unable to retrieve data. Please contact administrator if the problem persists.";
            }
        }

1 个答案:

答案 0 :(得分:1)

在您的委托中,您正在使用在for循环外声明的组变量。这就是它始终包含groupList的最后一个条目的原因。 而不是

group = groupList[i];

DO

Group group = groupList[i];