这是我用来动态生成转发器控件的代码,绑定到 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);
}
}
注意:输出应该像一个表格。一列中的数字和另一列中的字母。