请帮助我的代码 [英] Please help me in my code

查看:78
本文介绍了请帮助我的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好先生,
我想问一下我必须调用后端中保存的值,还是必须在gridview的itemtemplate字段内的下拉菜单中调用它们

我正在使用此代码

hello sir,
I want to ask that I have to call values that is saved in my backend I have to call them in a dropdown that is inside an itemtemplate field in gridview

I m using this code

protected void grdDaily_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataTable dt = new DataTable();
        dt = DelayworkReport.fillProjects();
        ListItem lst = new ListItem("Select", "0");
        DropDownList list = (DropDownList)grdDaily.FindControl("ddlProject");
        list.DataTextField = "ProjectABR";
        list.DataValueField = "ProjectId";
        list.DataSource = dt;
        list.DataBind();
        list.Items.Insert(0, lst);
    }


当我使用F10调试此代码时,调试在


When I debug this code using F10 the debugging stop at

list.DataTextField = "ProjectABR";

处停止,该错误来自对象引用未设置为该对象的实例.请在我的代码中引导我

the error comes of object Reference not set to an instance of the object Please guide me in my code

推荐答案

 protected void grdDaily_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Projects values in drodpown
            dt = DelayworkReport.fillProjects();
            ListItem lst = new ListItem("Select", "0");
            DropDownList list = (DropDownList)e.Row.Cells[5].FindControl("ddlProject");
            list.DataTextField = "ProjectABR";
            list.DataValueField = "ProjectId";
            list.DataSource = dt;
            list.DataBind();
            list.AutoPostBack = true;
            list.Items.Insert(0, lst);
}
}



我已经自己解决了上面的问题,我问过谢谢您的帮助



I have solved by myself the above question I asked thank u for help


不再!在Debugger下运行它并查看发生了什么问题.如果您的报告是正确的,则您对FindControl的调用返回空值.给您的注释:使用FindControl进行常规UI开发是设计错误或错误的标志.通常,您无需搜索任何控件.您可以访问所有控件,为什么要搜索.另外,使用任何立即常量就像您的"dllProject"一样,确实很糟糕.如果拼写错误,编译器将无助于发现问题.从一开始就万事大吉.如果您修复了该错误,其他错误将困扰您.您需要检查代码设计.

—SA
Not again! Is it a problem to run it under Debugger and see what''s going on. If your report is correct, your call to FindControl returned null. A note for you: using FindControl for regular UI development is a sign of incorrect or bad design. Normally, you never need to search for any controls. All your controls are accessible to you, why searching. Also, using any immediate constants like your "dllProject" is really bad. If you misspell it, compiler cannot help to find a problem. Everything is wrong from the very beginning; if you fix this bug, some other will haunt you. You need to review the code design.

—SA


这篇关于请帮助我的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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