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

Loaded user control is disapears in postback - why?

9 Answers 448 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Bader
Top achievements
Rank 1
Bader asked on 14 Nov 2011, 10:45 AM
Hi,

One of the pages loading usercontrols dynamically from a folder (each usercontrol has a different form and a different functionality), but when I click in any button in the usercontrol which causes postback, then the entire usercontrol disapears.
Does Telerik ajax controls handles this issue? if so, please let me know how.

Regards,
Bader

9 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 15 Nov 2011, 06:44 AM
Hello,

If you added dynamic control inside "if(!Page.IsPostback) " then remove this condition.Because dynamically added control not register with your mark up so after postback it was disappears. So you have to add this control every time.


Thanks,
Jayesh Goyani
0
Bader
Top achievements
Rank 1
answered on 17 Nov 2011, 11:02 AM
Hi,

Thank you very much for your reply,
I don't have any "if(!Page.IsPostback) ", within my code. By the way I tried to load the user controls to "MyPanel" control which is placed within "MainPanel" control that is ajaxified using:

<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="MainPanel">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="MainPanel" LoadingPanelID="MainRadAjaxLoadingPanel" />
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>

Also, I tried to remove the ajax component from the page, and the same problem occures.

Please, I need you help,

Regards,
Bader
0
Jayesh Goyani
Top achievements
Rank 2
answered on 17 Nov 2011, 11:16 AM
Hello,

Can you please provide your code/Project so i can check the problem in it?

Thanks,
Jayesh Goyani
0
Bader
Top achievements
Rank 1
answered on 17 Nov 2011, 12:56 PM
Hi,

Thank you very much for your reply,
Please download a sample project from www.f4rnet.com/WebSite1.rar 

Please, I need your help,

Regards,
Bader
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 17 Nov 2011, 01:56 PM
Hello,

try with below code and let me know if any concern.

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            Control control = null;
            string ctrlname =  Page.Request.Params.Get("__EVENTTARGET");
            control = Page.FindControl(ctrlname);
            if (control != null)
            {
                if (control.ID != "MainRadComboBox" && MainRadComboBox.SelectedIndex > 0)
                {
                    //click from page but not from 'MainRadComboBox'
                    UserControl reportUserControl = new UserControl();
                    reportUserControl = (UserControl)Page.LoadControl("UserControls/" + MainRadComboBox.SelectedValue + ".ascx");
                    MyPanel.Controls.Clear();
                    MyPanel.Controls.Add(reportUserControl);
                }
            }
            else
            {
                if (MainRadComboBox.SelectedIndex > 0)
                {
                    //click from usercontrol
                    UserControl reportUserControl = new UserControl();
                    reportUserControl = (UserControl)Page.LoadControl("UserControls/" + MainRadComboBox.SelectedValue + ".ascx");
                    MyPanel.Controls.Clear();
                    MyPanel.Controls.Add(reportUserControl);
                }
            }
 
        }
    }
 
    protected void MainRadComboBox_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        if (!e.Value.ToString().Equals(string.Empty))
        {
            UserControl reportUserControl = new UserControl();
            reportUserControl = (UserControl)Page.LoadControl("UserControls/" + e.Value.ToString() + ".ascx");
            MyPanel.Controls.Clear();
            MyPanel.Controls.Add(reportUserControl);
        }
    }
}


Thanks,
Jayesh Goyani
0
Bader
Top achievements
Rank 1
answered on 17 Nov 2011, 02:46 PM
Hi,

Thank you very much for your help,
It is working now.

Regards,
Bader
0
Bader
Top achievements
Rank 1
answered on 18 Nov 2011, 05:59 PM
Hi,

Thank you very much for your last reply, it is works for RadComboBox control, but I'm facing some problems with other controls. for example, I set a RadTreeView control istead of the RadComboBox then it wont works. I made some changes in the code you sent to me (Below).
Can you please explain to me about your code functionality, and explain to me what modifications is needed when I want to use another control.
protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dTable = Guides.GetGuide("SELECT * FROM [guidesQA] order by [Type],[Title]");
            GuideRadTreeView.DataSource = dTable;
            GuideRadTreeView.DataBind();
 
            if (IsPostBack)
            {
                Control control = null;
                string ctrlname = Page.Request.Params.Get("__EVENTTARGET");
                control = Page.FindControl(ctrlname);
 
                if (control != null)
                {
                    if (control.ID != "GuideRadTreeView" && Convert.ToInt64(GuideRadTreeView.SelectedNode.Value) > 0)
                    {
                        CodeHelper.LoadUserControl(Page, OperationsPanel, "UserControls/" + DataTableMng.GetValueByRowCellValue(dTable, "Type", "SerID", Convert.ToInt64(GuideRadTreeView.SelectedNode.Value)).Trim() + "OperationsUserControl.ascx");
                    }
                }
                else
                {
                    if (Convert.ToInt64(GuideRadTreeView.SelectedNode.Value) > 0)
                    {
                        CodeHelper.LoadUserControl(Page, OperationsPanel, "UserControls/" + DataTableMng.GetValueByRowCellValue(dTable, "Type", "SerID", Convert.ToInt64(GuideRadTreeView.SelectedNode.Value)).Trim() + "OperationsUserControl.ascx");
                    }
                }
            }
        }

But, I got an error (attached screen-shot).

By the way I tried to solve the problem using another way (using sessions, which contains the selected node value) as below:
protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dTable = Guides.GetGuide("SELECT * FROM [guidesQA] order by [Type],[Title]");
            GuideRadTreeView.DataSource = dTable;
            GuideRadTreeView.DataBind();
 
            if (IsPostBack)
            {
                Control control = null;
                string ctrlname = Page.Request.Params.Get("__EVENTTARGET");
                control = Page.FindControl(ctrlname);
 
                if (control != null)
                {
                    if (control.ID != "GuideRadTreeView" && Convert.ToInt64(GuideRadTreeView.SelectedNode.Value) > 0)
                    {
                        CodeHelper.LoadUserControl(Page, OperationsPanel, "UserControls/" + DataTableMng.GetValueByRowCellValue(dTable, "Type", "SerID", Convert.ToInt64(Session["YYYY"].ToString())).Trim() + "OperationsUserControl.ascx");
                    }
                }
                else
                {
                    CodeHelper.LoadUserControl(Page, OperationsPanel, "UserControls/" + DataTableMng.GetValueByRowCellValue(dTable, "Type", "SerID", Convert.ToInt64(Session["YYYY"].ToString())).Trim() + "OperationsUserControl.ascx");
                }
            }
        }
 
 
protected void QAGuideRadTreeView_NodeClick(object sender, RadTreeNodeEventArgs e)
        {
            string url = string.Empty;
            SelectedItemIDLabel.Text = e.Node.Value;
 
            Session["YYYY"] = e.Node.Value;
            CodeHelper.ChangePanelControlsVisibilty(InernalPanel, false);
 
            CodeHelper.LoadUserControl(Page, OperationsPanel, "~/UserControls/" + DataTableMng.GetValueByRowCellValue(Guides.GetGuide("SELECT * FROM [guidesQA] order by [Type],[Title]"), "Type", "SerID", Convert.ToInt64(e.Node.Value)).Trim() + "OperationsUserControl.ascx");
        }

My solution works, but I don't want to use sessions in my situation (I need to implement the solution exactly as it worked using the RadComboBox control).

Please, I need you help,

Regards,
Bader
0
Jayesh Goyani
Top achievements
Rank 2
answered on 19 Nov 2011, 05:34 AM
Hello Bader,

if (GuideRadTreeView.SelectedNode != null) //  Replace this condition
       {
          // do your logic here
       }


Thanks,
Jayesh Goyani
0
Bader
Top achievements
Rank 1
answered on 19 Nov 2011, 06:02 PM
Hello,

Thank you for your reply,
Your code is not working . Below is my current code:
protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dTable = Guides.GetGuide("SELECT * FROM [guidesQA] order by [Type],[Title]");
            GuideRadTreeView.DataSource = dTable;
            GuideRadTreeView.DataBind();
 
            if (IsPostBack)
            {
                Control control = null;
                string ctrlname = Page.Request.Params.Get("__EVENTTARGET");
                control = Page.FindControl(ctrlname);
 
                if (control != null)
                {
                    if (control.ID != "GuideRadTreeView" && GuideRadTreeView.SelectedNode != null)
                    {
                        CodeHelper.LoadUserControl(Page, OperationsPanel, "UserControls/" + DataTableMng.GetValueByRowCellValue(dTable, "Type", "SerID", Convert.ToInt64(GuideRadTreeView.SelectedNode.Value)).Trim() + "OperationsUserControl.ascx");
                    }
                }
                else
                {
                    if (GuideRadTreeView.SelectedNode != null)
                    {
                        CodeHelper.LoadUserControl(Page, OperationsPanel, "UserControls/" + DataTableMng.GetValueByRowCellValue(dTable, "Type", "SerID", Convert.ToInt64(GuideRadTreeView.SelectedNode.Value)).Trim() + "OperationsUserControl.ascx");
                    }
                }
            }
        }

Now, I dont gor any error message, but the loaded usercontrol disapears by clicking any button (or any cuaused postback).
Can you please help me solve this problem,

Regards,
Bader
Tags
Ajax
Asked by
Bader
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Bader
Top achievements
Rank 1
Share this question
or