Repeater 控件是模板化的数据绑定列表,Repeater 控件是“无外观的”,即:它不具有任何内置布局或样式,也就不会产生任何数据控制表格来控制数据的显示。因此,我们必须在控件的模板中明确声明所有 HTML 布局标记、格式标记和样式标记。
分页:
protected void Button6_Click(object sender, EventArgs e)
{
this.BindProduct("1");
}
protected void Button9_Click(object sender, EventArgs e)
{
int count = Convert.ToInt32(this.HiddenField2.Value);
this.BindProduct(count.ToString());
}
protected void Button7_Click(object sender, EventArgs e)
{
int index = Convert.ToInt32(this.HiddenField1.Value);
if (index > 0)
index--;
this.BindProduct(index.ToString());
}
protected void Button8_Click(object sender, EventArgs e)
{
int index = Convert.ToInt32(this.HiddenField1.Value);
int count = Convert.ToInt32(this.HiddenField2.Value);
if (index < count)
index++;
this.BindProduct(index.ToString());
}
private void BindProduct(string pageindex)
{
string str = ConfigurationManager.ConnectionStrings["studentCnn"].ConnectionString;
using (SqlConnection sqlCnn = new SqlConnection(str))
{
SqlDataAdapter da = new SqlDataAdapter("sp_Student_Select_by_Page_rowNumber", sqlCnn);
da.SelectCommand.Parameters.AddWithValue("@pageIndex", pageindex);
da.SelectCommand.Parameters.Add("@pageCount", SqlDbType.Int).Direction = ParameterDirection.Output;
da.SelectCommand.Parameters.AddWithValue("@pageSize", 2);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();
da.Fill(ds);
this.DataList1.DataSource = ds.Tables[0].DefaultView;
this.DataList1.DataBind();
this.HiddenField1.Value = pageindex;
this.HiddenField2.Value = da.SelectCommand.Parameters["@pageCount"].Value.ToString();
}
}
Treeview数据的动态绑定:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindTree();
}
}
private void BindTree()
{
DataTable dt = GetData();
this.FillNode(dt,null);
}
private void FillNode(DataTable dt,TreeNode no)
{
DataView dv = new DataView(dt);
if (no == null)
{
dv.RowFilter = "parentid='0'";
}
else
{
dv.RowFilter = "parentid='" + no.Value + "'";
}
foreach (DataRowView item in dv)
{
TreeNode node = new TreeNode(item["menuname"].ToString(), item["menuid"].ToString());
FillNode(dt, node);
if (no == null)
{
this.TreeView1.Nodes.Add(node);
}
else
{
no.ChildNodes.Add(node);
}
}
}
private DataTable GetData()
{
string str = ConfigurationManager.ConnectionStrings["sqlstr"].ConnectionString;
using (SqlConnection cnn=new SqlConnection(str))
{
SqlCommand cmm = cnn.CreateCommand();
cmm.CommandText = "select * from MenuTree order by ParentID,MenuOrder";
SqlDataAdapter da = new SqlDataAdapter(cmm);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
}