由于contentPage,对象引用未设置为对象的实例 [英] Object reference not set to an instance of an object due to the contentPage
问题描述
我有一个继承的主页和内容页面,因此在我的一个内容页面中,我正在使用此代码:
< asp:内容 ID = Content2 ContentPlaceHolderID = ContentPlaceHolder1 Runat = 服务器 >
< div >
< asp:TextBo x ID = txtNumber runat = server >
< / asp:TextBox >
< br / >
< asp:按钮 ID = btn runat = server 文字 = 生成 / >
< asp:Panel ID = pnlDate < span class =code-attribute> runat = server >
< / asp:Panel >
&l t; / div >
< div >
< asp:按钮 ID = btnSave runat = server 文字 = 保存 OnClick = btn_Click / >
< asp:标签 ID = lblInsert runat = 服务器 > < span class =code-keyword>< / asp:Label >
< / div >
< / asp:Content > ;
,背后的代码是:
protected void Page_Load(对象发送r,EventArgs e)
{
GenerateTxtB();
}
protected void GenerateTextBoxes_onClick(object sender,EventArgs e)
{
pnlDate.Controls.Clear();
GenerateTxtB();
}
private void GenerateTxtB()
{
if(txtNumber.Text!= string.Empty)
{
int number = Convert.ToInt32(txtNumber.Text);
string top =;
int left = 0;
TextBox txt;
表tb = new Table();
tb.ID =tb1;
TableRow tr = new TableRow();
for(int i = 1; i< = number; i ++)
{
Label lb = new Label();
lb.ID =lbFname+ i.ToString();
lb.Text =名字:;
txt = new TextBox();
txt.ID =textBox+ i.ToString();
txt.Attributes.Add(runat,server);
txt.CssClass =myDates;
txt.Width = Unit.Pixel(240);
txt.MaxLength = 1;
txt.EnableViewState = true;
txt.Style.Add(HtmlTextWriterStyle.Position,relative);
txt.Style [HtmlTextWriterStyle.Left] = left +px;
剩下+ = 5;
RequiredFieldValidator rf = new RequiredFieldValidator();
rf.ControlToValidate =textBox+ i.ToString();
rf.ID =RF+ i.ToString();
rf.Text =需要日期;
rf.ForeColor = System.Drawing.Color.Red;
rf.ErrorMessage =需要日期;
rf.Display = ValidatorDisplay.Dynamic;
rf.SetFocusOnError = true;
pnlDate.Controls.Add(rf);
TableCell tc = new TableCell();
tc.Controls.Add(txt);
tr.Controls.Add(tc);
tb.Controls.Add(tr);
pnlDate.Controls.Add(tb);
}
}
}
public void btn_Click(object sender,EventArgs e)
{
表t =(表)Page.FindControl(pnlDate)。FindControl(tb1);
TextBox文本框;
foreach(TableRow tr in t.Rows)
{
foreach(TableCell tc in tr.Cells)
{
foreach(Control c in tc) .Controls)
{
if(c.GetType()== typeof(TextBox))
{
textbox =(TextBox)c;
Response.Write(TextBox Value is:+ textbox.Text +< br />);
}
}
}
}
}
这个代码wotk在页面中没有继承主页而不是在contentPage中,我收到此错误:
Quote:对象引用未设置为对象的实例。
...
它指向这行代码:
Quote:表t =(表)Page.FindControl(pnlDate)。FindControl(tb1);
请一些人告诉我为什么和如何解决这个问题。
您是否在tb1
的定义> pnlDate 元素?我不能,所以我会得出与错误完全相同的结论。
祝你好运!
把这一行排除在外for for循环:
pnlDate.Controls.Add(tb);
参考此主题
http://forums.asp.net/t/ 1807044.aspx [ ^ ]
和本文
http://www.dotnetcurry.com/ ShowArticle.aspx?ID = 135 [ ^ 一>
Hi, I have a masterpage and contentpages that inherit from it so in one of my contentPage I am putting this code:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div>
<asp:TextBox ID="txtNumber" runat="server">
</asp:TextBox>
<br />
<asp:Button ID="btn" runat="server" Text="Generate" />
<asp:Panel ID="pnlDate" runat="server">
</asp:Panel>
</div>
<div>
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btn_Click" />
<asp:Label ID="lblInsert" runat="server"></asp:Label>
</div>
</asp:Content>
and the code behind is:
protected void Page_Load(object sender, EventArgs e) { GenerateTxtB(); } protected void GenerateTextBoxes_onClick(object sender, EventArgs e) { pnlDate.Controls.Clear(); GenerateTxtB(); } private void GenerateTxtB() { if (txtNumber.Text != string.Empty) { int number = Convert.ToInt32(txtNumber.Text); string top = ""; int left = 0; TextBox txt; Table tb = new Table(); tb.ID = "tb1"; TableRow tr = new TableRow(); for (int i = 1; i <= number; i++) { Label lb = new Label(); lb.ID = "lbFname" + i.ToString(); lb.Text = "First Name :"; txt = new TextBox(); txt.ID = "textBox" + i.ToString(); txt.Attributes.Add("runat", "server"); txt.CssClass = "myDates"; txt.Width = Unit.Pixel(240); txt.MaxLength = 1; txt.EnableViewState = true; txt.Style.Add(HtmlTextWriterStyle.Position, "relative"); txt.Style[HtmlTextWriterStyle.Left] = left + "px"; left += 5; RequiredFieldValidator rf = new RequiredFieldValidator(); rf.ControlToValidate = "textBox" + i.ToString(); rf.ID = "RF" + i.ToString(); rf.Text = "Date are required"; rf.ForeColor = System.Drawing.Color.Red; rf.ErrorMessage = "Date is required"; rf.Display = ValidatorDisplay.Dynamic; rf.SetFocusOnError = true; pnlDate.Controls.Add(rf); TableCell tc = new TableCell(); tc.Controls.Add(txt); tr.Controls.Add(tc); tb.Controls.Add(tr); pnlDate.Controls.Add(tb); } } } public void btn_Click(object sender, EventArgs e) { Table t = (Table)Page.FindControl("pnlDate").FindControl("tb1"); TextBox textbox; foreach (TableRow tr in t.Rows) { foreach (TableCell tc in tr.Cells) { foreach (Control c in tc.Controls) { if (c.GetType() == typeof(TextBox)) { textbox = (TextBox)c; Response.Write("TextBox Value is:" + textbox.Text + "<br/>"); } } } } }
This code wotk in page that doest not inherit the master page but not in a contentPage, I am getting this error:
Quote:Object reference not set to an instance of an object.
...
It is pointing to this line of code :
Quote:Table t = (Table)Page.FindControl("pnlDate").FindControl("tb1");
Please can some tell me why and how to resolve this issue.
Do you see a definition oftb1
within thepnlDate
element? I cannot, so I would come to the exact same conclusion as the error.
Good luck!
put this line out of for loop :
pnlDate.Controls.Add(tb);
Refer this thread
http://forums.asp.net/t/1807044.aspx[^]
and this article
http://www.dotnetcurry.com/ShowArticle.aspx?ID=135[^]
这篇关于由于contentPage,对象引用未设置为对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!