troubleshooting Question

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

Avatar of awilderbeast
awilderbeastFlag for United Kingdom of Great Britain and Northern Ireland asked on
.NET ProgrammingC#ASP.NET
7 Comments1 Solution2705 ViewsLast Modified:
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>
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;
        }
    }
}
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>
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();
        }
    }

}
ASKER CERTIFIED SOLUTION
awilderbeast

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros