0
这是我用来动态生成中继器控件的代码,绑定到SQL。我只能用C#编码来完成。 aspx部分只有一个“面板”。我能够使用这个获得输出。但是它会在我无法应用任何风格的标签中出现。 我附上了截图。有人请帮我将样式应用到输出。像交替颜色等。谢谢你。在代码后面的代码中添加样式到代码中的动态中继器控件
public partial class Default2 : System.Web.UI.Page
{ // Repeater Control Databinding using Datasource
Repeater Repeater1 = new Repeater();
//Repeater1.DataSource = myDataSet;
//Repeater1.DataBind();
protected void Page_Load(object sender, EventArgs e)
{
string connStr = ("Data Source=LFVMOSS;Initial Catalog=DB_SFG;Persist Security Info=True;User ID=DB_SFG;Password=sfg");
SqlConnection mySQLconnection = new SqlConnection(connStr);
if (mySQLconnection.State == ConnectionState.Closed)
{
mySQLconnection.Open();
}
SqlCommand mySqlSelect = new SqlCommand("select * from List1", mySQLconnection);
mySqlSelect.CommandType = CommandType.Text;
SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlSelect); DataSet myDataSet = new DataSet();
mySqlAdapter.Fill(myDataSet);
// Repeater Control Databinding using Datasource
Repeater1.DataSource = myDataSet;
Repeater1.DataBind();
if (mySQLconnection.State == ConnectionState.Open)
{
mySQLconnection.Close();
}
foreach (RepeaterItem repeatItem in Repeater1.Items)
{
// if condition to add HeaderTemplate Dynamically only Once
if (repeatItem.ItemIndex == 0)
{
RepeaterItem headerItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Header);
HtmlGenericControl hTag = new HtmlGenericControl("h4");
hTag.InnerHtml = "Employee Names";
repeatItem.Controls.Add(hTag);
}
// Add ItemTemplate DataItems Dynamically
RepeaterItem repeaterItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Item);
Label lbl = new Label();
lbl.Text = string.Format("{0} {1} <br />", myDataSet.Tables[0].Rows[repeatItem.ItemIndex]["id"], myDataSet.Tables[0].Rows[repeatItem.ItemIndex]["Name"]);
repeatItem.Controls.Add(lbl);
// Add SeparatorTemplate Dynamically
repeaterItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Separator);
LiteralControl ltrlHR = new LiteralControl();
ltrlHR.Text = "<hr />";
repeatItem.Controls.Add(ltrlHR);
}
// Add Repeater Control as Child Control
// of Panel Control
Panel1.Controls.Add(Repeater1);
}
}
注意:输出应该像一列table.The数字和字母的另一列。
'Repeater'上的'OnDataBound'事件可以让你的东西更干净 – harry180
我在aspx页面上没有中继器控件。所有我的代码只是代码后面。 – nitinvertigo
,但是你可以使用'Repeater'的'event'来代替这个: 'Repeater1.ItemDataBound + = new RepeaterItemEventHandler(Repeater1_ItemDataBound);'并且在方法Repeater1_ItemDataBound中,你可以制作所有的逻辑 – harry180