lundi 21 décembre 2015

Adding taxonomy web tagging controls dynamically on to the user interface (aspx page) with update panel under the button click

I am trying to add a taxonomy web tagging control on to the UI dynamically when a button click event is raised, the button is placed inside of an update panel, i followed many links and as per the suggestions i am adding code for endrequest method for taxonomy controls... Adding a taxonomy control dynamically works fine if you do on page load but my scenario is to add them when a button clic event was raised from update panel ..... any help is appreciated.

public partial class TestTaxanamyControl : WebPart
{

    public TestTaxanamyControl()
    {
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        InitializeControl();
    }

         protected void Page_Load(object sender, EventArgs e)
    {


    }

    protected void PostbackButton_OnClick(object sender, EventArgs e)
    {

    }



    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        TaxonomyWebTaggingControl TaxonomyControl = new TaxonomyWebTaggingControl();
        SPContext context = SPContext.Current;
        SPSite site = context.Site;
        TaxonomySession session = new TaxonomySession(site);
        TermStore termStore = session.TermStores[0];
        Group group = termStore.Groups[0];
        TermSet productsTermSet = group.TermSets[0];
        TaxonomyControl.SspId.Add(termStore.Id);
        TaxonomyControl.TermSetId.Add(productsTermSet.Id);
        TaxonomyControl.IsAddTerms = true;
        TaxonomyControl.AllowFillIn = true;
        TaxonomyControl.IsMulti = true;
        div1.Controls.Add(TaxonomyControl);
        String key = "TaxonomyWebTaggingAjaxIncludeOnce";
        if (!this.Page.ClientScript.IsClientScriptBlockRegistered(base.GetType(), key))
        {

            this.Page.ClientScript.RegisterClientScriptBlock(base.GetType(), key, GetReloadJavaScript(TaxonomyControl), true);
        }
        //TextBox txt = new TextBox();
        //txt.ID = "text";
        //txt.Text = "Sample Text";
        //div1.Controls.Add(txt);

    }
    private string GetReloadJavaScript(TaxonomyWebTaggingControl taxonomyControl)
    {
        String script = String.Empty;

        String containerId = SPEncode.ScriptEncode(taxonomyControl.Controls[1].ClientID);

        Type type_TaxonomyWebTaggingControl = typeof(TaxonomyWebTaggingControl);

        MethodInfo mi_getOnloadJavascript = type_TaxonomyWebTaggingControl.GetMethod("getOnloadJavascript", BindingFlags.NonPublic | BindingFlags.Instance);
        String fullScript = (String)mi_getOnloadJavascript.Invoke(taxonomyControl, null);
        int pos = fullScript.IndexOf(String.Format("function {0}_load()", containerId));

        if (pos > -1)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("var myPrm = Sys.WebForms.PageRequestManager.getInstance();");
            builder.Append("myPrm.add_endRequest(EndRequest);");
            builder.Append("function EndRequest(sender, args)");
            builder.Append("{");
            // we get te first part of the script needed to initialization
            // we start from pos 1, because we don't need the leading '{'
            builder.Append(fullScript.Substring(1, pos - 1));
            builder.Append("Microsoft.SharePoint.Taxonomy.ScriptForWebTaggingUI.onLoad('");
            builder.Append(containerId);
            builder.Append("');");
            builder.Append("}}");

            script = builder.ToString();
        }

        return script;
    }
}


///////////////////    ASPX DESIGN PAGE MARKUP///////////////////////

 <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Always" RenderMode="Block"      runat="server">
<ContentTemplate>

  <div id="div1" runat="server">


 </div>


   <p><asp:LinkButton ID="PostbackButton" OnClick="PostbackButton_OnClick"  runat="server">Postback</asp:LinkButton></p>
           <p><asp:LinkButton ID="LinkButton1"   runat="server"                  OnClick="LinkButton1_Click">GetControl</asp:LinkButton></p>


     </ContentTemplate>
     </asp:UpdatePanel>




Aucun commentaire:

Enregistrer un commentaire