Loaded user control is disapears in postback - why?

10 posts, 1 answers
  1. Bader
    Bader avatar
    215 posts
    Member since:
    Aug 2008

    Posted 14 Nov 2011 Link to this post

    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
  2. Jayesh Goyani
    Jayesh Goyani avatar
    2733 posts
    Member since:
    May 2010

    Posted 15 Nov 2011 Link to this post

    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
  3. Bader
    Bader avatar
    215 posts
    Member since:
    Aug 2008

    Posted 17 Nov 2011 Link to this post

    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
  4. Jayesh Goyani
    Jayesh Goyani avatar
    2733 posts
    Member since:
    May 2010

    Posted 17 Nov 2011 Link to this post

    Hello,

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

    Thanks,
    Jayesh Goyani
  5. Bader
    Bader avatar
    215 posts
    Member since:
    Aug 2008

    Posted 17 Nov 2011 Link to this post

    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
  6. Answer
    Jayesh Goyani
    Jayesh Goyani avatar
    2733 posts
    Member since:
    May 2010

    Posted 17 Nov 2011 Link to this post

    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
  7. Bader
    Bader avatar
    215 posts
    Member since:
    Aug 2008

    Posted 17 Nov 2011 Link to this post

    Hi,

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

    Regards,
    Bader
  8. Bader
    Bader avatar
    215 posts
    Member since:
    Aug 2008

    Posted 18 Nov 2011 Link to this post

    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
  9. Jayesh Goyani
    Jayesh Goyani avatar
    2733 posts
    Member since:
    May 2010

    Posted 18 Nov 2011 Link to this post

    Hello Bader,

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


    Thanks,
    Jayesh Goyani
  10. Bader
    Bader avatar
    215 posts
    Member since:
    Aug 2008

    Posted 19 Nov 2011 Link to this post

    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
Back to Top