locked
ASP dropdownlist selectedindexchange fires up only the first time RRS feed

  • Question

  • User727658907 posted

    Hi! I have an asp dropdownlist control which selectedindexchange event works only the first time I do it, I've found a couple more post about this but the solutions did not fit my scenario. Could you please have a look and let me know what looks wrong? The failing dropdown is ContentDD... I've set a break point on the event but it only stops there the first time... thanks in advance for your help!

    ASPX

    <%@ Page Title="" Language="C#" MasterPageFile="~/Master/Admin.master" AutoEventWireup="true" CodeFile="CreateArticles.aspx.cs" Inherits="App_Admin_CreateArticles" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
        <style>
            .error{
                color: red;
                font-weight: bold;
            }
        </style>
    </asp:Content>
    
    <asp:Content ID="Content3" ContentPlaceHolderID="Scripts" Runat="Server">
    
    </asp:Content>
    
    <asp:Content ID="Content2" ContentPlaceHolderID="Body" Runat="Server">
        <telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
    
        <div data-ng-app="ArticleCreation" id="page-content" class="page-content">
    
                <div class="row page-zone page-zone-a" style="padding: 0 20px;">
                    <div class="form-group">
                        <label for="ContentTypes">Content type</label>
                        <asp:DropDownList ID="ContentTypes" runat="server" AppendDataBoundItems="true" CssClass="form-control" OnSelectedIndexChanged="Type_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
                    </div>
                </div>
                <div class="row page-zone page-zone-a" style="padding: 0 0 0 5px;" runat="server" id="div_elements" visible="false">
          
                    <div class="col" style="width: 50%">
                        <div class="custom-checkbox">
                            <label class="inline radio-toggle"><input type="radio" id="rbNew" name="Source" runat="server" value="New" checked="" onclick="rbSrc_click(this)"/> New</label>
                            <label class="inline radio-toggle" style="padding-left:10px"><input type="radio" id="rbExisting" name="Source" runat="server" value="Existing" onclick="rbSrc_click(this)"/> Existing</label>
                        </div>
                        <asp:DropDownList Width="25%" ID="ContentDD" runat="server" AppendDataBoundItems="true" CssClass="form-control" OnSelectedIndexChanged="Content_SelectedIndexChanged" AutoPostBack="true" style="display:none"></asp:DropDownList>
                        <div class="form-group">
                            <label for="ContentTitle">Title</label>
                            <input type="text" class="form-control" id="ContentTitle" placeholder="Write the article's name" runat="server" autocomplete="off"/>
                        </div>
                        <div class="form-group">
                            <label for="ArticleSummary">Summary</label>
                            <input type="text" class="form-control" id="ArticleSummary" placeholder="Write the article's summary" runat="server" autocomplete="off"/>
                        </div>
                        <div class="form-group" runat="server" visible="false" id="div_rb">
                            <label class="inline radio-toggle"><input type="radio" id="rbExternal" name="Target" runat="server" checked="" value="1" onclick="rb_click(this)"/> External link to content</label>
                            <label class="inline radio-toggle" style="padding-left:10px"><input type="radio" id="rbInternal" name="Target" value="3" runat="server" onclick="rb_click(this)"/> Internal Web Page</label>
                            <label class="inline radio-toggle" style="padding-left:10px"><input type="radio" id="rbLocal" name="Target" value="2" runat="server" onclick="rb_click(this)"/> Create content</label>
                            <label class="inline radio-toggle" style="padding-left:10px"><input type="radio" id="rbEmail" name="Target" value="4" runat="server" onclick="rb_click(this)"/> Email</label>
                            <label class="inline radio-toggle" style="padding-left:10px"><input type="radio" id="rbFile" name="Target" value="5" runat="server" onclick="rb_click(this)"/> File</label>
                        </div>
                        <div class="form-group" style="display:none" id="div_external" runat="server">
                            <label for="ArticleLink">Article Link</label>
                            <input type="text" class="form-control" id="ArticleLink" placeholder="www.google.com" runat="server" autocomplete="off" />
                        </div>
                        <div class="form-group" style="display:none" id="div_internal" runat="server">
                            <label for="ArticleLink">Select Web Page</label>
                            <asp:DropDownList ID="InternalPages" runat="server" AppendDataBoundItems="false" CssClass="form-control" AutoPostBack="false" ></asp:DropDownList>
                        </div>
                        <div class="form-group" style="display:none" id="div_file" runat="server">
                            <label for="ArticleLink">Select File</label>
                            <input type="file" id="LMFile" class="filestyle" data-text="Browse"  runat="server" data-size="sm"/>
                        </div>
                        <div  style="min-height: 300px; padding: 0 20px;display:none" id="div_editor" runat="server">
                        <h4>Content</h4>
                            <div data-ng-controller="CreationCtrl">
                                <telerik:RadEditor ID="ContentEditor" runat="server" Height="600px" Width="100%" Skin="MetroTouch" >
                                    <ImageManager ViewPaths="~/Content/img/Content/Articles" UploadPaths="~/Content/img/Content/Articles" DeletePaths="~/Content/img/Content/Articles" EnableAsyncUpload="true" />
                                </telerik:RadEditor>
                            </div>
                            <br />
                        </div>
                        <div class="form-group" id="div_Img" runat="server" visible="false">
                            <label runat="server" for="ArticleImage" id="InputText">Image</label><small runat="server" id="Small1" class="form-text text-muted" style="display:inline; padding-left:5px">Videos must be pre-uploaded to videos folder using file manager in admin tools.</small>
                            <br />
                            <%--<input type="file" class="form-control-file" id="ArticleImage" aria-describedby="fileHelp" runat="server" onchange="refreshimage(this)"/>--%>
                            <asp:FileUpload runat="server" ID="ArticleImage" onchange="refreshimage(this);"/>
                            <asp:DropDownList ID="FileDDl" runat="server" AppendDataBoundItems="true" CssClass="form-control" Visible="false"></asp:DropDownList>
                            <small runat="server" id="fileHelp" class="form-text text-muted">Select the image for the article.</small>
                            <asp:Image ID="ImagePreview" runat="server" CssClass="img-thumbnail" Width="100" Height="75" style=";display:none"/>
                        </div>
    <%--                    <div class="form-group">
                            <label for="ctl00_Body_ArticleDate_dateInput">Article Date</label><br />
                            <telerik:RadDatePicker ID="ArticleDate" runat="server" />
                        </div>--%>
    
                        <div class="form-group" >
                            <input type="hidden" id="PageIdValue" value="" runat="server" />
                            <asp:Button ID="DeleteContent" runat="server" Text="Delete" CssClass="btn btn-danger" OnClick="DeleteContent_Click" style="display:none;float:right"/>
                            <asp:Button ID="ModifyContent" runat="server" Text="Save" CssClass="btn btn-primary" OnClick="ModifyContent_Click"  Style="float:right;margin-right:5px;" /><br /> <%--OnClientClick="return ProgressBar()"--%>
                            <asp:Label ID="Message" runat="server" CssClass="error"></asp:Label>
                        </div>
                         
    
                    </div>
                </div>
        </div>
    
    
                  
        <script>
    
            function rb_click(event) {
                var ext = document.getElementById("Body_div_external");
                var edt = document.getElementById("Body_div_editor");
                var int = document.getElementById("Body_div_internal");
                var fl = document.getElementById("Body_div_file"); 
                var txtbx = document.getElementById("Body_ArticleLink");
                if (event.value == 1) {
                    ext.style.display = "block";
                    edt.style.display = "none";
                    int.style.display = "none";
                    fl.style.display = "none";
                    txtbx.placeholder = 'www.google.com';
                }
                else if (event.value == 2) {
                    ext.style.display = "none";
                    edt.style.display = "block";
                    int.style.display = "none";
                    fl.style.display = "none";
                }
                else if (event.value == 3) {
                    ext.style.display = "none";
                    edt.style.display = "none";
                    int.style.display = "block";
                    fl.style.display = "none";
                }
                else if (event.value == 4) {
                    ext.style.display = "block";
                    edt.style.display = "none";
                    int.style.display = "none";
                    fl.style.display = "none";
                    txtbx.placeholder = 'john.doe@hp.com';
                }
                else if (event.value == 5) {
                    ext.style.display = "none";
                    edt.style.display = "none";
                    int.style.display = "none";
                    fl.style.display = "block";
                }
            };
            function rbSrc_click(event) {
                var dd = document.getElementById("Body_ContentDD");
                var dc = document.getElementById("Body_DeleteContent");
                var img = document.getElementById("Body_ImagePreview");
                if (event.value == "New") {
                    dd.style.display = "none";
                    dc.style.display = "none";
                    img.style.display = "none";
                }
                else {
                    dd.style.display = "block";
                    dc.style.display = "block";
                    img.style.display = "block";
                }
            };
    
            function refreshimage(event) {
                if (event.files && event.files[0] && $('#Body_ContentTypes').val()!='3') {
                    var reader = new FileReader();
                    reader.onload = function (e) {
                        //$('#imgpreview').css('visibility', 'visible');
                        $('#Body_ImagePreview').attr('src', e.target.result);
                    }
                    reader.readAsDataURL(event.files[0]);
                }
            };
    
        </script>
    </asp:Content>
    
    
    

    CS code behind

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.IO;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Telerik.Web.UI;
    
    public partial class App_Admin_CreateArticles : System.Web.UI.Page
    {
        private static string[] files;
        private static int PageId { get; set; }
    
        private static DataTable Data { get; set; }
    
        private static DataTable Data2 { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
    
            if (!Page.IsPostBack)
            {
                Init();
            }
            else
            {
                if (rbExisting.Checked)
                {
                    ContentDD.Style["display"] = "block";
                    DeleteContent.Style["display"] = "block";
                    ImagePreview.Style["display"] = "block";
                }
                else
                {
                    ContentDD.Style["display"] = "none";
                    DeleteContent.Style["display"] = "none";
                    ImagePreview.Style["display"] = "none";
                }
            }
        }
    
        private void Init()
        {
            ContentEditor.ToolbarMode = EditorToolbarMode.Default;
            ContentEditor.EnsureToolsFileLoaded();
            RemoveButton("AboutDialog");
            RemoveButton("ToggleScreenMode");
            RemoveButton("ModuleManager");
            RemoveButton("Zoom");
            RemoveButton("InsertCustomLink");
            RemoveButton("ImageMapDialog");
            RemoveButton("InsertSnippet");
            RemoveButton("InsertFormElement");
            RemoveButton("FormatStripper");
            RemoveButton("XhtmlValidator");
            RemoveButton("FormatCodeBlock");
            RemoveButton("InsertGroupbox");
            RemoveButton("InsertParagraph");
            RemoveButton("TemplateManager");
            RemoveButton("FlashManager");
            RemoveButton("DocumentManager");
            RemoveButton("ApplyClass");
    
            CreateArticle ca = new CreateArticle();
            DataTable dt = ca.GetSiteList();
    
            ContentTypes.DataSource = dt;
            ContentTypes.DataTextField = "ContentType";
            ContentTypes.DataValueField = "Id";
            ContentTypes.DataBind();
    
            GetFiles();
    
            if (files.Length > 0)
            {
                DataTable dt1 = new DataTable();
                dt1.Columns.Add("path", typeof(string));
                dt1.Columns.Add("file", typeof(string));
    
                foreach (string f in files)
                    dt1.Rows.Add(f, Path.GetFileName(f));
    
                FileDDl.DataSource = dt1;
                FileDDl.DataTextField = "file";
                FileDDl.DataValueField = "path";
                FileDDl.DataBind();
            }
            LoadSitesDropDown();
        }
    
        private void LoadSitesDropDown(bool SelectLast = false)
        {
            CreateContentData c = new CreateContentData();
            Data2 = c.GetSiteList();
    
            InternalPages.DataSource = Data2;
            InternalPages.DataTextField = "Title";
            InternalPages.DataValueField = "URL";
            InternalPages.DataBind();
            InternalPages.SelectedIndex = 0;
    
        }
        private void GetFiles()
        {
            string path = "Content\\img\\Content\\Videos";
            string fullPath = Path.Combine(Server.MapPath(@"\"), path);
            if (Directory.Exists(fullPath))
            {
                files = Directory.GetFiles(fullPath);
            }
            //FileDDl
        }
        private void RemoveButton(string toolName)
        {
            foreach (EditorToolGroup g in ContentEditor.Tools)
            {
                EditorTool tool = g.FindTool(toolName);
                if (tool != null)
                    g.Tools.Remove(tool);
            }
        }
        protected void DeleteContent_Click(object sender, EventArgs e)
        {
            int selectedValue = int.Parse(ContentDD.SelectedValue.ToString());
            string tabletodelete = int.Parse(ContentTypes.SelectedValue) < 3 ? "Articles":"Videos";
            if (selectedValue > 0)
            {
    
                DataAccess dac = new DataAccess();
                SqlParameter id = new SqlParameter("@id", selectedValue);
                try
                {
                    dac.ExecuteNonQuery("DELETE FROM " + tabletodelete + " WHERE ID=@ID", new SqlParameter[] { id });
                }
                catch (SqlException) { throw; }
                if (File.Exists(Server.MapPath("~/Content/img/Content/Videos/") + Path.GetFileName(PageIdValue.Value)))
                {
                    File.Delete(Server.MapPath("~/Content/img/Content/Videos/") + Path.GetFileName(PageIdValue.Value));
                    File.Delete(Server.MapPath("~/Content/img/Content/Videos/Thumbnails/") + Path.GetFileName(ImagePreview.ImageUrl));
                }
                IfSuccess();
                ContentDD.SelectedIndex = 0;
                ContentTypes.SelectedIndex = 0;
                div_elements.Visible = false;
            }
            else
            {
                Message.Text = "Please select an item to be deleted";
            }
        }
        protected void ModifyContent_Click(object sender, EventArgs e)
        {
            string id = ContentDD.SelectedValue;
            string title = ContentTitle.Value;
            string body = ContentEditor.Content;
            string imagePath = string.Empty;
            string filePath = string.Empty;
            string thumbnailPath = string.Empty;
            DateTime? articleDate = DateTime.Today;
            string summary = ArticleSummary.Value;
            string type = ContentTypes.SelectedValue;
            string IntPage = InternalPages.SelectedValue;
            short external = (short)1;
            string URL = string.Empty;
    
    
    
            if (string.IsNullOrEmpty(title) || (rbLocal.Checked && string.IsNullOrEmpty(body) && ContentTypes.SelectedIndex < 3) || ((rbExternal.Checked || rbEmail.Checked) && string.IsNullOrEmpty(ArticleLink.Value) && ContentTypes.SelectedIndex < 3) || string.IsNullOrEmpty(summary) || ContentTypes.SelectedIndex==0 || (rbInternal.Checked && InternalPages.SelectedIndex==0 && ContentTypes.SelectedIndex < 3) || (rbFile.Checked && LMFile.PostedFile != null && LMFile.PostedFile.ContentLength == 0 && ContentTypes.SelectedIndex < 3))
            {
                Message.Text = "One or more mandatory fields are empty";
                rbExternal.Checked = true;
                return;
            }
    
            thumbnailPath = ImagePreview.ImageUrl.Replace("../", "");
            imagePath = (ContentTypes.SelectedIndex < 3) ? ImagePreview.ImageUrl.Replace("../", "").Replace("Thumbnail\\","") : PageIdValue.Value;
            if (ArticleImage.PostedFile != null && ArticleImage.PostedFile.ContentLength > 0)
            {
                HttpPostedFile postedFile = ArticleImage.PostedFile;
                string fileExtension = Path.GetExtension(postedFile.FileName);
                if (!ValidExtension(fileExtension)) return;
                Operations op = new Operations();
                if (ContentTypes.SelectedIndex < 3)
                {
                    imagePath = op.SaveFileToServer(postedFile, Server.MapPath(@"\"), "Content\\img\\Content\\Articles\\");
                    thumbnailPath = op.CreateThumbnailImage(imagePath, Server.MapPath(@"\"), "Content\\img\\Content\\Articles\\Thumbnail\\", 100, 75);
                }
            }
            if (LMFile.PostedFile != null && LMFile.PostedFile.ContentLength > 0)
            {
                HttpPostedFile postedFile = LMFile.PostedFile;
                string fileExtension = Path.GetExtension(postedFile.FileName);
                if (!ValidExtension(fileExtension)) return;
                Operations op = new Operations();
                if (ContentTypes.SelectedIndex < 3)
                {
                    filePath = op.SaveFileToServer(postedFile, Server.MapPath(@"\"), "Content\\docs\\");
                }
            }
            if (ContentTypes.SelectedIndex==3)
            {
                imagePath = FileDDl.SelectedValue.Replace(Server.MapPath(@"\"), string.Empty);
                thumbnailPath = "Content\\img\\Content\\Videos\\ThumbNails\\" + Path.GetFileNameWithoutExtension(imagePath) + ".jpg";
                var ffMpeg = new NReco.VideoConverter.FFMpegConverter();
                ffMpeg.GetVideoThumbnail(imagePath, thumbnailPath, 5);
            }
    
            if (rbInternal.Checked)
            {
                URL = InternalPages.SelectedValue;
                external = 0;
            }
            else if (rbLocal.Checked)
            {
                URL = InternalPages.SelectedValue;
                external = 0;
            }
            else if(rbExternal.Checked)
            {
                URL = "//" + ArticleLink.Value;
                external = 1;
            }
            else if (rbEmail.Checked)
            {
                URL = "mailto:" + ArticleLink.Value;
                external = 1;
            }
            else if (rbFile.Checked)
            {
                URL = filePath;
                external = 1;
            }
    
            Dictionary<string, object> data = new Dictionary<string, object>();
            data.Add("id", id);
            data.Add("title", title);
            data.Add("body", body);
            data.Add("imagePath", imagePath);
            data.Add("thumbnailPath", thumbnailPath);
            data.Add("date", articleDate);
            data.Add("summary", summary);
            data.Add("type", type);
            data.Add("external", external);
            data.Add("Url", URL);
    
            CreateArticle ca = new CreateArticle();
            if (ContentTypes.SelectedIndex < 3) { ca.Create(data); } else { ca.CreateVideoEntry(data); }
            IfSuccess();
            ContentTypes.SelectedIndex = 0;
            ContentDD.SelectedIndex = 0;
            div_elements.Visible = false;
        }
    
        private bool ValidExtension(string extension)
        {
            string[] extensions =  ContentTypes.SelectedIndex < 3 ? new string[] { ".jpg", ".jpeg", ".bmp", ".png", ".doc", ".docx", ".pdf", ".ppt", ".pptx",".xls",".xlsx",".xlsm",".xlsb",".zip" } : new string[] { ".mp4", ".m4v", ".avi", ".mpeg" };
            return extensions.Contains(extension.ToLower());
        }
    
        private void IfSuccess()
        {
            PageIdValue.Value = string.Empty;
            ContentTitle.Value = string.Empty;
            ContentEditor.Content = string.Empty;
            //ArticleDate.SelectedDate = null;
            Message.Text = string.Empty;
            ArticleSummary.Value = string.Empty;
            ImagePreview.ImageUrl = string.Empty;
            ArticleLink.Value = string.Empty;
    
        }
        //Content_SelectedIndexChanged
        protected void Content_SelectedIndexChanged(object sender, EventArgs e)
        {
            string URLTemp = string.Empty;
            DropDownList content = sender as DropDownList;
            if (content == null) return;
            int selectedValue = int.Parse(content.SelectedValue.ToString());
            PageId = selectedValue;
            DataRow dr = (from d in Data.AsEnumerable() where d.Field<int>("ID") == selectedValue select d).First();
            ContentTitle.Value = dr.Field<string>("Title");
            if (ContentTypes.SelectedIndex == 3) { FileDDl.SelectedValue = Path.Combine(Server.MapPath(@"\"), dr.Field<string>("Path")); }
            ArticleSummary.Value = dr.Field<string>("Summary");
            if (int.Parse(ContentTypes.SelectedValue) == 3)
            {
                PageIdValue.Value = dr.Field<string>("Path");
            }
            else
            {
                div_external.Attributes.Add("style", "display:none");
                div_internal.Attributes.Add("style", "display:none");
                div_editor.Attributes.Add("style", "display:none");
    
                
                if (! string.IsNullOrEmpty(dr.Field<string>("Body")))
                {
                    ContentEditor.Content = dr.Field<string>("Body");
                    rbLocal.Checked = true;
                    div_editor.Attributes.Add("style", "display:block");
                }
                else
                {
                    URLTemp = dr.Field<string>("ExternalUrl");
                    //ArticleLink.Value = dr.Field<string>("ExternalUrl").Replace("emailto:","");
                    if (URLTemp.Substring(0,2).ToString() == "//")
                    {
                        ArticleLink.Value = URLTemp.Substring(2);
                        rbExternal.Checked = true;
                        div_external.Attributes.Add("style", "display:block");
                    }
                    else if (URLTemp.Contains("mailto:"))
                    {
                        ArticleLink.Value = URLTemp.Replace("mailto:","");
                        rbEmail.Checked = true;
                        div_external.Attributes.Add("style", "display:block");
                    }
                    //WebPagePlaceHolder.aspx?PageID=12                                                                   
                    else if (URLTemp.Contains("WebPagePlaceHolder"))
                    {
                        rbInternal.Checked = true;
                        div_internal.Attributes.Add("style", "display:block");
                        InternalPages.SelectedValue = URLTemp.TrimEnd();
                    }
                }
            }
            ImagePreview.ImageUrl = "../"+dr.Field<string>("ImagePath");
            
    
    
    
    }
    
        protected void Type_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList contentTypes = sender as DropDownList;
            if (contentTypes == null) return;
    
            int selectedValue = int.Parse(contentTypes.SelectedValue.ToString());
            if (selectedValue > 0)
            {
                CreateContentData ca = new CreateContentData();
                div_elements.Visible = true;
                switch (selectedValue)
                {
                    case 1:
                    case 2:
                        div_rb.Visible = true;
                        div_external.Style["display"] = "block";
                        div_Img.Visible = true;
                        InputText.InnerText = "Image";
                        ArticleImage.Visible = true;
                        fileHelp.InnerText = "Select the image for the article.";
                        Data = ca.GetArticles(selectedValue);
                        FileDDl.Visible = false;
                        Small1.Visible = false;
                        break;
                    case 3:
                        div_rb.Visible = false;
                        div_external.Style["display"] = "none";
                        div_Img.Visible = true;
                        ArticleImage.Visible = false;
                        InputText.InnerText = "Video";
                        Small1.Visible = true;
                        FileDDl.Visible = true;
                        fileHelp.InnerText = "Select the video for the article.";
                        Data = ca.GetVideosList();
                        break;
                }
                ContentDD.DataSource = Data;
                ContentDD.DataTextField = "Title";
                ContentDD.DataValueField = "Id";
                ContentDD.DataBind();
                IfSuccess();
                ContentDD.SelectedIndex = 0;
            }
            else
            {
                div_elements.Visible = false;
            }
        }
    
    }

    Wednesday, November 28, 2018 10:48 PM

All replies

  • User475983607 posted

    Hi! I have an asp dropdownlist control which selectedindexchange event works only the first time I do it, I've found a couple more post about this but the solutions did not fit my scenario. Could you please have a look and let me know what looks wrong? The failing dropdown is DDContent... I've set a break point on the event but it only stops there the first time... thanks in advance for your help!

    ASPX

    <%@ Page Title="" Language="C#" MasterPageFile="~/Master/Admin.master" AutoEventWireup="true" CodeFile="CreateArticles.aspx.cs" Inherits="App_Admin_CreateArticles" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
        <style>
            .error{
                color: red;
                font-weight: bold;
            }
        </style>
    </asp:Content>
    
    <asp:Content ID="Content3" ContentPlaceHolderID="Scripts" Runat="Server">
    
    </asp:Content>
    
    <asp:Content ID="Content2" ContentPlaceHolderID="Body" Runat="Server">
        <telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
    
        <div data-ng-app="ArticleCreation" id="page-content" class="page-content">
    
                <div class="row page-zone page-zone-a" style="padding: 0 20px;">
                    <div class="form-group">
                        <label for="ContentTypes">Content type</label>
                        <asp:DropDownList ID="ContentTypes" runat="server" AppendDataBoundItems="true" CssClass="form-control" OnSelectedIndexChanged="Type_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
                    </div>
                </div>
                <div class="row page-zone page-zone-a" style="padding: 0 0 0 5px;" runat="server" id="div_elements" visible="false">
          
                    <div class="col" style="width: 50%">
                        <div class="custom-checkbox">
                            <label class="inline radio-toggle"><input type="radio" id="rbNew" name="Source" runat="server" value="New" checked="" onclick="rbSrc_click(this)"/> New</label>
                            <label class="inline radio-toggle" style="padding-left:10px"><input type="radio" id="rbExisting" name="Source" runat="server" value="Existing" onclick="rbSrc_click(this)"/> Existing</label>
                        </div>
                        <asp:DropDownList Width="25%" ID="ContentDD" runat="server" AppendDataBoundItems="true" CssClass="form-control" OnSelectedIndexChanged="Content_SelectedIndexChanged" AutoPostBack="true" style="display:none"></asp:DropDownList>
                        <div class="form-group">
                            <label for="ContentTitle">Title</label>
                            <input type="text" class="form-control" id="ContentTitle" placeholder="Write the article's name" runat="server" autocomplete="off"/>
                        </div>
                        <div class="form-group">
                            <label for="ArticleSummary">Summary</label>
                            <input type="text" class="form-control" id="ArticleSummary" placeholder="Write the article's summary" runat="server" autocomplete="off"/>
                        </div>
                        <div class="form-group" runat="server" visible="false" id="div_rb">
                            <label class="inline radio-toggle"><input type="radio" id="rbExternal" name="Target" runat="server" checked="" value="1" onclick="rb_click(this)"/> External link to content</label>
                            <label class="inline radio-toggle" style="padding-left:10px"><input type="radio" id="rbInternal" name="Target" value="3" runat="server" onclick="rb_click(this)"/> Internal Web Page</label>
                            <label class="inline radio-toggle" style="padding-left:10px"><input type="radio" id="rbLocal" name="Target" value="2" runat="server" onclick="rb_click(this)"/> Create content</label>
                            <label class="inline radio-toggle" style="padding-left:10px"><input type="radio" id="rbEmail" name="Target" value="4" runat="server" onclick="rb_click(this)"/> Email</label>
                            <label class="inline radio-toggle" style="padding-left:10px"><input type="radio" id="rbFile" name="Target" value="5" runat="server" onclick="rb_click(this)"/> File</label>
                        </div>
                        <div class="form-group" style="display:none" id="div_external" runat="server">
                            <label for="ArticleLink">Article Link</label>
                            <input type="text" class="form-control" id="ArticleLink" placeholder="www.google.com" runat="server" autocomplete="off" />
                        </div>
                        <div class="form-group" style="display:none" id="div_internal" runat="server">
                            <label for="ArticleLink">Select Web Page</label>
                            <asp:DropDownList ID="InternalPages" runat="server" AppendDataBoundItems="false" CssClass="form-control" AutoPostBack="false" ></asp:DropDownList>
                        </div>
                        <div class="form-group" style="display:none" id="div_file" runat="server">
                            <label for="ArticleLink">Select File</label>
                            <input type="file" id="LMFile" class="filestyle" data-text="Browse"  runat="server" data-size="sm"/>
                        </div>
                        <div  style="min-height: 300px; padding: 0 20px;display:none" id="div_editor" runat="server">
                        <h4>Content</h4>
                            <div data-ng-controller="CreationCtrl">
                                <telerik:RadEditor ID="ContentEditor" runat="server" Height="600px" Width="100%" Skin="MetroTouch" >
                                    <ImageManager ViewPaths="~/Content/img/Content/Articles" UploadPaths="~/Content/img/Content/Articles" DeletePaths="~/Content/img/Content/Articles" EnableAsyncUpload="true" />
                                </telerik:RadEditor>
                            </div>
                            <br />
                        </div>
                        <div class="form-group" id="div_Img" runat="server" visible="false">
                            <label runat="server" for="ArticleImage" id="InputText">Image</label><small runat="server" id="Small1" class="form-text text-muted" style="display:inline; padding-left:5px">Videos must be pre-uploaded to videos folder using file manager in admin tools.</small>
                            <br />
                            <%--<input type="file" class="form-control-file" id="ArticleImage" aria-describedby="fileHelp" runat="server" onchange="refreshimage(this)"/>--%>
                            <asp:FileUpload runat="server" ID="ArticleImage" onchange="refreshimage(this);"/>
                            <asp:DropDownList ID="FileDDl" runat="server" AppendDataBoundItems="true" CssClass="form-control" Visible="false"></asp:DropDownList>
                            <small runat="server" id="fileHelp" class="form-text text-muted">Select the image for the article.</small>
                            <asp:Image ID="ImagePreview" runat="server" CssClass="img-thumbnail" Width="100" Height="75" style=";display:none"/>
                        </div>
    <%--                    <div class="form-group">
                            <label for="ctl00_Body_ArticleDate_dateInput">Article Date</label><br />
                            <telerik:RadDatePicker ID="ArticleDate" runat="server" />
                        </div>--%>
    
                        <div class="form-group" >
                            <input type="hidden" id="PageIdValue" value="" runat="server" />
                            <asp:Button ID="DeleteContent" runat="server" Text="Delete" CssClass="btn btn-danger" OnClick="DeleteContent_Click" style="display:none;float:right"/>
                            <asp:Button ID="ModifyContent" runat="server" Text="Save" CssClass="btn btn-primary" OnClick="ModifyContent_Click"  Style="float:right;margin-right:5px;" /><br /> <%--OnClientClick="return ProgressBar()"--%>
                            <asp:Label ID="Message" runat="server" CssClass="error"></asp:Label>
                        </div>
                         
    
                    </div>
                </div>
        </div>
    
    
                  
        <script>
    
            function rb_click(event) {
                var ext = document.getElementById("Body_div_external");
                var edt = document.getElementById("Body_div_editor");
                var int = document.getElementById("Body_div_internal");
                var fl = document.getElementById("Body_div_file"); 
                var txtbx = document.getElementById("Body_ArticleLink");
                if (event.value == 1) {
                    ext.style.display = "block";
                    edt.style.display = "none";
                    int.style.display = "none";
                    fl.style.display = "none";
                    txtbx.placeholder = 'www.google.com';
                }
                else if (event.value == 2) {
                    ext.style.display = "none";
                    edt.style.display = "block";
                    int.style.display = "none";
                    fl.style.display = "none";
                }
                else if (event.value == 3) {
                    ext.style.display = "none";
                    edt.style.display = "none";
                    int.style.display = "block";
                    fl.style.display = "none";
                }
                else if (event.value == 4) {
                    ext.style.display = "block";
                    edt.style.display = "none";
                    int.style.display = "none";
                    fl.style.display = "none";
                    txtbx.placeholder = 'john.doe@hp.com';
                }
                else if (event.value == 5) {
                    ext.style.display = "none";
                    edt.style.display = "none";
                    int.style.display = "none";
                    fl.style.display = "block";
                }
            };
            function rbSrc_click(event) {
                var dd = document.getElementById("Body_ContentDD");
                var dc = document.getElementById("Body_DeleteContent");
                var img = document.getElementById("Body_ImagePreview");
                if (event.value == "New") {
                    dd.style.display = "none";
                    dc.style.display = "none";
                    img.style.display = "none";
                }
                else {
                    dd.style.display = "block";
                    dc.style.display = "block";
                    img.style.display = "block";
                }
            };
    
            function refreshimage(event) {
                if (event.files && event.files[0] && $('#Body_ContentTypes').val()!='3') {
                    var reader = new FileReader();
                    reader.onload = function (e) {
                        //$('#imgpreview').css('visibility', 'visible');
                        $('#Body_ImagePreview').attr('src', e.target.result);
                    }
                    reader.readAsDataURL(event.files[0]);
                }
            };
    
        </script>
    </asp:Content>
    
    

    CS code behind

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.IO;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Telerik.Web.UI;
    
    public partial class App_Admin_CreateArticles : System.Web.UI.Page
    {
        private static string[] files;
        private static int PageId { get; set; }
    
        private static DataTable Data { get; set; }
    
        private static DataTable Data2 { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
    
            if (!Page.IsPostBack)
            {
                Init();
            }
            else
            {
                if (rbExisting.Checked)
                {
                    ContentDD.Style["display"] = "block";
                    DeleteContent.Style["display"] = "block";
                    ImagePreview.Style["display"] = "block";
                }
                else
                {
                    ContentDD.Style["display"] = "none";
                    DeleteContent.Style["display"] = "none";
                    ImagePreview.Style["display"] = "none";
                }
            }
        }
    
        private void Init()
        {
            ContentEditor.ToolbarMode = EditorToolbarMode.Default;
            ContentEditor.EnsureToolsFileLoaded();
            RemoveButton("AboutDialog");
            RemoveButton("ToggleScreenMode");
            RemoveButton("ModuleManager");
            RemoveButton("Zoom");
            RemoveButton("InsertCustomLink");
            RemoveButton("ImageMapDialog");
            RemoveButton("InsertSnippet");
            RemoveButton("InsertFormElement");
            RemoveButton("FormatStripper");
            RemoveButton("XhtmlValidator");
            RemoveButton("FormatCodeBlock");
            RemoveButton("InsertGroupbox");
            RemoveButton("InsertParagraph");
            RemoveButton("TemplateManager");
            RemoveButton("FlashManager");
            RemoveButton("DocumentManager");
            RemoveButton("ApplyClass");
    
            CreateArticle ca = new CreateArticle();
            DataTable dt = ca.GetSiteList();
    
            ContentTypes.DataSource = dt;
            ContentTypes.DataTextField = "ContentType";
            ContentTypes.DataValueField = "Id";
            ContentTypes.DataBind();
    
            GetFiles();
    
            if (files.Length > 0)
            {
                DataTable dt1 = new DataTable();
                dt1.Columns.Add("path", typeof(string));
                dt1.Columns.Add("file", typeof(string));
    
                foreach (string f in files)
                    dt1.Rows.Add(f, Path.GetFileName(f));
    
                FileDDl.DataSource = dt1;
                FileDDl.DataTextField = "file";
                FileDDl.DataValueField = "path";
                FileDDl.DataBind();
            }
            LoadSitesDropDown();
        }
    
        private void LoadSitesDropDown(bool SelectLast = false)
        {
            CreateContentData c = new CreateContentData();
            Data2 = c.GetSiteList();
    
            InternalPages.DataSource = Data2;
            InternalPages.DataTextField = "Title";
            InternalPages.DataValueField = "URL";
            InternalPages.DataBind();
            InternalPages.SelectedIndex = 0;
    
        }
        private void GetFiles()
        {
            string path = "Content\\img\\Content\\Videos";
            string fullPath = Path.Combine(Server.MapPath(@"\"), path);
            if (Directory.Exists(fullPath))
            {
                files = Directory.GetFiles(fullPath);
            }
            //FileDDl
        }
        private void RemoveButton(string toolName)
        {
            foreach (EditorToolGroup g in ContentEditor.Tools)
            {
                EditorTool tool = g.FindTool(toolName);
                if (tool != null)
                    g.Tools.Remove(tool);
            }
        }
        protected void DeleteContent_Click(object sender, EventArgs e)
        {
            int selectedValue = int.Parse(ContentDD.SelectedValue.ToString());
            string tabletodelete = int.Parse(ContentTypes.SelectedValue) < 3 ? "Articles":"Videos";
            if (selectedValue > 0)
            {
    
                DataAccess dac = new DataAccess();
                SqlParameter id = new SqlParameter("@id", selectedValue);
                try
                {
                    dac.ExecuteNonQuery("DELETE FROM " + tabletodelete + " WHERE ID=@ID", new SqlParameter[] { id });
                }
                catch (SqlException) { throw; }
                if (File.Exists(Server.MapPath("~/Content/img/Content/Videos/") + Path.GetFileName(PageIdValue.Value)))
                {
                    File.Delete(Server.MapPath("~/Content/img/Content/Videos/") + Path.GetFileName(PageIdValue.Value));
                    File.Delete(Server.MapPath("~/Content/img/Content/Videos/Thumbnails/") + Path.GetFileName(ImagePreview.ImageUrl));
                }
                IfSuccess();
                ContentDD.SelectedIndex = 0;
                ContentTypes.SelectedIndex = 0;
                div_elements.Visible = false;
            }
            else
            {
                Message.Text = "Please select an item to be deleted";
            }
        }
        protected void ModifyContent_Click(object sender, EventArgs e)
        {
            string id = ContentDD.SelectedValue;
            string title = ContentTitle.Value;
            string body = ContentEditor.Content;
            string imagePath = string.Empty;
            string filePath = string.Empty;
            string thumbnailPath = string.Empty;
            DateTime? articleDate = DateTime.Today;
            string summary = ArticleSummary.Value;
            string type = ContentTypes.SelectedValue;
            string IntPage = InternalPages.SelectedValue;
            short external = (short)1;
            string URL = string.Empty;
    
    
    
            if (string.IsNullOrEmpty(title) || (rbLocal.Checked && string.IsNullOrEmpty(body) && ContentTypes.SelectedIndex < 3) || ((rbExternal.Checked || rbEmail.Checked) && string.IsNullOrEmpty(ArticleLink.Value) && ContentTypes.SelectedIndex < 3) || string.IsNullOrEmpty(summary) || ContentTypes.SelectedIndex==0 || (rbInternal.Checked && InternalPages.SelectedIndex==0 && ContentTypes.SelectedIndex < 3) || (rbFile.Checked && LMFile.PostedFile != null && LMFile.PostedFile.ContentLength == 0 && ContentTypes.SelectedIndex < 3))
            {
                Message.Text = "One or more mandatory fields are empty";
                rbExternal.Checked = true;
                return;
            }
    
            thumbnailPath = ImagePreview.ImageUrl.Replace("../", "");
            imagePath = (ContentTypes.SelectedIndex < 3) ? ImagePreview.ImageUrl.Replace("../", "").Replace("Thumbnail\\","") : PageIdValue.Value;
            if (ArticleImage.PostedFile != null && ArticleImage.PostedFile.ContentLength > 0)
            {
                HttpPostedFile postedFile = ArticleImage.PostedFile;
                string fileExtension = Path.GetExtension(postedFile.FileName);
                if (!ValidExtension(fileExtension)) return;
                Operations op = new Operations();
                if (ContentTypes.SelectedIndex < 3)
                {
                    imagePath = op.SaveFileToServer(postedFile, Server.MapPath(@"\"), "Content\\img\\Content\\Articles\\");
                    thumbnailPath = op.CreateThumbnailImage(imagePath, Server.MapPath(@"\"), "Content\\img\\Content\\Articles\\Thumbnail\\", 100, 75);
                }
            }
            if (LMFile.PostedFile != null && LMFile.PostedFile.ContentLength > 0)
            {
                HttpPostedFile postedFile = LMFile.PostedFile;
                string fileExtension = Path.GetExtension(postedFile.FileName);
                if (!ValidExtension(fileExtension)) return;
                Operations op = new Operations();
                if (ContentTypes.SelectedIndex < 3)
                {
                    filePath = op.SaveFileToServer(postedFile, Server.MapPath(@"\"), "Content\\docs\\");
                }
            }
            if (ContentTypes.SelectedIndex==3)
            {
                imagePath = FileDDl.SelectedValue.Replace(Server.MapPath(@"\"), string.Empty);
                thumbnailPath = "Content\\img\\Content\\Videos\\ThumbNails\\" + Path.GetFileNameWithoutExtension(imagePath) + ".jpg";
                var ffMpeg = new NReco.VideoConverter.FFMpegConverter();
                ffMpeg.GetVideoThumbnail(imagePath, thumbnailPath, 5);
            }
    
            if (rbInternal.Checked)
            {
                URL = InternalPages.SelectedValue;
                external = 0;
            }
            else if (rbLocal.Checked)
            {
                URL = InternalPages.SelectedValue;
                external = 0;
            }
            else if(rbExternal.Checked)
            {
                URL = "//" + ArticleLink.Value;
                external = 1;
            }
            else if (rbEmail.Checked)
            {
                URL = "mailto:" + ArticleLink.Value;
                external = 1;
            }
            else if (rbFile.Checked)
            {
                URL = filePath;
                external = 1;
            }
    
            Dictionary<string, object> data = new Dictionary<string, object>();
            data.Add("id", id);
            data.Add("title", title);
            data.Add("body", body);
            data.Add("imagePath", imagePath);
            data.Add("thumbnailPath", thumbnailPath);
            data.Add("date", articleDate);
            data.Add("summary", summary);
            data.Add("type", type);
            data.Add("external", external);
            data.Add("Url", URL);
    
            CreateArticle ca = new CreateArticle();
            if (ContentTypes.SelectedIndex < 3) { ca.Create(data); } else { ca.CreateVideoEntry(data); }
            IfSuccess();
            ContentTypes.SelectedIndex = 0;
            ContentDD.SelectedIndex = 0;
            div_elements.Visible = false;
        }
    
        private bool ValidExtension(string extension)
        {
            string[] extensions =  ContentTypes.SelectedIndex < 3 ? new string[] { ".jpg", ".jpeg", ".bmp", ".png", ".doc", ".docx", ".pdf", ".ppt", ".pptx",".xls",".xlsx",".xlsm",".xlsb",".zip" } : new string[] { ".mp4", ".m4v", ".avi", ".mpeg" };
            return extensions.Contains(extension.ToLower());
        }
    
        private void IfSuccess()
        {
            PageIdValue.Value = string.Empty;
            ContentTitle.Value = string.Empty;
            ContentEditor.Content = string.Empty;
            //ArticleDate.SelectedDate = null;
            Message.Text = string.Empty;
            ArticleSummary.Value = string.Empty;
            ImagePreview.ImageUrl = string.Empty;
            ArticleLink.Value = string.Empty;
    
        }
        //Content_SelectedIndexChanged
        protected void Content_SelectedIndexChanged(object sender, EventArgs e)
        {
            string URLTemp = string.Empty;
            DropDownList content = sender as DropDownList;
            if (content == null) return;
            int selectedValue = int.Parse(content.SelectedValue.ToString());
            PageId = selectedValue;
            DataRow dr = (from d in Data.AsEnumerable() where d.Field<int>("ID") == selectedValue select d).First();
            ContentTitle.Value = dr.Field<string>("Title");
            if (ContentTypes.SelectedIndex == 3) { FileDDl.SelectedValue = Path.Combine(Server.MapPath(@"\"), dr.Field<string>("Path")); }
            ArticleSummary.Value = dr.Field<string>("Summary");
            if (int.Parse(ContentTypes.SelectedValue) == 3)
            {
                PageIdValue.Value = dr.Field<string>("Path");
            }
            else
            {
                div_external.Attributes.Add("style", "display:none");
                div_internal.Attributes.Add("style", "display:none");
                div_editor.Attributes.Add("style", "display:none");
    
                
                if (! string.IsNullOrEmpty(dr.Field<string>("Body")))
                {
                    ContentEditor.Content = dr.Field<string>("Body");
                    rbLocal.Checked = true;
                    div_editor.Attributes.Add("style", "display:block");
                }
                else
                {
                    URLTemp = dr.Field<string>("ExternalUrl");
                    //ArticleLink.Value = dr.Field<string>("ExternalUrl").Replace("emailto:","");
                    if (URLTemp.Substring(0,2).ToString() == "//")
                    {
                        ArticleLink.Value = URLTemp.Substring(2);
                        rbExternal.Checked = true;
                        div_external.Attributes.Add("style", "display:block");
                    }
                    else if (URLTemp.Contains("mailto:"))
                    {
                        ArticleLink.Value = URLTemp.Replace("mailto:","");
                        rbEmail.Checked = true;
                        div_external.Attributes.Add("style", "display:block");
                    }
                    //WebPagePlaceHolder.aspx?PageID=12                                                                   
                    else if (URLTemp.Contains("WebPagePlaceHolder"))
                    {
                        rbInternal.Checked = true;
                        div_internal.Attributes.Add("style", "display:block");
                        InternalPages.SelectedValue = URLTemp.TrimEnd();
                    }
                }
            }
            ImagePreview.ImageUrl = "../"+dr.Field<string>("ImagePath");
            
    
    
    
    }
    
        protected void Type_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList contentTypes = sender as DropDownList;
            if (contentTypes == null) return;
    
            int selectedValue = int.Parse(contentTypes.SelectedValue.ToString());
            if (selectedValue > 0)
            {
                CreateContentData ca = new CreateContentData();
                div_elements.Visible = true;
                switch (selectedValue)
                {
                    case 1:
                    case 2:
                        div_rb.Visible = true;
                        div_external.Style["display"] = "block";
                        div_Img.Visible = true;
                        InputText.InnerText = "Image";
                        ArticleImage.Visible = true;
                        fileHelp.InnerText = "Select the image for the article.";
                        Data = ca.GetArticles(selectedValue);
                        FileDDl.Visible = false;
                        Small1.Visible = false;
                        break;
                    case 3:
                        div_rb.Visible = false;
                        div_external.Style["display"] = "none";
                        div_Img.Visible = true;
                        ArticleImage.Visible = false;
                        InputText.InnerText = "Video";
                        Small1.Visible = true;
                        FileDDl.Visible = true;
                        fileHelp.InnerText = "Select the video for the article.";
                        Data = ca.GetVideosList();
                        break;
                }
                ContentDD.DataSource = Data;
                ContentDD.DataTextField = "Title";
                ContentDD.DataValueField = "Id";
                ContentDD.DataBind();
                IfSuccess();
                ContentDD.SelectedIndex = 0;
            }
            else
            {
                div_elements.Visible = false;
            }
        }
    
    }

    There's no DDContent anywhere in the post code.

    Wednesday, November 28, 2018 11:42 PM
  • User727658907 posted

    My bad.... it is ContentDD

    Thursday, November 29, 2018 12:00 AM
  • User475983607 posted

    The dropdown has a default style of dispaly:none.  The only time the dropdown is visible is when rbExisting.Checked is checked on a post back.  Please try running your logic through the Visual Studio debugger to verify the code logic your wrote matches your expectation.

        protected void Page_Load(object sender, EventArgs e)
        {
    
            if (!Page.IsPostBack)
            {
                Init();
            }
            else
            {
                if (rbExisting.Checked)
                {
                    ContentDD.Style["display"] = "block";
                    DeleteContent.Style["display"] = "block";
                    ImagePreview.Style["display"] = "block";
                }
                else
                {
                    ContentDD.Style["display"] = "none";
                    DeleteContent.Style["display"] = "none";
                    ImagePreview.Style["display"] = "none";
                }
            }
        }

    Thursday, November 29, 2018 12:14 AM