User control inside a listView

3 posts, 0 answers
  1. Yann
    Yann avatar
    2 posts
    Member since:
    Nov 2012

    Posted 26 Nov 2012 Link to this post

    Hi !

    I'm currently working on a website with asp.NET 4.0 and c#.
    I need to implemente a User control in a listview. I want the listview to show a list of data from a table name Lignes (with a ligne_id as primary key) and the user control to show a list of data from a table name Blocks (with a block_id as primary key and a ligne_id as foreign key).

    Here's the code I made :

    My Listview :

    <asp:ListView ID="ListView1" runat="server" DataKeyNames="ligne_id" DataSourceID="SqlDataSourceLigne">
        <ItemTemplate>
                <asp:Label ID="ligne_id" ClientIDMode="Static" runat="server" Text='<%# Eval("ligne_id") %>' />
                <uc1:UC_ListeBlock ID="UC_ListeBlock1" runat="server" Ligne_ID='<%# Eval("ligne_id") %>' />
        </ItemTemplate>
    </asp:ListView>


    My UserControl :

    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>


    My user control's code Behind :

    public partial class UC_ListeBlock : System.Web.UI.UserControl
    {
        public Int32 Ligne_ID;
     
        private Int32 ligne_ID
        {
            get { return Ligne_ID; }
            set { Ligne_ID = value; }
        }
     
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Label1.Text = ligne_ID.ToString();
            }
        }
    }


    When I execute this code, the label ligne_id from the listview has the good ligne_id from the database (so Text='<%# Eval("ligne_id") %>' is working) but the Label1 from the user control is always at 0 (so Ligne_ID='<%# Eval("ligne_id") %>' doesn't seem to work).

    Do you have an idea what is wrong with my code ?

    Thanks in advance for you awnser

    P.S : I'm french so I'm sorry if I'm not very clear in my explanation.
  2. Kostadin
    Admin
    Kostadin avatar
    1708 posts

    Posted 29 Nov 2012 Link to this post

    Hi Yann,

    I noticed that your example the property ligne_ID is private and it should be public. Check out the following code snippet in order to fix it.
    User Control Mark Up:
    <asp:Label ID="Label1" runat="server" Text='<%# ligne_ID %>' ></asp:Label>

    User Control Code Behind:
    public Int32 Ligne_ID;
     
        public Int32 ligne_ID
        {
            get
            {
                return Ligne_ID;
            }
            set
            {
                Ligne_ID = value;
            }
        }


    Greetings,
    Kostadin
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Yann
    Yann avatar
    2 posts
    Member since:
    Nov 2012

    Posted 29 Nov 2012 Link to this post

    Hi,

    Thanks for your reply.

    I also found that in that case, Ligne_ID is initialized after the Page_Load(). So I have to move the Label1.Text = ligne_ID.ToString(); to the Page_PreRender().

    Regards

    Yann
Back to Top