这是我用来动态生成转发器控件的代码,绑定到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);
}
}
注意:输出应该像一个表。一列中的数字和另一列中的字母。
答案 0 :(得分:1)
如果要向控件添加样式,请使用Style
属性。
lbl.Style.Add(HtmlTextWriterStyle.BackgroundColor, "green");
但正如上面的评论所表明的那样,你正以相反的方式解决这个问题。如果您想要像表格一样输出,请使用<table>
代码