我有很多这样的工作,并使用这种技术创建了一个下拉列表和一个树视图。用于动态创建控件并将它们返回到使用 jqueryajax 和 C# 完全构建和配置的页面的 ajax 调用。但我坚持从我的类对象创建一个数据表。该控件显然返回了一个双循环gridView,我只是希望它写出返回数据的视图,最终具有gridView CRUD Ops的所有优点。它很简单我做错了你能帮忙吗?
这是我用于创建 GridView 的 C# 代码
[WebMethod]
public static AjaxReturnObject GetProductsByCategoryID(string CategoryID)
{
AjaxReturnObject o = new AjaxReturnObject();
int catID = Convert.ToInt32(CategoryID);
Product.ProductCollection products = new Product().GetProductsByCategoryID(catID);
if (products.Count == 0)
{
o.Message = "There was no data returned";
o.Status = 999;
return o;
}
else
{
// build a new GridView (or List View) for the UI and populate it with data.
// 1: Initialize a object of type DataTable.
DataTable dt = new DataTable();
//2: Initialize a object of type DataRow
DataRow drow;
//3: Initialize enough objects of type DataColumns
DataColumn col1 = new DataColumn("Product Name", typeof(string));
DataColumn col2 = new DataColumn("Product Description", typeof(string));
DataColumn col3 = new DataColumn("Price", typeof(string));
DataColumn col4 = new DataColumn("Col4", typeof(string));
//4: Adding DataColumns to DataTable dt
dt.Columns.Add(col1);
dt.Columns.Add(col2);
dt.Columns.Add(col3);
dt.Columns.Add(col4);
//5: Adding values in DataColumns
for (int i = 0; i < products.Count; i++)
{
foreach (Product item in products)
{
drow = dt.NewRow();
dt.Rows.Add(drow);
dt.Rows[i][col1] = item.ProductName.ToString();// i.ToString();
dt.Rows[i][col2] = item.ProductDescription.ToString();
dt.Rows[i][col3] = String.Format("{0:C}", item.Price);
dt.Rows[i][col4] = String.Format("{0:.00}", item.Price);
}
}
GridView GridView1 = new GridView();
GridView1.DataSource = dt;
GridView1.DataBind();
// Render the new control and return it to the Ajax Return Object
StringWriter tw = new StringWriter();
Html32TextWriter writer = new Html32TextWriter(tw);
GridView1.RenderControl(writer);
writer.Close();
o.Object = tw.ToString();
o.Message = "Result Data Message";
o.Status = 1;
return o;
}
}
}