我正在尝试从后面的其他页面代码设置用户控件中的类的值,但无法使其正常工作。
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class itemtest : System.Web.UI.UserControl
{
public DataRowView DataRowItem { get; set; }
public class Headers
{
public string strItems { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
Headers hdrs = new Headers();
string strItems = hdrs.strItems;
if (strItems != null)
{
List<string> lstHeaders = strItems.Split(',').ToList<string>();
DataTable dtHeaders = new DataTable();
dtHeaders.Columns.Add("Header", typeof(String));
dtHeaders.Columns.Add("Value", typeof(String));
foreach (var item in lstHeaders)
{
DataRow drHeaders = dtHeaders.NewRow();
drHeaders["Header"] = item.ToString();
drHeaders["Value"] = DataRowItem[item.ToString()].ToString();
dtHeaders.Rows.Add(drHeaders);
}
rptHeaders.DataSource = dtHeaders;
rptHeaders.DataBind();
}
}
}
在我的另一个 page.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class ListViewTest : System.Web.UI.Page
{
public string strTableHeaders;
public DataRowView DataRowItem { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
strTableHeaders = "UserID,MachineName";
}
protected void lvExample_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
itemtest itemHeaders = (itemtest)e.Item.FindControl("itemsMain");
if (itemHeaders != null)
itemHeaders.Headers.strItems = strTableHeaders;
}
}
}
我收到错误 Compiler Error Message: CS0572: 'Headers': cannot reference a type through an expression; 尝试 'itemtest.Headers' 代替 line itemHeaders.Headers.strItems = strTableHeaders;
有人帮我吗?我对此很陌生,所以最好用lamen术语
谢谢
编辑:我的旧方法只是
public DataRowView DataRowItem { get; set; }
public string strItems { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (strItems != null)
{
List<string> lstHeaders = strItems.Split(',').ToList<string>();...
但似乎正在发生的事情是我的用户控件页面加载事件在 listview_itemdatabound 事件之前触发,我看不出这是怎么可能的,因为用户控件仅在列表视图中使用......
所以 strItems 始终为空,我的页面加载失败