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

Client ID changes on Postback

3 Answers 106 Views
Window
This is a migrated thread and some comments may be shown as answers.
Gotcha
Top achievements
Rank 1
Gotcha asked on 04 Jan 2012, 05:37 AM
Hi I have a RadWindow Generated at runtime based on certain condition , it also attaches itself to the RadWindowManager...
On first page load, the ID is set to "wndSetUp"... and on that page i have a button, when i click on the button to execute an onclick to display the radwindow, it postback , so on the next page load, i try to find the RadWindow using the FindControl("wndSetUp")  but coudlnt find it... it turned out the ID has changed to a defaut "ctl02" rather than "wndSetUp" ( which it was correct on first page load)...

In the Watch Window, the ID clearly changes when I click on the button.
Can anyone explain to me why the ID?

Code of Master Page which has the onClick Button
 
        protected void Page_Load(object sender, EventArgs e)
        {
            //Check if Calling Pages Require New User Set Up
            if (SessionContext.NeedUserSetUp)
            {
                // Find Reference to Window Set Up
                RadWindow wndSetUp = GetUserSetUpWindow();
                if (wndSetUp != null)
                {// Show Set Up Button and Add Ajax Settings
                    btNewUserSetUp.Visible = true;
                    RadAjaxManager_GM.AjaxSettings.AddAjaxSetting(btNewUserSetUp, wndSetUp);
                }
                else
                {
                    btNewUserSetUp.Visible = false;
                }
            }
            else
            {
                btNewUserSetUp.Visible = false;
            }
        }
 
      /// <summary>
        /// Handles the Click event of the btNewUserSetUp control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btNewUserSetUp_Click(object sender, EventArgs e)
        {
            RadWindow wndSetup = GetUserSetUpWindow();
            if (wndSetup != null)
            {
                wndSetup.Visible = true;
                wndSetup.VisibleOnPageLoad = true;
            }
        }
 
        /// <summary>
        /// Gets the user set up window. Find the Reference to the Window Set Up generated dynamically
        /// </summary>
        /// <returns></returns>
        private RadWindow GetUserSetUpWindow()
        {
            switch (sectionName)
            {
                case "SavingsCenter":
                case "ShoppingLists":
                case "BrowseItems":
                case "FindDeals":
                default:
                    return (RadWindow)Page.Form.FindControl("RadWindowManager_GM").FindControl("wndSetUp");
            }
        }
 
 
 
HTML
            <telerik:RadWindowManager ID="RadWindowManager_GM" runat="server">
            </telerik:RadWindowManager>
 
                <telerik:RadButton ID="btNewUserSetUp" runat="server" Visible="false" Width="98"
                    Height="21" OnClick="btNewUserSetUp_Click">
                    <Image ImageUrl="../Images/btNewUserSetUp.gif" HoveredImageUrl="../Images/btNewUserSetUp_up.gif" />
                </telerik:RadButton>

On My PageBASE class, inheritted by my pages, it checks a wnSetUp needs to be created using the 3 conditions: NeedUserSetUp, IsValidContext and !IsPostBack. My Work around is tp force a new creation of "wndSetUp" RadWindow (by removing the !IsPOstBack)... but in the end i had 2 RadWindows ( one with "ct02" and the other one since I recreated it on the postback with "wndSetUp")

protected override void OnLoad(EventArgs e)
 {
         _SessionContext.CallingPage = this;
     //Check for New User Set Up
     if (SessionContext.NeedUserSetUp && IsValidContext() && !IsPostBack)
     {
         RadWindow wndSetUp = CreateNewUserSetUpWindow(true);
     }
 
 
     base.OnLoad(e);
 }
 
 /// <summary>
 /// Determines whether [is valid context] only if PageBase called within the ApplicationLayer.
 /// </summary>
 /// <returns>
 ///   <c>true</c> if [is valid context]; otherwise, <c>false</c>.
 /// </returns>
 private bool IsValidContext()
 {
     MasterPage master = GetRootLevelMaster();
     return (master != null ? (master.GetType().Name.IndexOf("applicationlayer") > 0) : false);
 }
 
 
 /// <summary>
 /// Creates the new user set up window.
 /// </summary>
 /// <param name="manualSetUp">if set to <c>true</c> [manual set up].</param>
 /// <returns></returns>
 public RadWindow CreateNewUserSetUpWindow(bool manualSetUp)
 {
     RadWindow wndSetUp = new RadWindow();
     WindowSetUpConfigurations(wndSetUp, manualSetUp);
 
     RadWindowManager wndManager = (RadWindowManager)GetWindowManager();
     wndManager.Windows.Add(wndSetUp);
 
     //Attach RadWindow to the Form
     //this.Page.Form.Controls.Add(wndSetUp);
     return wndSetUp;
 }
 
 private Control GetWindowManager()
 {
     MasterPage master = GetRootLevelMaster();
     return master.FindControl("RadWindowManager_GM");
 }
 
 private MasterPage GetRootLevelMaster()
 {
     MasterPage master = Page.Master;
     MasterPage prevMaster = null;
 
     while (master != null)
     {
         prevMaster = master;
         master = master.Master;
     }
     return prevMaster;
 }
 
 
 /// <summary>
 /// Windows the set up configurations.
 /// Requirement: CSS Skin is loaded for RadWindow in Master
 /// </summary>
 /// <param name="wndSetUp">The WND set up.</param>
 private void WindowSetUpConfigurations(RadWindow wndSetUp,bool manualSetUp)
 {
     wndSetUp.ID = "wndSetUp";
     if (manualSetUp)
     {
         RadButton btSetUp = (RadButton)GetWindowManager().FindControl("btNewUserSetUp");
         wndSetUp.OpenerElementID = btSetUp.ClientID;
     }
     wndSetUp.Title = "First Time Setup";
     wndSetUp.Skin = "gmBlue";
     wndSetUp.NavigateUrl = "../Pages/InitialSetUp.aspx";
     wndSetUp.Height = 730;
     wndSetUp.Width = 900;
     wndSetUp.KeepInScreenBounds = true;
     wndSetUp.Modal = true;
     wndSetUp.Overlay = true;
     wndSetUp.ShowContentDuringLoad = true;
     wndSetUp.Visible = false;
     wndSetUp.VisibleStatusbar = false;
     wndSetUp.VisibleTitlebar = true;
     wndSetUp.Behaviors = WindowBehaviors.Close | WindowBehaviors.Move;
     wndSetUp.EnableEmbeddedSkins = false;
     wndSetUp.EnableShadow = false;
 }

3 Answers, 1 is accepted

Sort by
0
Accepted
Marin Bratanov
Telerik team
answered on 05 Jan 2012, 12:00 PM
Hello,

If you are creating a control that you need to persists across postbacks you need to do so in the PageInit event, as otherwise the viewstate is already loaded and you will add a new control with an ID assigned by the framework. More information on working with dynamic controls is available in the following article in the net: http://couldbedone.blogspot.com/2007/06/dynamically-created-controls-in-aspnet.html.

You also need to iterate through the Windows collection of the RadWindowManager and check the ID as this is the only way to find a specific RadWindow in the code-behind, e.g.:
protected override void OnInit(EventArgs e)
{
    RadWindow rw1 = new RadWindow();
    rw1.ID = "testID";
    rw1.NavigateUrl = "http://www.bing.com/";
    rwm1.Windows.Add(rw1);
 
    base.OnInit(e);
}

protected void Button1_Click(object sender, EventArgs e)
{
    foreach (RadWindow item in rwm1.Windows)
    {
        if (item.ID == "testID")
        {
            //item is now a reference to the RadWindow you need
            //work with it here
 
            //break the loop for performance
            break;
        }
    }
}


I also suggest that you examine this post on opening a RadWindow from the server: http://www.telerik.com/community/forums/aspnet-ajax/window/opening-radwindow-from-the-server.aspx.

Kind regards,
Marin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Gotcha
Top achievements
Rank 1
answered on 06 Jan 2012, 04:40 AM
Setting it EnableViewState in RWM to false did the trick...So this confirms the Window was loaded from ViewState... but I do not get where that ID CTL02 came from since I created the RadWindow onInit of the PageBase Class... and setting the ID there before the ViewState to "wndSetUp"

Anycase I'll mark it as answered... but if you have an explanation, i'd be glad to hear it...for my own comprehension.

Thanks

ps: Is there any reason why I would want ViewState on a RadWindowManager? In other words, am I breaking something by enabling it to false? From first thinking, I don't see why I would want to preserve the state of the RadWindowManager...
0
Marin Bratanov
Telerik team
answered on 06 Jan 2012, 01:00 PM
Hello,

I am glad to hear you have resolved the situation. On your question why you would want ViewState for the RadWindowManager - this is needed if you set some properties in the code-behind during runtime - if you do not have ViewState they will not be persisted, yet if you only use the static properties in the markup you shouldn't have any issues.


Greetings,
Marin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Window
Asked by
Gotcha
Top achievements
Rank 1
Answers by
Marin Bratanov
Telerik team
Gotcha
Top achievements
Rank 1
Share this question
or