ASP .NET Web Controls

Discussion in 'ASP .Net' started by =?Utf-8?B?Y29kZVdhcnJpb3I=?=, May 14, 2004.

  1. 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.
     
    =?Utf-8?B?Y29kZVdhcnJpb3I=?=, May 14, 2004
    #1
    1. Advertising

  2. 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.
     
    =?Utf-8?B?Y29kZVdhcnJpb3I=?=, May 14, 2004
    #2
    1. Advertising

  3. =?Utf-8?B?Y29kZVdhcnJpb3I=?=

    avnrao Guest

    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" <> wrote in message
    news:...
    > 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.
     
    avnrao, May 14, 2004
    #3
  4. 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?
     
    =?Utf-8?B?Y29kZVdhcnJpb3I=?=, May 14, 2004
    #4
  5. =?Utf-8?B?Y29kZVdhcnJpb3I=?=

    avnrao Guest

    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" <> wrote in message
    news:...
    > 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?
     
    avnrao, May 14, 2004
    #5
  6. 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.
     
    =?Utf-8?B?Y29kZVdhcnJpb3I=?=, May 14, 2004
    #6
  7. =?Utf-8?B?Y29kZVdhcnJpb3I=?=

    avnrao Guest

    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" <> wrote in message
    news:...
    >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.
     
    avnrao, May 14, 2004
    #7
  8. 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
     
    =?Utf-8?B?Y29kZVdhcnJpb3I=?=, May 14, 2004
    #8
  9. 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
    }
     
    =?Utf-8?B?Y29kZVdhcnJpb3I=?=, May 14, 2004
    #9
  10. =?Utf-8?B?Y29kZVdhcnJpb3I=?=

    avnrao Guest

    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" <> wrote in message
    news:...
    > 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.
    >
     
    avnrao, May 14, 2004
    #10
  11. 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
     
    =?Utf-8?B?Y29kZVdhcnJpb3I=?=, May 14, 2004
    #11
  12. =?Utf-8?B?Y29kZVdhcnJpb3I=?=

    avnrao Guest

    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" <> wrote in message
    news:...
    > 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;
    > }
    >
     
    avnrao, May 14, 2004
    #12
  13. It does help. You the man! Thanks for everything.
     
    =?Utf-8?B?Y29kZVdhcnJpb3I=?=, May 14, 2004
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. msnews.microsoft.com

    How to Create Web Controls using ASP.NET Controls ?

    msnews.microsoft.com, Apr 27, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    6,270
    msnews.microsoft.com
    Apr 27, 2005
  2. Bob Rock
    Replies:
    1
    Views:
    690
    Cowboy \(Gregory A. Beamer\)
    Sep 17, 2006
  3. Nathan Sokalski
    Replies:
    5
    Views:
    1,084
    Gaurav Vaish \(www.Edujini-Labs.com\)
    Jan 10, 2007
  4. Nathan Sokalski
    Replies:
    4
    Views:
    341
    Nathan Sokalski
    Dec 21, 2006
  5. Nathan Sokalski
    Replies:
    4
    Views:
    378
    Nathan Sokalski
    Dec 21, 2006
Loading...

Share This Page