[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 999
  • Last Modified:

asp.net access a assigned usercontrol from codebehind

Hi all,

Im trying to access a usercontrol ive put in a aspx page from the codebehind, hopefully the below will explain...

thanks for any advice

(PS im trying to create a usercontrol for sorting my listview)

use in aspx page
<C:Sort ID="srtSupplier" strColumn="Supplier" runat="server" />

Open in new window

use in aspx page code behind
    protected void Page_Load(object sender, EventArgs e)
    {
            srtSupplier. <---------- nothing comes here, underlined red!
    }

Open in new window


UserControl Code
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="sorting.ascx.cs" Inherits="asset_sorting" %>
<asp:ImageButton ID="srtAsc" runat="server" ImageUrl="/Content/images/sort_asc.png" OnClick="srtAsc_Click" /><br />
<asp:ImageButton ID="srtDesc" runat="server" ImageUrl="/Content/images/sort_desc.png" OnClick="srtDesc_Click"  />

Open in new window


UserControl Code Behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class asset_sorting : System.Web.UI.UserControl
{
    public ListView lstView { get; set; }
    public string strColumn { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void srtDesc_Click(object sender, ImageClickEventArgs e)
    {
        lstView.Sort(strColumn, SortDirection.Ascending);
    }
    protected void srtAsc_Click(object sender, ImageClickEventArgs e)
    {
        lstView.Sort(strColumn, SortDirection.Descending);
    }
}

Open in new window

0
awilderbeast
Asked:
awilderbeast
1 Solution
 
Christopher KileCommented:
I think I need to see the page where you're using this UserControl, the HTML and the code-behind.  Can you place them in code blocks and send them, please?  Thanks in advance.
0
 
YZlatCommented:
Try using Page.FindControl("srtSupplier")
0
 
awilderbeastAuthor Commented:
the code where im using them is in the first two code blocks, ive registered the user control in the web config file as "C:Sort"

but i cant access the control from the page behind by ID, i need to get its ID so i can pass the listview to it
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
Gerry BartleySoftware EngineerCommented:
Can you post the web config entry for your control
Should be something like.....
<controls>
<add tagPrefix="C" src="~/Controls/sorting.ascx" tagName="sort"/>
</controls>

It's possible that your usercontrol is not in same path as your config

Try dropping your usercontrol onto the page in design mode and it should put in the reference correctly. See if codebehind then picks up the control.
Then copy this src path to webconfig and remove the page reference


eg.
<%@ Register src="~/Controls/sorting.ascx" tagName="sort" TagPrefix="C"  %>

If this doesn't work try rebuilding the control and the page
0
 
awilderbeastAuthor Commented:
yup thats already in there

      <controls>
        <add tagPrefix="C" tagName="Sort" src="~/controls/sorting.ascx"/>
        <add tagPrefix="M" tagName="Side" src="~/controls/usermenu.ascx"/>
      </controls>

i have no errors in the aspx page when i use it there as <C:Sort... but when i want the ID of the control ive put in there eg on page_load i put srtAssettNo. it fails.

the issue seems to be i cant access a created control from the code behind, but the control itself seems fine

<asp:Content ID="Content" ContentPlaceHolderID="MainContent" runat="Server">
    <asp:ScriptManager ID="smItems" runat="server" />
    <asp:UpdatePanel ID="upItems" runat="server">
        <ContentTemplate>
            <asp:Label runat="server" ID="lblOutput"/>
            <asp:ListView ID="lvUserItems" runat="server" DataSourceID="dsUserItems">
                <LayoutTemplate>
                    <table cellpadding="0" cellspacing="0" class="gridview" ID="tblUserItems">
                        <thead>
                            <tr class="header">
                                <th></th>
                                <th>Asset No <C:Sort ID="SrtAssetNo" runat="server" trColumn="AssetNo"  /></th>
                                <th>Asset Type</th>
                                <th>Serial No</th>
                                <th>Order Number</th>
                                <th>Supplier <C:Sort ID="srtSupplier" runat="server" strColumn="Supplier"  /></th>
                                <th>Supplied Date</th>
                            </tr>
                        </thead>
                        <tr id="itemPlaceholder" runat="server" />
                    </table>
                </LayoutTemplate>

Open in new window

0
 
Gerry BartleySoftware EngineerCommented:
It's because it's inside a listview
You need the following

((asset_sorting)lvUserItems.FindControl("srtSupplier")). ---------------
0
 
awilderbeastAuthor Commented:
and i need to do this on page load, or in listview on load etc?

im not sure how to access this really, can you show me an example?

Thanks
0
 
Gerry BartleySoftware EngineerCommented:
On page load as in your original post

protected void Page_Load(object sender, EventArgs e)
    {
//            srtSupplier. <---------- nothing comes here, underlined red!
// do this instead.
((asset_sorting)lvUserItems.FindControl("srtSupplier"))., control properties/methods should appear in intellisense

    }
0
 
awilderbeastAuthor Commented:
i think it worked but i cant test becuase of other errors haha

how do i loop through listview edititemtemplate items?

i have the below, but btnSave is in the EditItemTemplate and i dont know how to get it

ive tried

foreach (ListViewDataItem i in lvUserItems.EditItem) but it doesnt like that!

foreach (ListViewDataItem i in lvUserItems.Items)
            {

                LinkButton btnEdit = (LinkButton)i.FindControl("btnEdit");
                Button btnSave = (Button)i.FindControl("btnSave");

Open in new window

0
 
Gerry BartleySoftware EngineerCommented:
What other errors?
.items is the collection of data items on the current page of the listview. Nothing to do with buttons you're trying to access
I'm not really sure what you're trying to do?
Have a look at the following
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.edititemtemplate(v=vs.90)
0
 
awilderbeastAuthor Commented:
well im trying to sort columns in a list view, but the errors im having now are that ive put my listview in an update panel

im getting
A control with ID 'ctl00$MainContent$lvUserItems$ctrl0$btnEdit' could not be found for the trigger in UpdatePanel 'upItems'. 

Open in new window


this below is on page load and the listview is inside the updatepanel
            foreach (ListViewDataItem i in lvUserItems.Items)
            {
                LinkButton btnEdit = (LinkButton)i.FindControl("btnEdit");               
                PostBackTrigger trEdit = new PostBackTrigger();
                trEdit.ControlID = btnEdit.UniqueID;
                upItems.Triggers.Add(trEdit);
                ScriptManager.GetCurrent(Page).RegisterPostBackControl(btnEdit);
            }

Open in new window

0
 
awilderbeastAuthor Commented:
Im such a tool!
Ive just found that listview does sort with command="Sort" haha!!
Let me guess its already in an update panel too?
0
 
awilderbeastAuthor Commented:
this code allowed me to access my controls
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now