61,825
社区成员




namespace WebApplication1
{
public partial class test02 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.ListView1.AlternatingItemTemplate = new TempLate("AlternatingItemTemplate");
this.ListView1.EmptyDataTemplate = new TempLate("EmptyDataTemplate");
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)
{
Label UserIDLabel = new Label();
Label UserNameLabel = new Label();
Label GroupIDLabel = new Label();
UserIDLabel.ID="UserIDLabel";
UserIDLabel.Text = "<%# Eval(\"UserID\") %>";
UserNameLabel.ID="UserNameLabel";
GroupIDLabel.ID="GroupIDLabel";
switch (this.templateType)
{
case "AlternatingItemTemplate":
container.Controls.Add(new LiteralControl("<tr><td>"));
container.Controls.Add(UserIDLabel);
container.Controls.Add(new LiteralControl("</td><td>"));
container.Controls.Add(UserNameLabel);
container.Controls.Add(new LiteralControl("</td><td>"));
container.Controls.Add(GroupIDLabel);
container.Controls.Add(new LiteralControl("</td></tr>"));
break;
case "EmptyDataTemplate":
container.Controls.Add(new LiteralControl("<tr></tr>"));
break;
}
}
//endregion
}
}
}
String ConnectionString ="server=.;database=你的数据库;user id=sa;password=sa;min pool size=4;max pool size=512;";