Drag drop dynamically generated webpart

Last post 08-06-2009 5:31 AM by vinz. 10 replies.

Sort Posts:

  • Drag drop dynamically generated webpart

    06-26-2009, 7:23 AM
    • Member
      2 point Member
    • joshijimit
    • Member since 06-26-2009, 11:16 AM
    • Posts 8

     hello All,

    I am new in webpart.

    I am adding a usercontrol dynamically to a webpartzone.

    It is displaying properly but when I am trying to drag it, It will disapear.

    I am herewith sending you code snippet.

    if (!IsPostBack)
                {

    Control uc = this.LoadControl("control1.ascx");
                    uc.ID = "wp2";
                    GenericWebPart wp2 = WebPartManager1.CreateWebPart(uc);
                    wp2.AllowZoneChange = true;
                    wp2.Title = "dynemic webpart";
                    WebPartManager1.AddWebPart(wp2, WebPartZone1, 1);
                }

    Can you tell me can I drag this control from one zone to another?

    thanks in advance

  • Re: Drag drop dynamically generated webpart

    06-27-2009, 2:13 AM

    http://blogs.neudesic.com/blogs/david_barkol/archive/2006/02/10/45.aspx

    Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
  • Re: Drag drop dynamically generated webpart

    06-28-2009, 9:28 PM
    • All-Star
      91,698 point All-Star
    • vinz
    • Member since 10-05-2007, 11:47 AM
    • Cebu, Philippines
    • Posts 13,769
    • TrustedFriends-MVPs
    "Code,Beer and Music ~ my way of being a programmer"

  • Re: Drag drop dynamically generated webpart

    06-29-2009, 12:34 AM
    • Member
      2 point Member
    • joshijimit
    • Member since 06-26-2009, 11:16 AM
    • Posts 8

     Hello ramireddyindia,

    I went through the given link and did the same but I am getting error

    on the following line

    <testsite:MyWebPartManager ID="MyWebPartManager1" runat="server" />

    its a parsing error generated on runtime...

    I registered the component by adding

    <%@ Register TagPrefix="testsite" Namespace="TestSite" %>

    but still the tag is not become available on page..

    Can you guide me whats  the proble is??

    Thanks

  • Re: Drag drop dynamically generated webpart

    06-29-2009, 1:54 AM
    Answer
    step1: this is the custom webpart manager i created. 
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    namespace CustomWebPartManager
    {
        [DefaultProperty("Text")]
        [ToolboxData("<{0}:CustomWebPartManagerControl runat=server></{0}:CustomWebPartManagerControl>")]
        public class CustomWebPartManagerControl : WebPartManager
        {
            [Bindable(true)]
            [Category("Appearance")]
            [DefaultValue("")]
            [Localizable(true)]
            public string Text
            {
                get
                {
                    String s = (String)ViewState["Text"];
                    return ((s == null) ? String.Empty : s);
                }
    
                set
                {
                    ViewState["Text"] = value;
                }
            }
            public void SetDirty()
            {
                // Invoke the protected SetPersonalizationDirty method      
                SetPersonalizationDirty();
            }
        }
    }
    

    step2: Build this custom control Application, it will build successfully.

    step3: Now go to your original application. there go to your toolbox, add a tab and right click there, choose the options, choose items. now a dialog box will come, through browse button, choose the dll file, that built in the custom control, now click ok.

    then a control will add to your toolbox.

    step4: Now drag that control into your webpage, in the place of webpartmanager. (when you drag, automatically the reister tag will come)

    step5: Now Call the setDirty() method immediately after you adding your webpart.
     

    Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
  • Re: Drag drop dynamically generated webpart

    06-29-2009, 4:05 AM
    • Member
      2 point Member
    • joshijimit
    • Member since 06-26-2009, 11:16 AM
    • Posts 8

     Hello ramireddy,

    Thanks for your detail description.. I added Assembly attribute in Register tag and the problem was solved.

    but now I stuck with another problem which I cant figure out.

    Problem is when I executed the application first time it shows all fine but when I close the explorer and run it in another explorer

    the webpart is getting duplicated. I can see two webpart with name webpart [1] and webpart [2].

    each time it is generating new one wich name webpart [N].. can you tell me pls what wrong..

     

  • Re: Drag drop dynamically generated webpart

    06-29-2009, 4:37 AM
    • Member
      2 point Member
    • joshijimit
    • Member since 06-26-2009, 11:16 AM
    • Posts 8

     Hello ramireddy,

    I am sending you the code snippet,

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    HttpCookie authCookie = Request.Cookies.Get(FormsAuthentication.FormsCookieName);
                    if (authCookie == null)
                    {
                        string MyCookieName = "Reminder";
                        System.Web.HttpCookie MyCookie = Request.Cookies.Get(MyCookieName);
                        string UserID = null;
                        if (MyCookie == null)
                        {
                            UserID = System.Guid.NewGuid().ToString().Replace("-", "");
                            MyCookie = new System.Web.HttpCookie(MyCookieName, UserID);
                            MyCookie.Expires = DateTime.Now.AddYears(10);
                            Response.Cookies.Add(MyCookie);
                           
                        }
                        else
                        {
                            UserID = MyCookie.Value;
                        }
                        FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, UserID, DateTime.Now, DateTime.Now.AddSeconds(30), false, "roles");
                        string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
                        authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                        Response.Cookies.Add(authCookie);
                        Response.Redirect(Request.Url.ToString());           
                       
                    }
                    Control uc = this.LoadControl("control1.ascx");
                    uc.ID = "wp2";
                    GenericWebPart wp2 = WebPartManager1.CreateWebPart(uc);
                    wp2.AllowZoneChange = true;
                    wp2.Title = "dynamic webpart";
                    WebPartManager1.AddWebPart(wp2, WebPartZone1, 1);
                    WebPartManager1.SetDirty();
                }          
                WebPartManager1.DisplayMode = WebPartManager.DesignDisplayMode;
            }

  • Re: Drag drop dynamically generated webpart

    06-29-2009, 7:03 AM
    Answer

    you can check like below.

    Boolean WebPartExists = false;
                foreach (WebPart wp in CustomWebPartManagerControl1.WebParts)
                {
                    if (wp.ID == "wp2")
                        WebPartExists = true;
                }
                if (!WebPartExists)
                {
                   // your webpart add code
                }


     

    Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
  • Re: Drag drop dynamically generated webpart

    06-29-2009, 7:43 AM
    • Member
      2 point Member
    • joshijimit
    • Member since 06-26-2009, 11:16 AM
    • Posts 8

     hello ramireddy,

     

    Above code is working perfactly.. the only change I had to do is changing the condition of checking the ID of dynamically

    generated webpart as it generated some random no behind the variavle name wp.. dont know why it is doing so..

    but the final code looks like below..

    Boolean WebPartExists = false;
                    foreach (WebPart wp in WebPartManager1.WebParts)
                    {
                        if (wp.ID.StartsWith("wp"))
                            WebPartExists = true;
                    }
                    if (!WebPartExists)
                    {
                        Control uc = this.LoadControl("control1.ascx");
                        uc.ID = "wp2";
                        GenericWebPart wp2 = WebPartManager1.CreateWebPart(uc);
                        wp2.AllowZoneChange = true;
                        wp2.Title = "dynamic webpart";
                        WebPartManager1.AddWebPart(wp2, WebPartZone1, 0);
                        WebPartManager1.SetDirty();
                    }

    Thanks man....

  • Re: Drag drop dynamically generated webpart

    08-06-2009, 5:15 AM

    What did you do for the Random ID..? I am having same problem and I am not able to fix it. Please help me.


    Here is My code.


     Using DB As New LocalDB
                For Each Panel In (From c In DB.CollapsePanelTypes Where c.CreatorID = UserID).ToList
                    Dim IsExists As Boolean = False
                    For Each Web As WebPart In WebPartManager.WebParts
                        Response.Write(Web.ID & "<br>")
                        'If Web.ID.StartsWith("wp") Then
                        '    IsExists = True
                        'End If
                    Next
                
                    If Not IsExists Then
                        
                        Dim Control = LoadControl("~/Controls/PersonalisedPanel.ascx")
                        With CType(Control, PersonalisedPanel)
                            .ID = "PersonalisedPanel" & ID
                            .VisitorName = VisitorName
                            .PanelID = Panel.ID
                            .IsReadOnly = IsReadOnly
                            .VisitorName = VisitorName
                            .IsOwnProfile = IsOwnProfile
                            .IsAdmin = IsAdmin
                        
                            Dim ZoneIndex = Zone1.WebParts.Count
    
                            Dim gwp As GenericWebPart = WebPartManager.CreateWebPart(Control)
                            gwp.ChromeType = PartChromeType.None
                            gwp.ID = "manish"
                            gwp.Title = "Manish"
                            If ZoneIndex = 0 Then
                                WebPartManager.AddWebPart(gwp, Zone1, ZoneIndex)
                            Else
                                WebPartManager.AddWebPart(gwp, Zone1, ZoneIndex + 1)
                            End If
                            WebPartManager.SetDirty()
                        End With
                    End If
                        
                Next
            End Using

    Please tell me where I am wrong.

    Thanks.

  • Re: Drag drop dynamically generated webpart

    08-06-2009, 5:31 AM
    • All-Star
      91,698 point All-Star
    • vinz
    • Member since 10-05-2007, 11:47 AM
    • Cebu, Philippines
    • Posts 13,769
    • TrustedFriends-MVPs

    manishprajapati:
    What did you do for the Random ID..? I am having same problem and I am not able to fix it. Please help me.

    Hi manishprajapati,

    Please note that this thread was already resolved and was created 2 months ago.Replying to old threads like this is considered as hijacking..

    Please try to focus onto your thread.

    Thank you for understanding.

    "Code,Beer and Music ~ my way of being a programmer"

Page 1 of 1 (11 items)