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

LinkButton in RadListView inside UserControl is not firing click event.

8 Answers 100 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Damodar
Top achievements
Rank 1
Damodar asked on 07 Feb 2013, 01:51 AM
Hi,

I have page it contains 3 user controls.
Each user control contains RadListView in which there are list of link buttons.

Why is click event is not firing when user clicks link button?

Best Regards,
Damodar

8 Answers, 1 is accepted

Sort by
0
Damodar
Top achievements
Rank 1
answered on 07 Feb 2013, 04:02 AM

Click event fires only if I load the RadListView in user controls Init method?

Question:
I registered all three user controls in the page.
When ever click event is fired it will load all 3 user controls' Init method.
Even though link button is clicked in user control 1.
 
How to load only user control whose link button is clicked?
(As RadListView is loading data from data base)
OR Stop loading all user controls!

Is there better way to load user controls in aspx page depending on your choice?

Best Regards,
Damodar
0
Christophe
Top achievements
Rank 1
answered on 07 Feb 2013, 04:06 AM
Any chance you could post your code here ?

Cheers
0
Damodar
Top achievements
Rank 1
answered on 07 Feb 2013, 05:58 AM
<%@ Page Title="" Language="C#" MasterPageFile="~/Mobile.Master" AutoEventWireup="true" CodeBehind="Search.aspx.cs" Inherits="Mobile.Search" %><br> <br><%@ Register src="~/Mobile/Controls/Hardware.ascx" tagprefix="UCH" tagname="UC_Hardware" %><br>..<br><asp:Content ID="Content1" ContentPlaceHolderID="MobileContentPlaceHolder" runat="server">            <br>    <UCH:UC_Hardware ID="UCHardware" Visible="false" runat="server" /><br>  ..<br></asp:Content>

codebehind
public partial class Search : System.Web.UI.Page   
{               
protected void Page_Load(object sender, EventArgs e)       
{            if (!IsPostBack)           
LoadTargetUC(); 
        }
private void LoadTargetUC()<span class="Apple-tab-span" style="white-space:pre"> </span>{       
sstring target = string.Empty;      
 target = Request.QueryString["page"] as string;       
if (!string.IsNullOrEmpty(target))        {           
List<
string> QueryList = new List<string> { "Hardware", "Software", "Misc" };           
if (QueryList.Contains(target))            {               
switch (target)
                {                   
case ("Hardware"):
                        
UCHardware.Visible = true;                       
UCHardware.Load_Contorls();                       
break;.....

In User Control
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Hardware.ascx.cs" Inherits="Control_Hardware" %>
<
telerik:RadListView ID="RadLVHardware" runat="server" AllowPaging="True">       
<
ItemTemplate>           
<
ul>
<
li>                                       
<
asp:LinkButton OnCommand="lnkHardwareList_Command" ID="lnkShipList" runat="server" Text='<%# Eval(" CommandArgument='<%#((System.Data.DataRowView)Container.DataItem)["HardwareID"]%>'                        CommandName="Hardware" CausesValidation="false" />               
</
li>           
</
ul>       
</
ItemTemplate>    </telerik:RadListView>


Code Behind
protected void lnkHardwareList_Command(object sender, CommandEventArgs e){
string paramValue = Convert.ToString(e.CommandArgument)
override protected void OnInit(EventArgs e){Load_Controls();}
public void Load_Contorls()
{CommonProcess common = new CommonProcess();
DataSet ds = common.GetHardwareList() as DataSet;
if (ds != null){               
RadLVHardware.DataSource = ds;
RadLVHardware.DataBind();}}

protected void lnkHardwareList_Command(object sender, CommandEventArgs e)
{
string paramValue = Convert.ToString(e.CommandArgument)
...
}
0
Christophe
Top achievements
Rank 1
answered on 08 Feb 2013, 12:31 AM
God !! Some formatting never hurt anyone plz :) Ctrl-K + Ctrl-D is your friend.

I bet your code as shown here doesn't compile right ?
So far I can only strongly advise you to revise the way you are loading your usercontrols. Check this demo using the RadAjaxManager here and its online documentation there

You should be alright with that.
Let me know how you go.

Chris
0
Damodar
Top achievements
Rank 1
answered on 13 Feb 2013, 01:50 AM
Hi Chris,

Thanks for the tips for formatting :). 

I have master page while trying the tutorial, I got following error. 

Multiple controls with the same ID 'ControlHardware' were found. FindControl requires that controls have unique IDs.


When I checked public
 void LoadUserControl(string controlName) ran two times. If I put
if (LatestLoadedControlName != null)
{
LoadUserControl(LatestLoadedControlName);
}
    else
LoadUserControl("~/Control/Hardware.ascx");
Is there any way we can stop it? Best Regards, Damdoar
0
Accepted
Christophe
Top achievements
Rank 1
answered on 14 Feb 2013, 03:41 AM
May be you could try something like this (considering "PContent" is an asp:Panel):

private string LatestLoadedControlName
{
    get
    {
        return (string)ViewState["LatestLoadedControlName"];
    }
    set
    {
        ViewState["LatestLoadedControlName"] = value;
    }
}
 
protected void Page_Load(object sender, EventArgs e)
{
    if (LatestLoadedControlName != null)
    {
        LoadUserControl(LatestLoadedControlName);
    }
    else
    {
        LoadUserControl("~/Control/Hardware.ascx");
    }
}
 
public void LoadUserControl(string controlName)
{
    if (LatestLoadedControlName != null)
    {
        Control previousControl = PContent.FindControl(LatestLoadedControlName.Replace("~/Control/", "").Split('.')[0]);
        if (!Object.Equals(previousControl, null))
        {
            this.PContent.Controls.Remove(previousControl);
            previousControl.Dispose();
        }
    }
    string userControlID = controlName.Replace("~/Control/", "").Split('.')[0];
    Control targetControl = PContent.FindControl(userControlID);
    if (Object.Equals(targetControl, null))
    {
        UserControl userControl = (UserControl)this.LoadControl(controlName);
        userControl.ID = userControlID.Replace("/", "").Replace("~", "");
        this.PContent.Controls.Add(userControl);
        LatestLoadedControlName = controlName;
    }
}


Obviously your code can't work and loops indefinitely (shouldn't there be an exception). It runs only twice b/c it fails the second time.
Let say that on the first call your LatestLoadedControlName is equal to null, therefore it loops back to the same method and tries to load your "~/Control/Hardware.ascx".
This time your string controlName is equal to "~/Control/Hardware.ascx" but you never use it anyway ???? Hence you try to load your "~/Control/Hardware.ascx" again and again and again and ......

If your LatestLoadedControlName != null then you end up in an infinite loop.


Dude, my advise ? Read your code carefully.

0
Damodar
Top achievements
Rank 1
answered on 25 Feb 2013, 04:30 AM
Hi Chris,

Thanks! 
Thought telerik code works out of box.
Didn't realize telerik example code need to be modified.


Best Regards,
Damodar
0
Damodar
Top achievements
Rank 1
answered on 01 Mar 2013, 05:47 AM
Hi Chris,

Thanks for your help.

Page load is hitting two times, When every list button is clicked.
Can we stop this? I am not sure why is this happening.

Best Regards,
Damodar
Tags
ListView
Asked by
Damodar
Top achievements
Rank 1
Answers by
Damodar
Top achievements
Rank 1
Christophe
Top achievements
Rank 1
Share this question
or