Question

Update:

  protected override void CreateChildControls()
            {

                // autopostback
                ddlTopic = new DropDownList
                {
                    CssClass = "UserInput",
                    Width = 176,
                    ID = ID + "ddlTopic",
                    AutoPostBack = true
                };

                ddlTitle = new DropDownList
                {
                    CssClass = "UserInput",
                    Width = 176,
                    ID = ID + "ddlTitle",
                    AutoPostBack = false
                };


                //loading Topics 
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(url);
                XmlNodeList elemList = xmldoc.GetElementsByTagName("content:Topic");
                for (int i = 0; i < elemList.Count; i++)
                {
                    string name = elemList[i].Attributes["TopicName"].Value.ToString();
                    string id = elemList[i].Attributes["TopicId"].Value.ToString();

                    if (ddlTopic.Items.Contains(ddlTopic.Items.FindByValue(id)) == false)
                    {
                        this.ddlTopic.Items.Add(new ListItem(name, id));
                    }
                }
                this.ddlTopic.DataBind();
                ddlTopic.SelectedIndexChanged += ddlTopic_SelectedIndexChanged;


                var panel = new UpdatePanel { ID = ID + "UpdatePanel" };
                panel.ContentTemplateContainer.Controls.Add(
                    new LiteralControl(@"<table cellspacing=""0"" border=""0"" 
                    style=""border-width:0px;width:100%;
                    border-collapse:collapse;""><tr><td>
                    <div class=""UserSectionHead"">"));

                panel.ContentTemplateContainer.Controls.Add(
                    new LiteralControl(@"Select Topics:<br/>"));
                panel.ContentTemplateContainer.Controls.Add(ddlTopic);
                panel.ContentTemplateContainer.Controls.Add(
                    new LiteralControl(@"</div></td></tr>
                    <tr><td>
                    <div style=""width:100%"" class=""UserDottedLine"">
                    </div></td></tr><tr><td>
                    <div class=""UserSectionHead"">"));


                panel.ContentTemplateContainer.Controls.Add(
                   new LiteralControl(@"Select Title:<br/>"));
                panel.ContentTemplateContainer.Controls.Add(ddlTitle);
                panel.ContentTemplateContainer.Controls.Add(
                    new LiteralControl(@"</div></td></tr>
                    <tr><td>
                    <div style=""width:100%"" class=""UserDottedLine"">
                    </div></td></tr><tr><td>
                    <div class=""UserSectionHead"">"));

                Controls.Add(panel);
                base.CreateChildControls();              
}

    protected void ddlTopic_SelectedIndexChanged(object sender, EventArgs e)
    {
       //loading second dropdownlist
    }

    public override bool ApplyChanges()
            {
                var part = (XMLDDL.XMLDDL)WebPartToEdit;
            }

            public override void SyncChanges()
            {
                EnsureChildControls();
                var part = (XMLDDL.XMLDDL)WebPartToEdit;
            }

Update end

i am developing custom editor web part and in it i have two dropdownlist, the first dropdonwlist have autopost=true and based on the first selection the second dropdownlist loads.

my question is; after it do the postback the second dropdownlist loads but the other property selection disappears.

i have attached the screen shots...

enter image description here

enter image description here

Was it helpful?

Solution

If you are doing a postback, you need update the values of your controls in your SyncChanges() method implementation.

UPDATE: I think part of the confusion here is that the Editor part is a control, not a page or usercontrol, and further it has some very specific requirements/behavior. You are creating all the controls programmatically, and you need to set their values yourself. For dropdowns, you should (generally) bind/load the choices in CreateChildControls(), and then set the currenlty selected item (or default) in your SyncChanges() method. Generally you are setting the control's value to the value of the corresponding web part property. However, in your case I would bind/load the second dropdown in the SyncChanges method, because it sounds like the choices are dependent on whatever is selected in the first dropdown.

To save the web part properties, and persist them, you need to use ApplyChanges() to grab values from your controls and and set the web part properties.

So here is how you can implement this (shooting from the hip, cleanup required):

protected override void CreatChildControls()
{
   //create all your controls
   //load the choices for the first dropdown
}
public override void SyncChanges()
{
  EnsureChildControls();
  if (WebPartToEdit != null && WebPartToEdit is YourWebpartType
                && WebPartManager.Personalization.Scope == PersonalizationScope.Shared)
  {
     YourWebpartType webpart = (YourWebpartType)WebPartToEdit;
     //get the value for the first property and set the selection on the first dropdown
     ListItem item = ddlTemplateField.Items.FindByValue(webpart.SomeProperty);
     if (item != null)
       item.Selected = true; 
     //carry on, bind/load the second dropdown, and then set it's selected value
  }
}
public override void ApplyChanges()
{
  EnsureChildControls();
  if (WebPartToEdit != null && WebPartToEdit is YourWebpartType
                && WebPartManager.Personalization.Scope == PersonalizationScope.Shared)
  {
     YourWebpartType webpart = (YourWebpartType)WebPartToEdit;
     webpart.SomeProperty = firstDropDown.SelectedValue;
     //etc...

  }
}

OTHER TIPS

Do you have viewstate turned on? When are these controls created, in createchildcontrols? Are you adding them to the controls collection? Are you storing the value of this dropdown list in a webpart property? Are you setting the selectedindex after creating the control?

It would help you posted your editorpart code.

Update:

Where is the code for the dropdownlist controls? To get an idea of the code we need to see, below is some editorpart code that I have used before. This is an editor part that uses the PeopleEditor control to allow you to select a user that the webpart renders.

public class UserDataEditorPart : EditorPart
{
    PeopleEditor _p;

    /// <summary>
    /// Constructor for the class.
    /// </summary>
    public UserDataEditorPart()
    {
        this.Title = "UserID Information";
    }

    /// <summary>
    /// This method is called by the ToolPane object to apply property changes to the selected Web Part. 
    /// </summary>
    public override bool ApplyChanges()
    {
        // Apply property values here.
        UserDataWebPart wp1 = (UserDataWebPart)this.WebPartToEdit;
        try
        {
            wp1.StoredUserID = _p.CommaSeparatedAccounts;
            return true;
        }
        catch
        {
            return false;
        }
    }

    /// <summary>
    /// If the ApplyChanges method succeeds, this method is called by the ToolPane object
    /// to refresh the specified property values in the toolpart user interface.
    /// </summary>
    public override void SyncChanges()
    {
        // Sync with the new property changes here.
    }

    /// <summary>
    /// Render this tool part to the output parameter specified.
    /// </summary>
    /// <param name="output">The HTML writer to write out to </param>
    protected override void Render(HtmlTextWriter output)
    {
        output.Write("Domain UserID (Domain\\UserID):");
        output.Write("<br>");
        _p.RenderControl(output);
        output.Write("<br><div style='width:100%' class='UserDottedLine'></div>");
    }

    protected override void CreateChildControls()
    {
        UserDataWebPart wp1 = (UserDataWebPart)this.WebPartToEdit;
        _p = new PeopleEditor();
        _p.ID = wp1.ID + "_PeopleEditor";
        _p.AllowEmpty = true;
        _p.AllowTypeIn = true;
        _p.MultiSelect = false;
        _p.PlaceButtonsUnderEntityEditor = false;
        _p.SelectionSet = "User";
        _p.Width = Unit.Pixel(195);
        _p.CommaSeparatedAccounts = wp1.StoredUserID;
        _p.PrincipalSource = SPPrincipalSource.UserInfoList;
        Controls.Add(_p);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top