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