How to Create a listview ItemTenplate with c# not

MeChecksV 2008-05-03 09:14:27
RT

I am using vs2008 c# net3.5
Listview web control
...全文
111 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
MeChecksV 2008-05-03
  • 打赏
  • 举报
回复
TO SHOW DYNAMIC COLUMN
MeChecksV 2008-05-03
  • 打赏
  • 举报
回复
i just want to use listview control
崔鹏飞 2008-05-03
  • 打赏
  • 举报
回复
what do you wanna do?
To show the data?
why do you wanna do it throug such an ancient way?
May be gridview can help you
(it sucks,my e language)
MeChecksV 2008-05-03
  • 打赏
  • 举报
回复
Is there anyone can help me??
I read this article
http://msdn.microsoft.com/en-us/library/0e39s2ck.aspx

but i don't know how to bind then data in ItemTemplate

MY CODE;

public partial class DynamicList : System.Web.UI.Page
{

static void Item_DataBinding(object sender, System.EventArgs e)
{
PlaceHolder ph = (PlaceHolder)sender;
ListViewItem ri = (ListViewItem)ph.NamingContainer;
String UserIDValue = (String)DataBinder.Eval(ri.DataItem, "UserID");
String UserNameValue = (String)DataBinder.Eval(ri.DataItem, "UserName");
String GroupIDValue = (String)DataBinder.Eval(ri.DataItem, "GroupID");
((Label)ph.FindControl("UserID")).Text = UserIDValue.ToString();
((Label)ph.FindControl("UserName")).Text = UserNameValue.ToString();
((Label)ph.FindControl("GroupID")).Text = GroupIDValue.ToString();
}

protected void Page_Load(object sender, EventArgs e)
{
ListView1.AlternatingItemTemplate = new TempLate("AlternatingItemTemplate");
// ListView1.LayoutTemplate = new TempLate("LayoutTemplate");
ListView1.EmptyDataTemplate = new TempLate("EmptyDataTemplate");
ListView1.ItemTemplate = new TempLate("ItemTemplate");
DataSet ResultSet = RunQuery("Select * From UserInfo");
ListView1.DataSource = ResultSet;
ListView1.DataBind();

}
DataSet RunQuery(String QueryString)
{

// Declare the connection string. This example uses Microsoft SQL Server
// and connects to the Northwind sample database.
String ConnectionString = "Data Source=XU-AE42BC5C42AC;Initial Catalog=uzone;Persist Security Info=True;User ID=sa;Password=sa";

SqlConnection DBConnection = new SqlConnection(ConnectionString);
SqlDataAdapter DBAdapter;
DataSet ResultsDataSet = new DataSet();

try
{

// Run the query and create a DataSet.
DBAdapter = new SqlDataAdapter(QueryString, DBConnection);
DBAdapter.Fill(ResultsDataSet);

// Close the database connection.
DBConnection.Close();

}
catch (Exception ex)
{

// Close the database connection if it is still open.
if (DBConnection.State == ConnectionState.Open)
{
DBConnection.Close();
}

Message.Text = "Unable to connect to the database.";

}

return ResultsDataSet;

}

public class TempLate : ITemplate
{
private string templateType = "";
public TempLate(string type)
{
this.templateType = type;
}

//region ITemplate Members

public void InstantiateIn(Control container)
{
PlaceHolder ph = new PlaceHolder();
PlaceHolder itemPlaceholder = new PlaceHolder();
itemPlaceholder.ID = "itemPlaceholder";

Label UserIDLabel = new Label();
Label UserNameLabel = new Label();
Label GroupIDLabel = new Label();
UserIDLabel.ID = "UserIDLabel";
UserIDLabel.Text = "test";
UserNameLabel.ID = "UserNameLabel";
GroupIDLabel.ID = "GroupIDLabel";
switch (this.templateType)
{
case "AlternatingItemTemplate":
ph.Controls.Add(new LiteralControl("<tr><td>"));
ph.Controls.Add(UserIDLabel);
ph.Controls.Add(new LiteralControl("</td><td>"));
ph.Controls.Add(UserNameLabel);
ph.Controls.Add(new LiteralControl("</td><td>"));
ph.Controls.Add(GroupIDLabel);
ph.Controls.Add(new LiteralControl("</td></tr>"));
break;
case "LayoutTemplate":
ph.Controls.Add(new LiteralControl("<table runat=\"server\">"));
ph.Controls.Add(new LiteralControl("<tr runat=\"server\">"));
ph.Controls.Add(new LiteralControl("<td runat=\"server\">"));
ph.Controls.Add(new LiteralControl("<tr runat=\"server\">"));
ph.Controls.Add(new LiteralControl("<table ID=\"itemPlaceholderContainer\" runat=\"server\" border=\"1\">"));
ph.Controls.Add(new LiteralControl("<tr runat=\"server\">"));
ph.Controls.Add(new LiteralControl("<th runat=\"server\">UserID</th>"));
ph.Controls.Add(new LiteralControl("<th runat=\"server\">UserName</th>"));
ph.Controls.Add(new LiteralControl("<th runat=\"server\">GroupID</th></tr>"));
ph.Controls.Add(new LiteralControl("<tr ID=\"itemPlaceholder\" runat=\"server\"></tr>"));
ph.Controls.Add(new LiteralControl("</table></td></tr>"));
break;

case "EmptyDataTemplate":
ph.Controls.Add(new LiteralControl("<tr></tr>"));
break;
case "ItemTemplate":
ph.Controls.Add(new LiteralControl("<tr><td>"));
ph.Controls.Add(UserIDLabel);
ph.Controls.Add(new LiteralControl("</td><td>"));
ph.Controls.Add(UserNameLabel);
ph.Controls.Add(new LiteralControl("</td><td>"));
ph.Controls.Add(GroupIDLabel);
ph.Controls.Add(new LiteralControl("</td></tr>"));
ph.DataBinding += new EventHandler(Item_DataBinding);
break;



}

}



//endregion
}
Pig23 2008-05-03
  • 打赏
  • 举报
回复
what the hell language?

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧