日期:2014-05-18  浏览次数:20767 次

关于ASP.NET页面绑定问题
我想将数据库里的数据取出来然后在每个对应的lable上显示。我的做法如下。但是我感觉这样太麻烦了,也太笨了。因为初学所以只想到了这种办法。不知道还有没有更好,更简单方法。将数据很快高效率的绑定到控件上。
C# code

//绑定方法
public void Bind()
        {
            string meetingID = Request.Params["MeetingCountID"];
            string sql = "select [name],[startDate],[finishDate],[applyDate],[content]," +
                                 "case when [MettingType]=1 then '所有人员' else [participator] end as participator," +
                                 "case when [startDate]<getdate() and getdate()<[finishDate] then '正在进行' else case when getdate()<[startDate] then '未开始' else '已结束' end end as estate ,[BoardroomName]," +
                                 "[EmpName],[DeptName],[BranchName],[MeetingCount].[MeetingCountID],[Boardroom].[BoardroomID],[MeetingCount].[Servers],[MeetingCount].[Nature],"+
                                 "[MeetingCount].[participatorID],cast([MeetingUser].[DepID] as varchar(5))+'*'+cast([MeetingUser].[EmpID] as varchar(5)) as [IDS],[MeetingCount].[approve],[MeetingCount].[approveDate] " +
                         "from [MeetingCount] join [Boardroom] on [MeetingCount].[BoardroomID]=[Boardroom].[BoardroomID]" +
                                 "join [MeetingUser] on [MeetingCount].[MeetingUserID]=[MeetingUser].[MeetingUserID]" + 
                         "where MeetingCount.[MeetingCountID]=" + meetingID;
            ArrayList al = (new Meeting()).GetDataOdd(sql);
            nameLab.Text = al[0].ToString();
            startDateLab.Text = al[1].ToString();
            finishDateLab.Text = al[2].ToString();
            applyDateLab.Text = al[3].ToString();
            contentLab.Text = al[4].ToString();
            participatorLab.Text = al[5].ToString();
            estateLab.Text = al[6].ToString();
            BranchNameLab.Text = al[10].ToString();
            EmpNameLab.Text = al[8].ToString();
            DeptNameLab.Text = al[9].ToString();
            boardroomLab.Text = al[7].ToString();
            Label2.Text = al[11].ToString();//会议ID
            Label1.Text = al[12].ToString();//会议室ID
            serverLab.Text = al[13].ToString();
            NatureLab.Text = al[14].ToString();
            participator2Lab.Text = al[15].ToString();
            EmpIDs.Text = al[16].ToString();
            approveLab.Text = al[17].ToString();
            approveDateLab.Text = al[18].ToString();
            al.Clear();
        }



------解决方案--------------------
拖一个FormView到设计界面,指定数据源,选择SqlDataSource,把上面的Sql语句拷进去,最后部分改成where MeetingCount.[MeetingCountID]=@meetingID;
下一步时会问@mettingID从哪里获得,选择QueryString, 名称:MeetingCountID

结束。

FormView会自动为你生成一大堆Label.