添加控件列表并循环翻转 [英] Add control to list and loop overthem

查看:56
本文介绍了添加控件列表并循环翻转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的asp页面中有多个GridView,我希望通过将它们插入列表来循环它们

然而这会在运行时抛出异常

i知道我遗失的东西



我的代码是:





列表< object> grdvw; 
受保护 void Page_Load( object sender,EventArgs e)
{


grdvw.Add(GridView1);
grdvw.Add(GridView2);
grdvw.Add(GridView3);
grdvw.Add(GridView4);
grdvw.Add(GridView5);
grdvw.Add(GridView6);
grdvw.Add(GridView7);
grdvw.Add(GridView8);
grdvw.Add(GridView9);
grdvw.Add(GridView10);

// 不计算清单:
var waitingDoc =(来自 wdoc db.waitings
join doc in db.Emplyees on wdoc.DoctorId equals doc.EmpID
< span class =code-sdkkeyword> where wdoc.dismissed!= 2
select new {doc.EmpName,doc.EmpID})。Distinct();
int doctors = waitingDoc.Count();

docCountlbl.Text = doctors.ToString();

int li = 0 ;
if (waitingDoc.Count()> 0
{

foreach var docdata in waitingDoc)
{

// 使div和gridview可见

foreach (Control TempControl in this .Form.Controls)
{
if (TempControl GridView&& TempControl.ID == grdvw [li])
{

GridView TempGridView = TempControl as GridView;
TempGridView.Visible = true ;
}
}

foreach (Control TempControl in this .Form.Controls)
{
if (TempControl.ID = = divesion [li])
{

TempControl.Visible = true ;
}
}
// 清除数据
waiting.Clear();



// 列heades

string DocName =( from doctr in db.Emplyees
其中 doctr.EmpID == docdata.EmpID
选择 doctr.EmpName)。 Single ();
waiting.Columns.Add( postion);
waiting.Columns.Add( Dr + DocName);
waiting.Columns.Add( s);

// 获取数据和索引
var witingList = 来自等待 db.waitings
join doc in db.Emplyees on wait.DoctorId equals doc.EmpID
join cli in db.Clints on wait.clintID equals cli.ClintID
where wait.dismissed!= 2 && doc.EmpID == docdata.EmpID
选择 new {ClintName = cli.Name,position = wait.dismissed};

int noFrows = witingList.Count();
int i = 1 ;
foreach var item in witingList)
{
DataRow lin = waiting.NewRow();
lin [ 2 ] = i;
lin [ 1 ] = item.ClintName.ToString();


if (item.position == 1
{
lin [ 0 ] = 诊所;

}

if (item.position == 0
{
lin [ 0 ] = 等待;

}
if (item.position == 3
{
lin [ 0 ] = 成品;

}

i ++;
waiting.Rows.Add(lin);
}



}


}
}
}

















当我运行它给我这个例外





< pre lang =text>对象引用未设置为对象的实例。
描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例。

来源错误:


第18行:
第19行:
第20行:grdvw.Add(GridView1);
第21行:grdvw.Add(GridView2);
第22行:grdvw.Add(GridView3);

解决方案

对我来说,看起来你的全局变量grdvw可能没有被初始化。在你的PageLoad功能中你可以试试这个。



 如果( grdvw ==  null 

{
grdvw = new List< ;对象>();
}


I have multiple GridView in my asp page and i want to loop over them by inserting them in list
however this throw an exception on run time
i know that Im missing something

my code is :


 List<object> grdvw;
 protected void Page_Load(object sender, EventArgs e)
    {
        
        
        grdvw.Add(GridView1);
        grdvw.Add(GridView2);
        grdvw.Add(GridView3);
        grdvw.Add(GridView4);
        grdvw.Add(GridView5);
        grdvw.Add(GridView6);
        grdvw.Add(GridView7);
        grdvw.Add(GridView8);
        grdvw.Add(GridView9);
        grdvw.Add(GridView10);

 //counting no of list:
        var waitingDoc = (from wdoc in db.waitings
                          join doc in db.Emplyees on wdoc.DoctorId equals doc.EmpID
                          where wdoc.dismissed != 2
                          select new { doc.EmpName, doc.EmpID }).Distinct();
        int doctors = waitingDoc.Count();

        docCountlbl.Text = doctors.ToString();

        int li = 0;
        if (waitingDoc.Count() > 0)
        {

            foreach (var docdata in waitingDoc)
            {

                //make div and gridview visible

                foreach (Control TempControl in this.Form.Controls)
                {
                    if (TempControl is GridView && TempControl.ID == grdvw[li])
                    {

                        GridView TempGridView = TempControl as GridView;
                        TempGridView.Visible = true;
                    }
                }

                foreach (Control TempControl in this.Form.Controls)
                {
                    if (TempControl.ID == divesion[li])
                    {

                        TempControl.Visible = true;
                    }
                }
                //clear the data
                waiting.Clear();



                //column heades        

                string DocName = (from doctr in db.Emplyees
                                  where doctr.EmpID == docdata.EmpID
                                  select doctr.EmpName).Single();
                waiting.Columns.Add("postion");
                waiting.Columns.Add("Dr " + DocName);
                waiting.Columns.Add("s");

                // get data and index
                var witingList = from wait in db.waitings
                                 join doc in db.Emplyees on wait.DoctorId equals doc.EmpID
                                 join cli in db.Clints on wait.clintID equals cli.ClintID
                                 where wait.dismissed != 2 && doc.EmpID == docdata.EmpID
                                 select new { ClintName = cli.Name, position = wait.dismissed };

                int noFrows = witingList.Count();
                int i = 1;
                foreach (var item in witingList)
                {
                    DataRow lin = waiting.NewRow();
                    lin[2] = i;
                    lin[1] = item.ClintName.ToString();


                    if (item.position == 1)
                    {
                        lin[0] = "Clinic";

                    }

                    if (item.position == 0)
                    {
                        lin[0] = "waiting";

                    }
                    if (item.position == 3)
                    {
                        lin[0] = "finished";

                    }

                    i++;
                    waiting.Rows.Add(lin);
                }



            }


        }
    }
}









when i run it it give me this exception


Object reference not set to an instance of an object. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 


Line 18:         
Line 19:         
Line 20:         grdvw.Add(GridView1);
Line 21:         grdvw.Add(GridView2);
Line 22:         grdvw.Add(GridView3); 

解决方案

To me it looks your global variable grdvw might not be initialized. In your PageLoad Function you could try something this.

if(grdvw == null)

 {
    grdvw = new List<object>();
 }


这篇关于添加控件列表并循环翻转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆