请更正此错误 [英] Please corrct this error Exeception

查看:128
本文介绍了请更正此错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

源错误:


136行:
137行://currentTitleOfCourtesy =((TextBox)r.FindControl("TitleOfCourtesyTextBox")).Text;
第138行:currentLastName =((TextBox)r.FindControl("Name")).Text;
第139行:currentFirstName =((TextBox)r.FindControl("Location")).Text;
第140行://currentTitle =((TextBox)r.FindControl("TitleTextBox")).Text;


我在线路附近出现错误

currentLastName =(((TextBox)r.FindControl("Name")).Text;

感谢您的提前重播

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

Source Error:


Line 136:
Line 137: //currentTitleOfCourtesy = ((TextBox)r.FindControl("TitleOfCourtesyTextBox")).Text;
Line 138: currentLastName = ((TextBox)r.FindControl("Name")).Text;
Line 139: currentFirstName = ((TextBox)r.FindControl("Location")).Text;
Line 140: //currentTitle = ((TextBox)r.FindControl("TitleTextBox")).Text;


I got Error Near The line

currentLastName = ((TextBox)r.FindControl("Name")).Text;

Thanks for your advance replay

推荐答案

您在r控件上没有带有ID"Name"的控件,无论此r是什么.确保r上具有TextBox ID="Name",并且如果这样做,请在浏览器中查看页面源-如果您使用母版页,则ID可能会更改
You don''t have a control with ID "Name" on your r control, whatever this r is. Ensure you have TextBox with ID="Name" on r and, if you do, view page source in the browser - ID could change if you use master pages


hi ....

在这里,我认为如果您要在绑定控件之前尝试设置文本,可能会导致该错误

只需在绑定控件后尝试此代码.


hi....

here I think if You are trying to set text before binding your control that may give that error

just try this code after binding your control.


int i = 0;
        foreach (GridViewRow row in GridView1.Rows)
        {
                ((TextBox)row.FindControl("TextBox1")).Text = "Hello";
         

            i++;
        }



这将完美地工作



This will work perfectly


使用FindControl通常是错误且不可靠的.

如果拼错名称会怎样?编译器不会发现任何错误.在通常设计的应用程序中,您不需要查找控件,因为您在窗体类中拥有所有变量.至少在使用找到的引用之前检查找到的控件是否为null.您的问题只是在引用为null时尝试对找到的控件进行操作.

—SA
Using FindControl is generally bad and unreliable.

What happens if you misspell a name? A compiler won''t find any errors. In a normally designed application you never need to find a control, because you have all the variables in your form classes. At least check up found control for null before using the found reference. You problem is just an attempt to operate on found control when its reference is null.

—SA


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

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