10 Replies - 6565 Views - Last Post: 29 January 2012 - 08:14 AM Rate Topic: -----

#1 DritonCA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 28-January 12

Panel in a repeater

Posted 28 January 2012 - 10:41 AM

Panel in a Repeater
Does anyone know what i need to do before to make this work?

I want my button to generate inside a panel,
And I want to do to grab my panel and put the button there. my code looks like this
cmd.Parameters.Add("@udbudtekurser", SqlDbType.Bit).Value = true;

        Conn.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        string tempId = string.Empty;
        while (reader.Read())
        {
            if (reader["KursusKategoriId"].ToString() != tempId)
            {
                tempId = reader["KursusKategoriId"].ToString();
                udbudteKurser.Add(new Udbudtekurser(
                    (int)reader["KursusKategoriId"],
                    (int)reader["Varighed"],
                    (string)reader["Semester"],
                    (DateTime)reader["Begynder"],
                    (int)reader["KursusKatelogId"],
                    (string)reader["Niveau"],
                    (string)reader["Titel"],
                    (string)reader["Billede_Lille"],
                    (string)reader["Kode"],
                    (string)reader["KortBeskrivelse"],
                    (string)reader["Fornavn"],
                    (string)reader["Efternavn"],
                    (bool)reader["Tilmeldning"],
                    (int)reader["Pladser"]));

            }
        }
        Conn.Close();
        Repeater_udbudteKurser.DataSource = udbudteKurser;
        Repeater_udbudteKurser.DataBind();

     
    }
    protected void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            LinkButton LBudbudte = new LinkButton();
            LBudbudte.ID = "1";
            LBudbudte.Text = "Tilmeldning åben";

            Panel panel = (Panel)Repeater_udbudteKurser.FindControl("Panel_Button");
            panel.Controls.Add(LBudbudte);
        }
    }

my aspx looks like this

<asp:Repeater ID="Repeater_udbudteKurser"  OnItemDataBound="R1_ItemDataBound" runat="server">
              <ItemTemplate>
              <a href='kursuskatalog.aspx?kursusid=<%#Eval("KursusKatelogId") %>' "><h1 style="color:#164825; font-family:Arabic Typesetting; font-size:26px;"><%#Eval("Kode") %>&nbsp;:&nbsp;<%#Eval("Titel") %></h1></a><div style=" margin-top:-10px;">
                <p style=" border-bottom:solid 1px black; width:73px; font-size:12px;">Undervises af:</p>
                <p style=" font-size:12px; margin-left:75px; margin-top:-28px;">Ustadh&nbsp;<%#Eval("Fornavn") %>&nbsp;<%#Eval("Efternavn") %></p>
                <p style=" font-size:12px;"><img src="images/Sunni.png" height="134" width="156" style=" float:right; padding-left:25px; padding-bottom:1px; margin-top:-20px" alt="#" /><b>Niveau:</b>&nbsp;<%#Eval("Niveau")%><br /><b>Varighed:</b>&nbsp;<%#Eval("Varighed") %>&nbsp; Uger<br />
                <b>Semester:</b>&nbsp;<%#Eval("Semester") %><br /><b>Begyndelsesdato:</b>&nbsp;<%#Eval("Begynder", "{0:d.MMMM yyyy}")%><br /><b>Beskrivelse:&nbsp;</b><%#Eval("KortBeskrivelse")%><br /><a href='kursuskatalog.aspx?kursusid=<%#Eval("KursusKatelogId") %>' style=" border-bottom: 1px solid blue; font-size:12px;">Detaljer</a>
                    </p>
                  <asp:Panel ID="Panel_Button" runat="server">
                  </asp:Panel>


I've tried to solve it yourself, you just can not make it. no matter what I do I get the error

myself

This post has been edited by modi123_1: 28 January 2012 - 10:38 AM
Reason for edit:: please use the code tags..


Is This A Good Question/Topic? 0
  • +

Replies To: Panel in a repeater

#2 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6519
  • View blog
  • Posts: 14,408
  • Joined: 02-June 10

Re: Panel in a repeater

Posted 28 January 2012 - 10:53 AM

View PostDritonCA, on 28 January 2012 - 11:41 AM, said:

no matter what I do I get the error


What error do you get?
What does this error message mean?
Was This Post Helpful? 0
  • +
  • -

#3 DritonCA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 28-January 12

Re: Panel in a repeater

Posted 28 January 2012 - 10:57 AM

Object reference not set to an instance of an object.


Line 402: panel.Controls.Add(LBudbudte);
Was This Post Helpful? 0
  • +
  • -

#4 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 183
  • View blog
  • Posts: 785
  • Joined: 27-October 11

Re: Panel in a repeater

Posted 28 January 2012 - 11:10 AM

We have a separate ASP.NET forum:

ASP.NET Forum

The most probable reason is:

It didn't find the control called "Panel_Button". And thus didn't instantiate the variable panel, and thus it will generate an error if you try to add a control to a non-existent panel.

This line is the problem:
Panel panel = (Panel)Repeater_udbudteKurser.FindControl("Panel_Button");



It didn't find the control "Panel_Button". Are you sure there is a control with that name?

And when saying on which line it breaks, please don't say the number line since it's relative to the code you provided us. Tell us on which line in the code provided or just copy paste the exact line.

This post has been edited by RexGrammer: 28 January 2012 - 11:11 AM

Was This Post Helpful? 0
  • +
  • -

#5 DritonCA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 28-January 12

Re: Panel in a repeater

Posted 28 January 2012 - 11:14 AM

View PostRexGrammer, on 28 January 2012 - 11:10 AM, said:

We have a separate ASP.NET forum:

ASP.NET Forum

The most probable reason is:

It didn't find the control called "Panel_Button". And thus didn't instantiate the variable panel, and thus it will generate an error if you try to add a control to a non-existent panel.

This line is the problem:
Panel panel = (Panel)Repeater_udbudteKurser.FindControl("Panel_Button");



It didn't find the control "Panel_Button". Are you sure there is a control with that name?

And when saying on which line it breaks, please don't say the number line since it's relative to the code you provided us. Tell us on which line in the code provided or just copy paste the exact line.


see the bottom of the code, where my aspx-code is. and the last piece of line is my panel with Panel_Button
Was This Post Helpful? 0
  • +
  • -

#6 DritonCA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 28-January 12

Re: Panel in a repeater

Posted 28 January 2012 - 11:21 AM

Maybe there is a more simpel way to get my panel, i have been on google all day didnt find anything
Was This Post Helpful? 0
  • +
  • -

#7 DritonCA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 28-January 12

Re: Panel in a repeater

Posted 28 January 2012 - 11:26 AM

This is my aspx full

 <asp:Repeater ID="Repeater_udbudteKurser" OnItemDataBound="R1_ItemDataBound" runat="server">
              <ItemTemplate>
              <a href='kursuskatalog.aspx?kursusid=<%#Eval("KursusKatelogId") %>' "><h1 style="color:#164825; font-family:Arabic Typesetting; font-size:26px;"><%#Eval("Kode") %>&nbsp;:&nbsp;<%#Eval("Titel") %></h1></a><div style=" margin-top:-10px;">
                <p style=" border-bottom:solid 1px black; width:73px; font-size:12px;">Undervises af:</p>
                <p style=" font-size:12px; margin-left:75px; margin-top:-28px;">Ustadh&nbsp;<%#Eval("Fornavn") %>&nbsp;<%#Eval("Efternavn") %></p>
                <p style=" font-size:12px;"><img src="images/Sunni.png" height="134" width="156" style=" float:right; padding-left:25px; padding-bottom:1px; margin-top:-20px" alt="#" /><b>Niveau:</b>&nbsp;<%#Eval("Niveau")%><br /><b>Varighed:</b>&nbsp;<%#Eval("Varighed") %>&nbsp; Uger<br />
                <b>Semester:</b>&nbsp;<%#Eval("Semester") %><br /><b>Begyndelsesdato:</b>&nbsp;<%#Eval("Begynder", "{0:d.MMMM yyyy}")%><br /><b>Beskrivelse:&nbsp;</b><%#Eval("KortBeskrivelse")%><br /><a href='kursuskatalog.aspx?kursusid=<%#Eval("KursusKatelogId") %>' style=" border-bottom: 1px solid blue; font-size:12px;">Detaljer</a>
                    </p>
                  <asp:Panel ID="Panel_Button" runat="server">
                  </asp:Panel>
                 <br />
                 </div> 
              </ItemTemplate>
              </asp:Repeater>



this is my C# full
        Panel_visdropdown.Visible = true;
        MultiView_VisKursus.SetActiveView(View_udbudteKurser);
        List<kurser.Udbudtekurser> udbudteKurser = new List<kurser.Udbudtekurser>();
        SqlConnection Conn;
        SqlCommand cmd;
        SqlConnection(out Conn, out cmd);
        cmd.CommandType = CommandType.StoredProcedure;
       cmd.CommandText = "udbudteKurserVarighed";
       cmd.Parameters.Add("@udbudtekurser", SqlDbType.Bit).Value = true;

        Conn.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        string tempId = string.Empty;
        while (reader.Read())
        {
            if (reader["KursusKategoriId"].ToString() != tempId)
            {
                tempId = reader["KursusKategoriId"].ToString();
                udbudteKurser.Add(new kurser.Udbudtekurser(
                    (int)reader["KursusKategoriId"],
                    (int)reader["Varighed"],
                    (string)reader["Semester"],
                    (DateTime)reader["Begynder"],
                    (int)reader["KursusKatelogId"],
                    (string)reader["Niveau"],
                    (string)reader["Titel"],
                    (string)reader["Billede_Lille"],
                    (string)reader["Kode"],
                    (string)reader["KortBeskrivelse"],
                    (string)reader["Fornavn"],
                    (string)reader["Efternavn"],
                    (bool)reader["Tilmeldning"],
                    (int)reader["Pladser"]));

            }


        }
        Conn.Close();
        Repeater_udbudteKurser.DataSource = udbudteKurser;
        Repeater_udbudteKurser.DataBind();


        
        
        
    }
    protected void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            LinkButton LBudbudte = new LinkButton();
            LBudbudte.ID = "1";
            LBudbudte.Text = "Tilmeldning åben";

            Panel panel = (Panel)Repeater_udbudteKurser.FindControl("Panel_Button");
            panel.Controls.Add(LBudbudte);
        }
    }




I only when to catch my panel, and for each course I have, it must generate a button for it.
But i cant catch my button :(
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6519
  • View blog
  • Posts: 14,408
  • Joined: 02-June 10

Re: Panel in a repeater

Posted 28 January 2012 - 11:28 AM

View PostDritonCA, on 28 January 2012 - 11:57 AM, said:

Object reference not set to an instance of an object.


Did you even look at the tutorial I linked to you?
What does this error message mean?

Object reference not set to an instance of an object is the very first error covered.

This post has been edited by tlhIn`toq: 28 January 2012 - 11:30 AM

Was This Post Helpful? 0
  • +
  • -

#9 DritonCA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 28-January 12

Re: Panel in a repeater

Posted 28 January 2012 - 11:52 AM

Yep
Was This Post Helpful? 0
  • +
  • -

#10 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Panel in a repeater

Posted 28 January 2012 - 03:57 PM

Your problem is that the Panel control that you are looking for is not in the Repeater itself. It's in the Item. There is a FindControl method for the e.Item. Maybe you could try that instead.

We have an ASP.Net forum for this reason. Your problem is specific to ASP.Net, not C#. Therefore, your question should be in the ASP.Net forum.
Was This Post Helpful? 0
  • +
  • -

#11 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 448
  • View blog
  • Posts: 1,504
  • Joined: 28-April 09

Re: Panel in a repeater

Posted 29 January 2012 - 08:14 AM

if you are using the ItemDatabound event then the way to access a panel inside the ItemTemplate would be to access the control through the Item as eclipsed4utoo said.

            RepeaterItem item = e.Item;

            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
            {
                Control ctrl = item.FindControl("Panel1");
                if (ctrl is Panel)
                {
                    // Add code here.
                }
                else
                {
                    // Do something if the control is not a Panel
                }
            }



I'll usually put a label on the page and print an error message to the page with the appropriate error message. You could also just debug and step through it to see if the control is null, whichever you're more comfortable with.

This post has been edited by Nakor: 29 January 2012 - 03:30 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1