This is a migrated thread and some comments may be shown as answers.

User control inside a listView

2 Answers 125 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Yann
Top achievements
Rank 1
Yann asked on 26 Nov 2012, 03:06 PM
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 Answers, 1 is accepted

Sort by
0
Kostadin
Telerik team
answered on 29 Nov 2012, 03:02 PM
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.
0
Yann
Top achievements
Rank 1
answered on 29 Nov 2012, 03:13 PM
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
Tags
ListView
Asked by
Yann
Top achievements
Rank 1
Answers by
Kostadin
Telerik team
Yann
Top achievements
Rank 1
Share this question
or