Click here to Skip to main content
15,885,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a masterpage and contentpages that inherit from it so in one of my contentPage I am putting this code:

XML
<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.
Posted
Updated 10-Sep-13 23:00pm
v2
Comments
Dholakiya Ankit 11-Sep-13 8:21am    
could u get tb1 in html?at runtime?
El Dev 11-Sep-13 8:45am    
Hi dholakiya!Look how I am doing,I do not if you mean like this:
<div id="pnlDate" runat="server">
<table id="tb1" runat="server"></table>
</div>
But also this does not work...Please help fixing this I do not understand what is the problem
Dholakiya Ankit 12-Sep-13 3:23am    
but what about position its looks like u are going another way see you are using table inner attributes in databound its meaning less when you have not filled out data in tb1 so why you are using that data?
Anuja Pawar Indore 11-Sep-13 9:00am    
Is your table "tbl" a server control, as its not runat server
El Dev 11-Sep-13 9:12am    
To be a server control it must have a runat='server' attribute I dont know what do u mean but
wherever I am putting the code it does not work means from
code behind:Table tb = new Table();
tb.ID = "tb1";
and
Html:<table id="tb1" runat="server"></table>
so nothing is working...

Do you see a definition of tb1 within the pnlDate element? I cannot, so I would come to the exact same conclusion as the error.

Good luck!
 
Share this answer
 
Comments
El Dev 11-Sep-13 6:09am    
so where do you want me to put this and why it is working on a page which doest not inherit a masterPage...try it and you will see that is working on a simpler webForm.
E.F. Nijboer 11-Sep-13 6:19am    
You forgot to add the tb to the page. Also pointed out by Neema Derakhshan.
El Dev 11-Sep-13 6:31am    
so what does this code mean:
Table tb = new Table();
tb.ID = "tb1";
I am generating the table dynamically.
and why it is working as normal in a webForm with the same code, please try it and see what I mean...
Neema Derakhshan 12-Sep-13 1:28am    
it can not find Page.FindControl("pnlDate"),it returns null
you should use Quickwatch in these cases :)
put this line out of for loop :

C#
pnlDate.Controls.Add(tb);
 
Share this answer
 
Comments
El Dev 11-Sep-13 6:07am    
I Neema Derakhshan! I have put the code out of the loop but it is still giving me the same error...
 
Share this answer
 
ok dude,after you put that line out of the loop this code guide you to the "tb1" :)
C#
Table t = (Table)Page.Controls[0].Controls[3].Controls[1].FindControl("pnlDate").FindControl("tb1");


hope this help
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900