ASP .NET Web Controls

G

Guest

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.
 
G

Guest

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.
 
A

avnrao

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.
 
G

Guest

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?
 
A

avnrao

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.
 
G

Guest

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.
 
A

avnrao

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.
 
G

Guest

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
 
G

Guest

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
}
 
A

avnrao

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.
 
G

Guest

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_ItemCommand)

private void repeaterNameHere_ItemCommand(object sender, RepeaterCommandEventArgs e

e.Item.FindControl("tableNameHere").Visible = true
 
A

avnrao

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top