Link to home
Start Free TrialLog in
Avatar of awilderbeast
awilderbeastFlag for United Kingdom of Great Britain and Northern Ireland

asked on

c#,.net User Control inside list view item template is loading before item template...!?

Hi all,

I have a user control inside an item template a value of that user control is set on an itemdatabound event. the onyl issue im having is that the page load of the user control is happening before itemdatabound for some reason, please see code below.

ive run visual studio debug on the itemdatabound event, but i get an error on controltest.ascx.cs
            List<string> lstHeaders = strItems.Split(',').ToList<string>();

before it gets to the debug on itemdatabound

Thanks

testpage.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ListViewTest.aspx.cs" Inherits="ListViewTest" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="frmMain" runat="server">
        <div>
            <asp:ListView ID="lvExample" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="lvExample_ItemDataBound">
                <LayoutTemplate>
                    <table border="1">
                        <tr>
                            <td>&nbsp;</td>
                            <td>User ID</td>
                            <td>Machine Name</td>
                        </tr>
                        <tr id="itemPlaceholder" runat="server"></tr>
                    </table>
                </LayoutTemplate>
                <ItemTemplate>
                    <td><asp:Button ID="butEdit" runat="server" Text="Edit" CommandName="Edit" /></td>
                    <C:Test runat="server" ID="itemsMain" DataRowItem='<%# GetDataItem()%>' ClientIDMode="Static" />
                </ItemTemplate>
                <EditItemTemplate>
                    <td><asp:Button ID="butEdit" runat="server" Text="Cancel" CommandName="Cancel" ClientIDMode="Static" /></td>
                    <C:Test runat="server" ID="itemsMain" DataRowItem='<%# GetDataItem()%>' ClientIDMode="Static"  />
                </EditItemTemplate>
            </asp:ListView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                ConnectionString="<%$ ConnectionStrings:ITAPPConnectionString %>"
                SelectCommand="sp_Equipment_Select_Users"
                SelectCommandType="StoredProcedure"
                UpdateCommand="sp_Equipment_Update"
                UpdateCommandType="StoredProcedure">
                <UpdateParameters>
                    <asp:Parameter Name="ID" Type="Int32" />
                    <asp:Parameter Name="UserID" Type="Int32" />
                    <asp:Parameter Name="MachineName" Type="String" />
                </UpdateParameters>
            </asp:SqlDataSource>
        </div>
    </form>

</body>
</html>

Open in new window

test.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ListViewTest : System.Web.UI.Page
{
    public string strTableHeaders;
    public DataRowView DataRowItem { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        strTableHeaders = "UserID,MachineName";
    }

    protected void lvExample_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            var itemHeaders = (itemtest)e.Item.FindControl("itemsMain");
            if (itemHeaders != null)
                itemHeaders.strItems = strTableHeaders;
        }
    }
}

Open in new window

controltest.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="itemtest.ascx.cs" Inherits="itemtest" %>
<asp:Repeater runat="server" ID="rptHeaders">
    <ItemTemplate>
        <td><%# Eval("Value") %></td>
    </ItemTemplate>
</asp:Repeater>

Open in new window

controltest.ascx.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class itemtest : System.Web.UI.UserControl
{
    public DataRowView DataRowItem { get; set; }
    public string strItems { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (strItems != null)
        {
            List<string> lstHeaders = strItems.Split(',').ToList<string>();
            DataTable dtHeaders = new DataTable();
            dtHeaders.Columns.Add("Header", typeof(String));
            dtHeaders.Columns.Add("Value", typeof(String));
            foreach (var item in lstHeaders)
            {
                DataRow drHeaders = dtHeaders.NewRow();
                drHeaders["Header"] = item.ToString();
                drHeaders["Value"] = DataRowItem[item.ToString()].ToString();
                dtHeaders.Rows.Add(drHeaders);
            }
            rptHeaders.DataSource = dtHeaders;
            rptHeaders.DataBind();
        }
    }

}

Open in new window

Avatar of Aaron Jabamani
Aaron Jabamani
Flag of United Kingdom of Great Britain and Northern Ireland image

Why you want to pass the headers from aspx to user control . Instead place them there itself. Or instead of setting the property can you call a method in user control?
Avatar of awilderbeast

ASKER

Becuase the strHeaders will change each time i use the control. could you show me an example of calling a method?

Thanks
Page load will be called everytime controltest.ascx.cs is loaded. so have a logic here to read the headers....
But sureley controltest.ascx isnt loaded until it loads the item template?

the first time its called is on the item template, so logic to me states it should go

page load
item load
control page load

no?
ASKER CERTIFIED SOLUTION
Avatar of awilderbeast
awilderbeast
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Glad you found the fix...
As above