How to load Controls from RadDock using WebUserControl?

3 posts, 1 answers
  1. Ian David
    Ian David avatar
    3 posts
    Member since:
    Feb 2013

    Posted 08 Feb 2013 Link to this post

    I am having a hard time trying to load my controls inside a RadDock that uses ascx.\

    This is my code (it is same as the Portal Demo)

    protected void ButtonAddDock_Click(object sender, EventArgs e)
            {
                RadDock dock = CreateRadDock();          
                RadDockZone dz = RadDockZone1;
      
                 
                RadDockLayout1.Controls.Add(dock);
                dock.Dock(dz);          
                CreateSaveStateTrigger(dock);
      
                dock.Tag = "Templates/NewQuiz.ascx";
                LoadWidget(dock);
      
            }
    private void LoadWidget(RadDock dock)
            {
                if (string.IsNullOrEmpty(dock.Tag) || dock.Closed)
                {
                    return;
                }
                Control widget = LoadControl(dock.Tag);
                widget.EnableViewState = false;
                dock.ContentContainer.Controls.Add(widget);
            }
    protected void btnSaveDraft_Click(object sender, EventArgs e)
            {
                string[] questions = {};
                int x = 0;
                foreach (RadDock dock in RadDockLayout1.RegisteredDocks)
                {              
                    RadTextBox txtQuestion = (RadTextBox)dock.ContentContainer.FindControl("txtQuestion");
                    questions[x]= txtQuestion.Text;
                    x++;
                }
    }

    And here is my ASCX

    <center style="padding-top: 15px;">
        <table style="text-align: justify;">
            <tr>
                <td style="width: 15%;">
                    Question:
                </td>
                <td>
                    <telerik:RadTextBox ID="txtQuestion" runat="server" Width="100%">
                    </telerik:RadTextBox>
                </td>
            </tr>
            <tr>
                <td style="width: 15%;">
                    Help Text:
                </td>
                <td>
                    <telerik:RadTextBox ID="txtHelp" runat="server" Width="100%">
                    </telerik:RadTextBox>
                </td>
            </tr>
            <tr>
                <td style="width: 15%;">
                    Mandatory:
                </td>
                <td>
                    <asp:CheckBox ID="chkMandatory" runat="server" />
                </td>
            </tr>
            <tr>
                <td style="width: 15%;">
                    Question Weight:
                </td>
                <td>
                    <telerik:RadTextBox ID="txtWeight" runat="server" Width="30px" Text="1">
                    </telerik:RadTextBox>
                </td>
            </tr>
            <tr>
                <td style="width: 15%;">
                    Question Type:
                </td>
                <td>
                    <telerik:RadComboBox ID="ddlType" runat="server" AutoPostBack="True">
                        <Items>
                            <telerik:RadComboBoxItem runat="server" Selected="True" Text="--Select Question Type--"
                                Value="--Select Question Type--" />
                            <telerik:RadComboBoxItem runat="server" Text="Multiple Choice - Radio Button" Value="Multiple Choice - Radio Button" />
                            <telerik:RadComboBoxItem runat="server" Text="Multiple Choice - Check Box" Value="Multiple Choice - Check Box" />
                        </Items>
                    </telerik:RadComboBox>
                </td>
            </tr>
        </table>
    </center>

    The code in my aspx can't find the txtQuestion textbox. 
  2. Answer
    Slav
    Admin
    Slav avatar
    1356 posts

    Posted 13 Feb 2013 Link to this post

    Hi Ian,

    The RadTextBox is in the user control that is loaded in the ContentContainer of the RadDock, so you should first get a reference to the user control in order to retrieve the text box. The following code sample will help you do this:
    RadTextBox txtQuestion = ((UserControl)dock.ContentContainer.Controls[0]).FindControl("txtQuestion") as RadTextBox;

    Greetings,
    Slav
    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.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Ian David
    Ian David avatar
    3 posts
    Member since:
    Feb 2013

    Posted 13 Feb 2013 Link to this post

    Hi Slav,

    It worked! Thank you for the reply. So, that is how I can call the controls inside a UserControl. At first I'm trying to do this :

    UserControl userControl = (UserControl)dock.FindControl(dock.Tag);
    RadTextBox txtQuestion = (RadTextbox)userControl.FindControl("txtQuestion");

    But it's not returning any value too. I thought I would never learn how to work on this. Thanks again!

Back to Top