locked
popup insert control -> imagebutton RRS feed

  • Question

  • User-751080518 posted

    Hi.

    I used popup insert control in my web site.  I would like to show ImageButton with image instead of standart Button on Foreign Field template.

    What should I change to achieved that?

    Monday, April 4, 2011 4:50 AM

Answers

  • User-330204900 posted

    Sorry I will have a look at the source code and add the ability to use an image in the next version, I forgot that I completly overode the render method;

    protected override void Render(HtmlTextWriter writer)
    {
        //base.Render(writer);
        //declare the function and variable names
        var popupId = this.UniqueID.Replace("$", "").Replace("_", "");
        var functionName = "NAC_" + popupId + "Func";
        var variableName = "NAC_" + popupId + "Var";
        var scrollBars = ScrollBars ? "yes" : "no";
    
        StringBuilder script = new StringBuilder();
        script.Append("\n<script language=\"JavaScript\" type=\"text/javascript\">\n");
        script.Append("function " + functionName + "() {\n");
        script.Append("var " + variableName + " = NAC_OpenPopup('" +
            PostBackUrl + "', '" +
            QueryStringField + "', '" +
            this.UniqueID + "', '" +
            AdditionalQueryString + "', '" +
            Title + "', '" +
            WindowWidth + "', '" +
            WindowHeight + "', '" +
            scrollBars + "')\n");
        //script.Append("while (!" + variableName + ".closed) { }\n");
        //script.Append(Page.ClientScript.GetPostBackEventReference(this, "") + ";\n");
        script.Append("}\n");
        script.Append("\n</script>\n");
    
        writer.Write(script.ToString());
    
        // render button
        writer.AddAttribute(HtmlTextWriterAttribute.Id, linkButtonId);
        writer.AddAttribute(HtmlTextWriterAttribute.Name, linkButtonId);
        writer.AddAttribute(HtmlTextWriterAttribute.Type, "button");
    
        // set CSS styling
        if (!String.IsNullOrEmpty(CssClass))
            writer.AddAttribute(HtmlTextWriterAttribute.Class, CssClass);
    
        writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "javascript:" + functionName + "();");
        writer.RenderBeginTag(HtmlTextWriterTag.Button);
        writer.Write(Text);
        writer.RenderEndTag();
    
        // hidden field
        //<input type="hidden" name="HiddenField1" id="HiddenField1" />
        writer.AddAttribute(HtmlTextWriterAttribute.Id, this.UniqueID);
        writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
        writer.AddAttribute(HtmlTextWriterAttribute.Type, "hidden");
        writer.RenderBeginTag(HtmlTextWriterTag.Input);
        writer.RenderEndTag();
    }

    As you can see with this line

    writer.RenderBeginTag(HtmlTextWriterTag.Button);
    

    I have had coded the tag that is being rendered you will need to alter this so that if you add an image path then it will render the correct tag and render the image.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 5, 2011 5:16 AM
  • User-751080518 posted

    Yeee!

    It's working now!

     

    /// <summary>
            /// Renders the control to the specified HTML writer.
            /// </summary>
            /// <param name="writer">
            /// The <see cref="T:System.Web.UI.HtmlTextWriter"/> 
            /// object that receives the control content.
            /// </param>
            protected override void Render(HtmlTextWriter writer)
            {
                //base.Render(writer);
                //declare the function and variable names
                var popupId = this.UniqueID.Replace("$", "");
                var functionName = "NAC_" + popupId + "Func";
                var variableName = "NAC_" + popupId + "Var";
    
                StringBuilder script = new StringBuilder();
                script.Append("\n<script language=\"JavaScript\" type=\"text/javascript\">\n");
                script.Append("//<![CDATA[\n");
                script.Append("function " + functionName + "() {\n");
                script.Append("var " + variableName + " = NAC_OpenPopup('" +
                    PostBackUrl + "', '" +
                    QueryStringField + "', '" +
                    this.UniqueID + "', '" +
                    Title.Replace(" ", "") + "', '" +
                    WindowWidth + "', '" +
                    WindowHeight + "');\n");
                //script.Append("while (!" + variableName + ".closed) { }\n");
                //script.Append(Page.ClientScript.GetPostBackEventReference(this, "") + ";\n");
                script.Append("//]]>\n");
                script.Append("}\n");
                script.Append("\n</script>\n");
    
                writer.Write(script.ToString());
    
                // render button
                writer.AddAttribute(HtmlTextWriterAttribute.Id, linkButtonId);
                writer.AddAttribute(HtmlTextWriterAttribute.Name, linkButtonId);
                writer.AddAttribute(HtmlTextWriterAttribute.Src, ImageUrl);
                writer.AddAttribute(HtmlTextWriterAttribute.Type, "Image");
    
                // set CSS styling
                if (!String.IsNullOrEmpty(CssClass))
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, CssClass);
    
                writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "javascript:" + functionName + "();");
                writer.RenderBeginTag(HtmlTextWriterTag.Input);
                writer.Write(Text);
                //writer.RenderEndTag();
    
                // hidden field
                //<input type="hidden" name="HiddenField1" id="HiddenField1" />
                writer.AddAttribute(HtmlTextWriterAttribute.Id, this.UniqueID);
                writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
                writer.AddAttribute(HtmlTextWriterAttribute.Type, "hidden");            
                writer.RenderBeginTag(HtmlTextWriterTag.Input);
                writer.RenderEndTag();
            }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 5, 2011 5:31 AM

All replies

  • User-330204900 posted

    Just change the button in the Popup control to inherit from ImageButton not Button.

    Monday, April 4, 2011 5:44 AM
  • User-751080518 posted

    I tried that, but when I set ImageUrl, image newer shows up.

    I triad all variants for the image path.

    Monday, April 4, 2011 8:05 AM
  • User-751080518 posted

    If I look at the source that is build. There is still Button on the page even if I set in PopupButton.cs to inherit from ImageButton.

    Probably I should change something in Render procedure in same file?

    Tuesday, April 5, 2011 5:15 AM
  • User-330204900 posted

    Sorry I will have a look at the source code and add the ability to use an image in the next version, I forgot that I completly overode the render method;

    protected override void Render(HtmlTextWriter writer)
    {
        //base.Render(writer);
        //declare the function and variable names
        var popupId = this.UniqueID.Replace("$", "").Replace("_", "");
        var functionName = "NAC_" + popupId + "Func";
        var variableName = "NAC_" + popupId + "Var";
        var scrollBars = ScrollBars ? "yes" : "no";
    
        StringBuilder script = new StringBuilder();
        script.Append("\n<script language=\"JavaScript\" type=\"text/javascript\">\n");
        script.Append("function " + functionName + "() {\n");
        script.Append("var " + variableName + " = NAC_OpenPopup('" +
            PostBackUrl + "', '" +
            QueryStringField + "', '" +
            this.UniqueID + "', '" +
            AdditionalQueryString + "', '" +
            Title + "', '" +
            WindowWidth + "', '" +
            WindowHeight + "', '" +
            scrollBars + "')\n");
        //script.Append("while (!" + variableName + ".closed) { }\n");
        //script.Append(Page.ClientScript.GetPostBackEventReference(this, "") + ";\n");
        script.Append("}\n");
        script.Append("\n</script>\n");
    
        writer.Write(script.ToString());
    
        // render button
        writer.AddAttribute(HtmlTextWriterAttribute.Id, linkButtonId);
        writer.AddAttribute(HtmlTextWriterAttribute.Name, linkButtonId);
        writer.AddAttribute(HtmlTextWriterAttribute.Type, "button");
    
        // set CSS styling
        if (!String.IsNullOrEmpty(CssClass))
            writer.AddAttribute(HtmlTextWriterAttribute.Class, CssClass);
    
        writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "javascript:" + functionName + "();");
        writer.RenderBeginTag(HtmlTextWriterTag.Button);
        writer.Write(Text);
        writer.RenderEndTag();
    
        // hidden field
        //<input type="hidden" name="HiddenField1" id="HiddenField1" />
        writer.AddAttribute(HtmlTextWriterAttribute.Id, this.UniqueID);
        writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
        writer.AddAttribute(HtmlTextWriterAttribute.Type, "hidden");
        writer.RenderBeginTag(HtmlTextWriterTag.Input);
        writer.RenderEndTag();
    }

    As you can see with this line

    writer.RenderBeginTag(HtmlTextWriterTag.Button);
    

    I have had coded the tag that is being rendered you will need to alter this so that if you add an image path then it will render the correct tag and render the image.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 5, 2011 5:16 AM
  • User-751080518 posted

    That was quick :)

    Tuesday, April 5, 2011 5:18 AM
  • User-751080518 posted

    Yeee!

    It's working now!

     

    /// <summary>
            /// Renders the control to the specified HTML writer.
            /// </summary>
            /// <param name="writer">
            /// The <see cref="T:System.Web.UI.HtmlTextWriter"/> 
            /// object that receives the control content.
            /// </param>
            protected override void Render(HtmlTextWriter writer)
            {
                //base.Render(writer);
                //declare the function and variable names
                var popupId = this.UniqueID.Replace("$", "");
                var functionName = "NAC_" + popupId + "Func";
                var variableName = "NAC_" + popupId + "Var";
    
                StringBuilder script = new StringBuilder();
                script.Append("\n<script language=\"JavaScript\" type=\"text/javascript\">\n");
                script.Append("//<![CDATA[\n");
                script.Append("function " + functionName + "() {\n");
                script.Append("var " + variableName + " = NAC_OpenPopup('" +
                    PostBackUrl + "', '" +
                    QueryStringField + "', '" +
                    this.UniqueID + "', '" +
                    Title.Replace(" ", "") + "', '" +
                    WindowWidth + "', '" +
                    WindowHeight + "');\n");
                //script.Append("while (!" + variableName + ".closed) { }\n");
                //script.Append(Page.ClientScript.GetPostBackEventReference(this, "") + ";\n");
                script.Append("//]]>\n");
                script.Append("}\n");
                script.Append("\n</script>\n");
    
                writer.Write(script.ToString());
    
                // render button
                writer.AddAttribute(HtmlTextWriterAttribute.Id, linkButtonId);
                writer.AddAttribute(HtmlTextWriterAttribute.Name, linkButtonId);
                writer.AddAttribute(HtmlTextWriterAttribute.Src, ImageUrl);
                writer.AddAttribute(HtmlTextWriterAttribute.Type, "Image");
    
                // set CSS styling
                if (!String.IsNullOrEmpty(CssClass))
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, CssClass);
    
                writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "javascript:" + functionName + "();");
                writer.RenderBeginTag(HtmlTextWriterTag.Input);
                writer.Write(Text);
                //writer.RenderEndTag();
    
                // hidden field
                //<input type="hidden" name="HiddenField1" id="HiddenField1" />
                writer.AddAttribute(HtmlTextWriterAttribute.Id, this.UniqueID);
                writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
                writer.AddAttribute(HtmlTextWriterAttribute.Type, "hidden");            
                writer.RenderBeginTag(HtmlTextWriterTag.Input);
                writer.RenderEndTag();
            }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 5, 2011 5:31 AM
  • User-330204900 posted

    Glad your sorted.

    Tuesday, April 5, 2011 7:12 AM
  • User-751080518 posted

    Hi.

    I found out, that this isn't working if I set EnablePartialRender to true in Site.master.

    In my app I use ListDetails page template. I also tried your sample PopupInsert, set default page to ListDetails and it also doesnt work with EnablePartialRender=true.

    Error: Microsoft JScript runtime error: 'NAC_ctl00ContentPlaceHolder1FormView1ctl02ctl38__q_m_GREZNICE_Odjemna_Mesta_sifPopupInsert1Func' is undefined

    Do you know what could that be?

    Thanks in advance!

    Borut

    Wednesday, June 1, 2011 5:48 AM
  • User-330204900 posted

    Looks liek you are missing a script have you tried the same from my blog?

    Wednesday, June 1, 2011 6:03 AM
  • User-751080518 posted

    Yes, I have downloaded code from your blog and changed page template from List to ListDetails.

    Then I get same error as I described in previous post.

    Wednesday, June 1, 2011 8:31 AM
  • User-330204900 posted

    I was asking does the sample from my blog work as is?

    Thursday, June 2, 2011 4:28 AM
  • User-751080518 posted

    Your sample from blog works just fine. But in my appp I am using ListDetails page, so I tried that also in your sample.

    I changed this lines in global.asax:

    FROM:

    routes.Add(new DynamicDataRoute("{table}/{action}.aspx")
    {
    	Model = model
    	Constraints = new RouteValueDictionary(new { action = 	"List|Details|Edit|Insert|PopupInsert" }),
    });
    TO:
    routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx")
                {
                    Action = PageAction.List,
                    ViewName = "ListDetails",
                    Model = model
                });
    
                routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx")
                {
                    Action = PageAction.Details,
                    ViewName = "ListDetails",
                    Model = model
                })
    And now it doesn't work anymore. Can you try that and see what goes wrong?
    Friday, June 3, 2011 1:55 AM
  • User-330204900 posted

    I just tested it and what you need to do is;

    routes.Add(new DynamicDataRoute("{table}/{action}.aspx")
    {
        Constraints = new RouteValueDictionary(new { action = "PopupInsert" }),
        Model = DefaultModel
    });
    
    //routes.Add(new DynamicDataRoute("{table}/{action}.aspx")
    //{
    //    Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
    //    Model = DefaultModel
    //});
    
    routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx")
    {
        Action = PageAction.List,
        ViewName = "ListDetails",
        Model = DefaultModel
    });
    
    routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx")
    {
        Action = PageAction.Details,
        ViewName = "ListDetails",
        Model = DefaultModel
    });

    Note you need to route for the PopupInsert view.

    Friday, June 3, 2011 4:41 AM
  • User-751080518 posted

    Nop. It still doesn't work.

    When I click on New button i get this error:

    Error: 'NAC_ctl00ContentPlaceHolder1DetailsView1__ProductModelPopupButton1Func' is undefined

    Friday, June 3, 2011 7:55 AM
  • User-330204900 posted

    If you e-mail me I will send you the working same with ListDetails

    Friday, June 3, 2011 8:12 AM