By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,211 Members | 2,227 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,211 IT Pros & Developers. It's quick & easy.

ASP .NET Web Controls

P: n/a
I'm having trouble working with web controls from the C# code behind file
Some controls are accessible to me programatically, and some are not

For example, I trying to hide, or display an <asp:Table> on post back o
the page, here is my code

HTM

<asp:Table id="commentTable
runat="Server"><asp:TableRow><asp:TableCell>Som
Text</asp:TableCell></asp:TableRow></asp:Table

C# Code Behin

protected System.Web.UI.WebControls.Table commentTable

private void Page_Load(object sender, System.EventArgs e

commentTable.Visible = isPostBack
I get this error anytime I run the app

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

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

Source Error

Line 37: commentTable.Visible = isPostBack

If you have experienced a similiar problem, please help.
Nov 18 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Also, I want to mention that this only seems to be happening when I embed the table in a repeater in the <ItemTemplate></ItemTemplate

Thanks.
Nov 18 '05 #2

P: n/a
you should access the table thru Repeater. rather than accessing the table,
you can set Repeater.visible property if only the table exists in the
repeater.

Av.
"codeWarrior" <an*******@discussions.microsoft.com> wrote in message
news:CE**********************************@microsof t.com...
Also, I want to mention that this only seems to be happening when I embed
the table in a repeater in the <ItemTemplate></ItemTemplate>

Thanks.

Nov 18 '05 #3

P: n/a
Thanks for the response, however

1.) I want to hide/display more than just the repeater (surrounding controls)
2.) This error still occurs on the repeater itself (which by the way is nested in another repeater)

I think this error is occuring because the nested table isn't created yet for some reason. Like I said, if it isn't in the repeater, it works fine

I thought the load event fired after everything was created?
Nov 18 '05 #4

P: n/a
when you include tables inside the repeater control, you will not see this
code..
protected System.Web.UI.WebControls.Table commentTable;

it will only appear only if you add the table using drag/drop in design
mode. i think you have created one table in code behind with same name as
that in the repeater control and you are accessing the other one (not the
table in repeater control). can you paste your complete code..

Av.

"codeWarrior" <an*******@discussions.microsoft.com> wrote in message
news:5D**********************************@microsof t.com...
Thanks for the response, however,

1.) I want to hide/display more than just the repeater (surrounding
controls).
2.) This error still occurs on the repeater itself (which by the way is
nested in another repeater).

I think this error is occuring because the nested table isn't created yet
for some reason. Like I said, if it isn't in the repeater, it works fine.

I thought the load event fired after everything was created?

Nov 18 '05 #5

P: n/a
I think you are correct. I didn't want to use the designer to layout the page, so I manually coded the layout with HTML. I added the table to the HTML source, and then declared it in the code behind. So, rather than posting my code wich basically illustrates this, allow me to ask a question

If I don't want to use the designer, how do I get the code behind to look at the HTML element

If you still want to see the all the code, I will post it. Oh, and thanks so much for your assistance. I searched, and searched on this to no avail.
Nov 18 '05 #6

P: n/a
to set the visible property of the asp:table control, you should access it.
but i am not sure when you want to set the visible property.

when repeater control is bound to a datasource, it creates an asp:table for
each row in the source..right.. so you will see so many tables. if you want
to set visible property of table in a row, you should do it in any of the
events of repeater like

Repeater1_ItemCreated or Repeater1_ItemCommand.
e.Item.FindControl("tablename") returns the table object in that row.

hth,
if you can clearly state when you want to set visible property, i can focus
on it.

Av.

"codeWarrior" <an*******@discussions.microsoft.com> wrote in message
news:1B**********************************@microsof t.com...
I think you are correct. I didn't want to use the designer to layout the
page, so I manually coded the layout with HTML. I added the table to the
HTML source, and then declared it in the code behind. So, rather than
posting my code wich basically illustrates this, allow me to ask a
question:

If I don't want to use the designer, how do I get the code behind to look
at the HTML element?

If you still want to see the all the code, I will post it. Oh, and thanks
so much for your assistance. I searched, and searched on this to no avail.

Nov 18 '05 #7

P: n/a
If I do it on the Repeater1_ItemCommand, will it then output the HTML? I've tried this, and because it's initially invisible, the HTML isn't rendered. In other words, when I set the visible property to true, it still doesn't show up

private void repeaterNameHere_ItemCommand(object sender, RepeaterCommandEventArgs e

System.Web.UI.Control ctl = e.Item.FindControl("commentTablel").Visible = true
I know it gets in here, due to the break point I inserted. Also the ctl is set to the correct control when examined in the Autos pane

Nov 18 '05 #8

P: n/a
on which item command it comes up here? on click of something right? what is
it?. when on ItemCreated is it set to false?
the html will be rendered anyway irrespective of the visible property, only
thing is when to set it true is question?

Av.

"codeWarrior" <an*******@discussions.microsoft.com> wrote in message
news:8D**********************************@microsof t.com...
If I do it on the Repeater1_ItemCommand, will it then output the HTML?
I've tried this, and because it's initially invisible, the HTML isn't
rendered. In other words, when I set the visible property to true, it
still doesn't show up.

private void repeaterNameHere_ItemCommand(object sender,
RepeaterCommandEventArgs e)
{
System.Web.UI.Control ctl = e.Item.FindControl("commentTablel").Visible =
true;
}

I know it gets in here, due to the break point I inserted. Also the ctl is
set to the correct control when examined in the Autos pane.

Nov 18 '05 #9

P: n/a
Oops, messed that code up a little when typing here's what I meant

private void repeaterNameHere_ItemCommand(object sender, RepeaterCommandEventArgs e

e.Item.FindControl("commentTablel").Visible = true
}
Nov 18 '05 #10

P: n/a
On ItemCreated it is set to false

On the ItemCreated event of a repeater is when the event fires and the sub control is set to true

repeaterNameHere.ItemCommand += new RepeaterCommandEventHandler(repeaterNameHere_ItemC ommand)

private void repeaterNameHere_ItemCommand(object sender, RepeaterCommandEventArgs e

e.Item.FindControl("tableNameHere").Visible = true
Nov 18 '05 #11

P: n/a
hey this is what is tried and it works for me..

repeater control html :
<asp:repeater id="Repeater1" runat="server">
<ItemTemplate>
<asp:Button ID="button1" CommandName="select" Visible="True"
Runat="server" Text="set"></asp:Button>
<asp:Table ID="Table1" Runat="server" Visible="true">
<asp:TableRow>
<asp:TableCell>one</asp:TableCell>
</asp:TableRow>
</asp:Table>
</ItemTemplate>
</asp:repeater>

ItemCreated : e.Item.FindControl("Table1").Visible = false;
ItemCommand : e.Item.FindControl("Table1").Visible = true;

Page_Load :

if(!IsPostBack)
{
object[] arr = new object[] {"one","two","three"};
Repeater1.DataSource = arr;
Repeater1.DataBind();
}

it is setting the visible property to true. check if you have IsPostBack and
you are not binding DataSource everytime.

hth,
Av.

"codeWarrior" <an*******@discussions.microsoft.com> wrote in message
news:A7**********************************@microsof t.com...
On ItemCreated it is set to false.

On the ItemCreated event of a repeater is when the event fires and the sub
control is set to true:

repeaterNameHere.ItemCommand += new
RepeaterCommandEventHandler(repeaterNameHere_ItemC ommand);

private void repeaterNameHere_ItemCommand(object sender,
RepeaterCommandEventArgs e)
{
e.Item.FindControl("tableNameHere").Visible = true;
}

Nov 18 '05 #12

P: n/a
It does help. You the man! Thanks for everything.
Nov 18 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.