Avatar of awilderbeast
awilderbeast
Flag 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

ASP.NETC#.NET Programming

Avatar of undefined
Last Comment
awilderbeast

8/22/2022 - Mon
Aaron Jabamani

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?
awilderbeast

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

Thanks
Aaron Jabamani

Page load will be called everytime controltest.ascx.cs is loaded. so have a logic here to read the headers....
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
awilderbeast

ASKER
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
awilderbeast

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Aaron Jabamani

Glad you found the fix...
awilderbeast

ASKER
As above
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.