private
void
Page_Load(
object
sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
//构造当前时间
this.txtTime.Text = ClassLib.getCurrentTime();
//构造天气列表
this.ddlWeather.DataSource = ClassLib.getWeatherList();
this.ddlWeather.DataValueField = "Id";
this.ddlWeather.DataTextField = "Weather";
this.ddlWeather.DataBind();
//按用户构造文章分类
this.ddlCategory.DataSource = ClassLib.getCategoryList(ClassLib.getUserId());
this.ddlCategory.DataValueField = "Id";
this.ddlCategory.DataTextField = "Title";
this.ddlCategory.DataBind();
}
}
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
//构造当前时间
this.txtTime.Text = ClassLib.getCurrentTime();
//构造天气列表
this.ddlWeather.DataSource = ClassLib.getWeatherList();
this.ddlWeather.DataValueField = "Id";
this.ddlWeather.DataTextField = "Weather";
this.ddlWeather.DataBind();
//按用户构造文章分类
this.ddlCategory.DataSource = ClassLib.getCategoryList(ClassLib.getUserId());
this.ddlCategory.DataValueField = "Id";
this.ddlCategory.DataTextField = "Title";
this.ddlCategory.DataBind();
}
}
为了增加程序的封装性,我把对数据库的查询动作封装到了一个统一的全局类库中(ClassLib.cs)。这个文件中包含了几个查询动作。这里举例其一:
//
得到天气列表
static public IList getWeatherList()
{
IList listWeather = Mapper.Instance().QueryForList("SelectAll",null);
return listWeather;
}
// 得到分类列表
static public IList getCategoryList( int _userId)
{
IList listCategory = Mapper.Instance().QueryForList("SelectByUserId", _userId);
return listCategory;
}
static public IList getWeatherList()
{
IList listWeather = Mapper.Instance().QueryForList("SelectAll",null);
return listWeather;
}
// 得到分类列表
static public IList getCategoryList( int _userId)
{
IList listCategory = Mapper.Instance().QueryForList("SelectByUserId", _userId);
return listCategory;
}
绑定动作是很容易懂的。声明DropDownList的数据源DataSource,接着绑定结果集中的特定列到Value和Text就可以了。