+1-888-365-2779
Try Now
More in this section

Forums / General Discussions / Finding Control

Finding Control

2 posts, 0 answered
  1. Asif
    Asif avatar
    110 posts
    Registered:
    18 Nov 2010
    08 Dec 2010
    Link to this post
    Hi,
    I am using the code to find a control inside <telerik:messagecontrol id="message1"... But it seems I cant find the control. All I want to grab the value from fileuploader and put it into the label. I have also put my complete code page. Please help.

     protected void saveButton1_Click(object sender, EventArgs e)
        {

            FileUpload fp = (FileUpload)getFile(message1);
            Label vp = (Label)getVideo(message1);
            if (fp != null)
            {
                vp.Text = fp.FileName;
            }
        }

        private Control getFile(Control control)
        {
            FileUpload fl = (FileUpload)control.FindControl("fileupload");
            if (fl != null)
                return fl;
            else
            {
                
                foreach (Control ctrl in control.Controls)
                {
                    return getFile(ctrl);
                }
                return null;

            }
        }

        private Control getVideo(Control control)
        {
            Label vl = (Label)control.FindControl("VideoUpload");
            if (vl != null)
                return vl;
            else
            {
                
                foreach (Control ctrl in control.Controls)
                {
                    return getVideo(ctrl);
                }
                return null;

            }
        }

    ------------------- Code page ------------------

    <%@ Control Language="C#" %>
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
    <%@ Register TagPrefix="sf" Namespace="Telerik.Cms.Engine.WebControls" Assembly="Telerik.Cms.Engine" %>
    <%@ Register TagPrefix="sf" Namespace="Telerik.Cms.Engine.WebControls.Admin" Assembly="Telerik.Cms.Engine" %>

    <script runat="server">

        protected void saveButton1_Click(object sender, EventArgs e)
        {

            FileUpload fp = (FileUpload)getFile(message1);
            Label vp = (Label)getVideo(message1);
            if (fp != null)
            {
                vp.Text = fp.FileName;
            }
        }

        private Control getFile(Control control)
        {
            FileUpload fl = (FileUpload)control.FindControl("fileupload");
            if (fl != null)
                return fl;
            else
            {
                
                foreach (Control ctrl in control.Controls)
                {
                    return getFile(ctrl);
                }
                return null;

            }
        }

        private Control getVideo(Control control)
        {
            Label vl = (Label)control.FindControl("VideoUpload");
            if (vl != null)
                return vl;
            else
            {
                
                foreach (Control ctrl in control.Controls)
                {
                    return getVideo(ctrl);
                }
                return null;

            }
        }

    </script>

    <div class="ToolsAll">
        <div class="backWrapp">
            <asp:HyperLink ID="BackButton1" CssClass="actions back" runat="server">
                <asp:Literal ID="Literal1" Text="<%$Resources:BackToAllProducts %>" runat="server" />
            </asp:HyperLink></div>
    </div>
    <div id="divWorkArea" runat="server" class="workArea insert">
        <telerik:messagecontrol runat="server" ID="message1">
            <ItemTemplate>
                <asp:Label runat="server" ID="messageText"></asp:Label></ItemTemplate></telerik:messagecontrol>
        <div class="mainForm">
            <p class="mand">
                <asp:Literal ID="Literal2" runat="server" Text="<%$Resources:MandatoryFields %>"></asp:Literal></p>
            <h3>
                <asp:Label ID="Label1" AssociatedControlID="Name" runat="server">
                    <asp:Literal ID="Literal3" runat="server" Text="<%$Resources:Name %>"></asp:Literal>
                    * <em id="NameLabel" runat="server"></em></asp:Label></h3>
            <fieldset class="set">
                <div class="setIn title">
                    <asp:TextBox ID="Name" Text="<%$Resources:NameInput %>" runat="server"></asp:TextBox><asp:RequiredFieldValidator
                        ID="validator1" runat="server" ControlToValidate="Name" Display="Dynamic" EnableViewState="False"
                        CssClass="validMessage" SetFocusOnError="True">
                        <strong>
                            <asp:Literal ID="Literal4" runat="server" Text="<%$Resources:NameEmpty %>"></asp:Literal></strong></asp:RequiredFieldValidator></div>
            </fieldset>
            <div class="bottom">
                <div>
                    <!-- -->
                </div>
            </div>
            <h3>
                <asp:Literal ID="Literal5" runat="server" Text="<%$Resources:Content %>"></asp:Literal>*
                <em id="contentEditorLabel" runat="server"></em>
            </h3>
            <fieldset class="set">
                <div class="setIn">
                    <telerik:radeditor runat="server" id="Editor" contentareacssfile="~/Sitefinity/Admin/Themes/Default/AjaxControlsSkins/Sitefinity/EditorContentArea.css"
                        toolsfile="~/Sitefinity/Admin/ControlTemplates/EditorToolsFile.xml" skin="WebBlue"
                        newlinebr="False" dialogscssfile="~/Sitefinity/Admin/Themes/Default/AjaxControlsSkins/Sitefinity/EditorDialogs.css"
                        width="95%">
                        <ImageManager ViewPaths="~/Images" UploadPaths="~/Images" DeletePaths="~/Images" />
                        <MediaManager ViewPaths="~/Files" UploadPaths="~/Files" DeletePaths="~/Files" />
                        <FlashManager ViewPaths="~/Files" UploadPaths="~/Files" DeletePaths="~/Files" />
                        <DocumentManager ViewPaths="~/Files" UploadPaths="~/Files" DeletePaths="~/Files" />
                        <CssFiles>
                            <telerik:EditorCssFile Value="~/Sitefinity/Admin/Themes/Default/AjaxControlsSkins/Sitefinity/EditorCssFile.css" />
                        </CssFiles>

                    </telerik:radeditor>

                    <script type="text/javascript">

                        Telerik.Web.UI.Editor.CommandList["LibraryImageManager"] = function(commandName, editor, args) {
                            var editorArgs = editor.getSelectedElement();
                            if (!editorArgs.nodeName || typeof (editorArgs.nodeName) == "undefined" || editorArgs.nodeName != "A")
                                editorArgs = editor.getSelection();

                            var myCallbackFunction = function(sender, args) {
                                if (typeof (editorArgs.nodeName) != "undefined" && editorArgs.nodeName == "IMG")
                                    args.parentNode.replaceChild(editorArgs, args);
                                else {
                                    var cloned = args.cloneNode(true);
                                    var div = args.ownerDocument.createElement("DIV");
                                    div.appendChild(cloned);
                                    editorArgs.pasteHtml(div.innerHTML);
                                }
                            }
                            editor.showExternalDialog(
                                   '<%= Page.ResolveUrl("~/Sitefinity/UserControls/Dialogs/ImageEditorDialog.aspx") %>',
                                   editorArgs,
                                   750,
                                   600,
                                   myCallbackFunction,
                                   null,
                                   'ImageLibraryDialog',
                                   true,
                                   Telerik.Web.UI.WindowBehaviors.Close + Telerik.Web.UI.WindowBehaviors.Move,
                                   false,
                                   true)
                        };

                        Telerik.Web.UI.Editor.CommandList["LibraryDocumentManager"] = function(commandName, editor, args) {
                            var editorArgs = editor.getSelectedElement();
                            if (!editorArgs.nodeName || typeof (editorArgs.nodeName) == "undefined" || editorArgs.nodeName != "A")
                                editorArgs = editor.getSelection();

                            var myCallbackFunction = function(sender, args) {
                                if (typeof (editorArgs.nodeName) != "undefined" && editorArgs.nodeName == "A")
                                    args.parentNode.replaceChild(editorArgs, args);
                                else {
                                    var cloned = args.cloneNode(true);
                                    var div = args.ownerDocument.createElement("DIV");
                                    div.appendChild(cloned);
                                    editorArgs.pasteHtml(div.innerHTML);
                                }
                            }
                            editor.showExternalDialog(
                                   '<%= Page.ResolveUrl("~/Sitefinity/UserControls/Dialogs/DocumentEditorDialog.aspx") %>',
                                   editorArgs,
                                   750,
                                   600,
                                   myCallbackFunction,
                                   null,
                                   'ImageLibraryDialog',
                                   false,
                                   Telerik.Web.UI.WindowBehaviors.Close + Telerik.Web.UI.WindowBehaviors.Move,
                                   false,
                                   true)
                        };

                        Telerik.Web.UI.Editor.CommandList["LinkManager"] = function(commandName, editor, args) {
                            var editorArgs = editor.getSelectedElement();
                            if (!editorArgs.nodeName || typeof (editorArgs.nodeName) == "undefined" || editorArgs.nodeName != "A") {
                                var sel = editor.getSelection();
                                editorArgs = sel;
                                editorArgs.Html = sel.getHtml();
                                editorArgs.Text = sel.getText();
                            }


                            var myCallbackFunction = function(sender, args) {
                                if (typeof (editorArgs.nodeName) != "undefined" && editorArgs.nodeName == "A")
                                    args.parentNode.replaceChild(editorArgs, args);
                                else {
                                    var cloned = args.cloneNode(true);
                                    var div = args.ownerDocument.createElement("DIV");
                                    div.appendChild(cloned);
                                    editorArgs.pasteHtml(div.innerHTML);
                                }
                            }
                            editor.showExternalDialog(
                                   '<%= Page.ResolveUrl("~/Sitefinity/UserControls/Dialogs/LinksDialog.aspx") %>',
                                   editorArgs,
                                   750,
                                   600,
                                   myCallbackFunction,
                                   null,
                                   'ImageLibraryDialog',
                                   false,
                                   Telerik.Web.UI.WindowBehaviors.Close + Telerik.Web.UI.WindowBehaviors.Move,
                                   false,
                                   true)
                        };

                        Telerik.Web.UI.Editor.CommandList["SetLinkProperties"] = function(commandName, editor, args) {
                            var editorArgs = editor.getSelectedElement();
                            if (!editorArgs.nodeName || typeof (editorArgs.nodeName) == "undefined" || editorArgs.nodeName != "A")
                                editorArgs = editor.getSelection();

                            var myCallbackFunction = function(sender, args) {
                                if (typeof (editorArgs.nodeName) != "undefined" && editorArgs.nodeName == "A")
                                    args.parentNode.replaceChild(editorArgs, args);
                                else {
                                    var cloned = args.cloneNode(true);
                                    var div = args.ownerDocument.createElement("DIV");
                                    div.appendChild(cloned);
                                    editorArgs.pasteHtml(div.innerHTML);
                                }
                            }
                            editor.showExternalDialog(
                                   '<%= Page.ResolveUrl("~/Sitefinity/UserControls/Dialogs/LinksDialog.aspx") %>',
                                   editorArgs,
                                   750,
                                   600,
                                   myCallbackFunction,
                                   null,
                                   'ImageLibraryDialog',
                                   false,
                                   Telerik.Web.UI.WindowBehaviors.Close + Telerik.Web.UI.WindowBehaviors.Move,
                                   false,
                                   true)
                        };
                        var oldFunction = Telerik.Web.UI.Editor.CommandList["ToggleScreenMode"]; //save the original Paste function

                        Telerik.Web.UI.Editor.CommandList["ToggleScreenMode"] = function(commandName, editor, args) {
                            oldFunction(commandName, editor, args);
                            var bd = document.getElementsByTagName("body")[0];

                            if (/fullScreenMode/.test(bd.className)) {
                                var rep = bd.className.match(' ' + 'fullScreenMode') ? ' ' + 'fullScreenMode' : 'fullScreenMode';
                                bd.className = bd.className.replace(rep, '');

                            } else {
                                bd.className += bd.className ? ' ' + 'fullScreenMode' : 'fullScreenMode';
                            }
                        };

                        // automated tests helper function
                        function InsertTextArea() {
                            var editor = $find('<%=Editor.ClientID%>');
                            editor.set_html('<textarea id="myTableToFind" style="overflow:hidden; height: 300px; width: 500px;" border="none"></textarea>');
                        }

                    </script>

                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="Editor"
                        Display="Dynamic" EnableViewState="False" CssClass="validMessage" SetFocusOnError="True">
                        <strong>
                            <asp:Literal ID="Literal6" runat="server" Text="<%$Resources:ContentEmpty %>"></asp:Literal></strong></asp:RequiredFieldValidator></div>
            </fieldset>
            <div class="bottom">
                <div>
                    <!-- -->
                </div>
            </div>
            <h3>
                <asp:Literal ID="Literal7" runat="server" Text="<%$Resources:Category %>"></asp:Literal></h3>
            <sf:contentmetafields id="MetaFields" runat="server">
            <ItemTemplate>
                <fieldset class="set">
                    <ol class="setIn">
                            <li class="catSel clearfix">
                                <asp:Label ID="Label2" AssociatedControlID="Category" runat="server"> <asp:Literal ID="Literal8" runat="server" Text="<%$Resources:Category %>"></asp:Literal><em id="Em1" runat="server"></em></asp:Label><sf:ContentCategoriesField ID="Category" runat="server" />
                            </li>
                  </ol>
                </fieldset>
                 <div class="bottom"><div><!-- --></div></div>
                <h3>
                    <asp:Literal ID="Literal9" runat="server" Text="<%$Resources:Tags %>"></asp:Literal></h3><fieldset class="set">
                    <ol class="setIn">
                        <li class="tags">
                            <sf:ContentTagEditor ID="tagsControl" runat="server" />
                        </li>
                    </ol>
                </fieldset>
                 <div class="bottom"><div><!-- --></div></div>
                <h3>
                    <asp:Literal ID="Literal10" runat="server" Text="<%$Resources:SearchTerms %>"></asp:Literal></h3><fieldset class="set">
                    <div class="setIn">
                        <p class="example">
                            <asp:Literal ID="Literal11" runat="server" Text="<%$Resources:SearchTermsInfoNote %>"></asp:Literal></p><ol>
                            <li>
                                <asp:Label ID="Label3" AssociatedControlID="SearchTerms" runat="server" CssClass="faq-search-terms-label"><asp:literal ID="Literal12" runat="server" Text="<%$Resources:SearchTermsKeywords %>"></asp:literal><em id="PriceLabel" runat="server"></em></asp:Label><asp:TextBox ID="SearchTerms" runat="server" Text="<%$Resources:SearchTermsText %>" Columns="3" Rows="5" TextMode="MultiLine" Height="50" Width="500" CssClass="faq-search-terms-text"></asp:TextBox></li></ol></div></fieldset> <div class="bottom"><div><!-- --></div></div>
                <!-- Date -->
                <h3>
                    <asp:Literal ID="Literal13" runat="server" Text="Date"></asp:Literal></h3><fieldset class="set">
                    <div class="setIn">
                       
                <telerik:RadDateTimePicker id="RadDateTimePicker1" runat="server">
                                    </telerik:RadDateTimePicker>
                                    </div>
                </fieldset>
                                     <!-- /Date --><!-- Related Videos --><div class="bottom"><div><!-- --></div></div>
                <h3>
                    <asp:Literal ID="Literal3" runat="server" Text="Related Videos"></asp:Literal></h3><fieldset class="set">
                    <div class="setIn">
                        <p class="example">
                            <asp:Literal ID="Literal22" runat="server" Text="Links to Related Videos"></asp:Literal></p><asp:FileUpload ID="fileupload" runat="server"></asp:FileUpload><asp:Label ID="VideoUpload" runat="server" Text="" Visible="false"></asp:Label></div></fieldset> <div class="bottom"><div><!-- --></div></div>
        <!-- /Related Videos -->
          <!-- Related FAQs -->
                                   
                <h3>
                    <asp:Literal ID="Literal20" runat="server" Text="Related Forums"></asp:Literal></h3><fieldset class="set">
                    <div class="setIn">
                        <p class="example">
                            <asp:Literal ID="Literal21" runat="server" Text="Links to Related FAQs"></asp:Literal></p><asp:FileUpload ID="FAQsUpload" runat="server"></asp:FileUpload>
               
                    </div>
                </fieldset>
                 <div class="bottom"><div><!-- --></div></div>
        <!-- /Related FAQs -->




            </ItemTemplate>
        </sf:contentmetafields>
            <div class="bottom bot">
                <div>
                    <!-- -->
                </div>
            </div>
            <p class="button_area bot">
                <asp:LinkButton ID="saveButton1" runat="server" CssClass="CmsButLeft okdark" OnClick="saveButton1_Click">
                    <asp:Literal ID="Literal14" runat="server" Text="<%$Resources:CreateThisProduct %>"></asp:Literal>
                </asp:LinkButton><span id="separator1" runat="server"><asp:Literal ID="Literal15"
                    runat="server" Text="<%$Resources:Or %>"></asp:Literal></span><asp:LinkButton ID="cancelButton1"
                        runat="server" CssClass="cmscclcmd" CausesValidation="false">
                        <asp:Literal ID="Literal16" runat="server" Text="<%$Resources:Cancel %>"></asp:Literal>
                    </asp:LinkButton></p>
        </div>
        <div class="info" id="divFaq" runat="server">
            <div class="infoBottom">
                <h3>
                    <asp:Literal ID="Literal17" runat="server" Text="<%$Resources:ProductsFAQ %>"></asp:Literal></h3>
                <asp:Literal ID="Literal18" runat="server" Text="<%$Resources:ProductsFAQText %>"></asp:Literal><p
                    class="hideAllFAQs">
                    <a href="javascript:void(0)" onclick="javascript:Personalization.hideFaqSection('<%= divFaq.ClientID %>')">
                        Hide FAQs everywhere</a>
                </p>
            </div>
        </div>
        <div class="clear">
            <!-- -->
        </div>
    </div>
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    08 Dec 2010
    Link to this post
    Hi Asif,

    You could try using recursion to get the control inside the MessageControl

    private Control FindControlRecursive(Control container, string id)
     {
         Control ctrl = null;
         foreach (Control c in container.Controls)
         {
             ctrl = c;
             if (ctrl.ID == id)
                 break;
             ctrl = FindControlRecursive(ctrl, id);
         }
         return ctrl;
     }

    or you can get access to the container's controls collection

    this.message1.Controls[0].Controls[1]

    All the best,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
2 posts, 0 answered