我在ASP.net C#中创建了一个带有Dropdownlist的用户控件(以下称为LookupGrid)。现在在主窗体中,我想为Combobox设置数据源,但是在为组合框设置数据源的行上却收到NullExceptions
主要网络表单中的代码:
LookupGrid droplist = new LookupGrid();
droplist.ID = field.Name;
RecordSet lookupvalues = inRecs.ServiceClient.WebServiceClient.GetInstance().GetLookup("CurrentNRS_Admit", Convert.ToInt64(Session["UserID"].ToString()), field);
droplist.SetData(lookupvalues.ToDataSet());
在用户控件中:
protected void Page_Load(object sender, EventArgs e)
{
}
public void SetData(DataSet dTLookupValues)
{
DropDownList1.DataSource = dTLookupValues.Tables[0];
}
我收到错误消息:
Object reference not set to an instance of an object.
在这行上:
DropDownList1.DataSource = dTLookupValues.Tables[0];
创建属性并通过public方法设置数据表。在页面加载中,您可以绑定数据
public DataTable MyData { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
DropDownList1.DataSource =MyData ;
DropDownList1.DataBind();
}
public void SetData(DataSet dTLookupValues)
{
MyData = dTLookupValues.Tables[0];
}
在您的主页上
protected void Page_Load(object sender, EventArgs e)
{
var control = (LookupGrid )LoadControl("~/LookupGrid.ascx");
control.SetData(lookupvalues.ToDataSet());
Panel1.Controls.Add(control); //add control to your page or panel
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句