คำตอบ Dropdownlist selectedIndexChanged in EditorPart

  • 12 เมษายน 2555 20:00
     
     

    Hi,

    I have a System.Web.UI.WebControls.DropDownlist created in an EditorPart class.  I implement the selectedIndexChanged event and set AutoPostPack to true.  When the event is fired, I would like to retrieve some properties from the WebPart that associates with the editorpart object; however, when the event is fired, my WebPart instance is recreated; therefore, editorPart is also recreated and I cannot get the right properties from the new WebPart instance.  All values are reset.  What should I do to fix this problem?  How to prevent the recreating of the WebPart?  That is very odd to me.

    Below is the structure of the code:
    public class MyWebPart : WebPart
    {
    ...
    public override EditorPartCollection CreateEditorParts()
    {
                var partsList = new List<EditorPart>();
                MyEditorPart part = new MyEditorPart();
                part.ID = this.ID + "EditorPart";
                partsList.Add(part);
                EditorPartCollection parts = new EditorPartCollection(partsList);
                return parts;
     }

    [Personalizable(PersonalizationScope.User), WebBrowsable(false)]
     public List<int> MyValueList
            {
                get { return _myVaueList; }
                set { _myVaueList= value; }
            }
    }
    Public class MyEditorPart : EditorPart
    {
    public override void SyncChanges()
     {
           this.EnsureChildControls();
           //retrieve MyValueList and display  
    }
     protected override void CreateChildControls()
    {
          //create dropdownlist
          DropDownList dd = new DropDownList();
         container.Controls.Add(dd);
          dd.AutoPostBack = true;
          dd.SelectedIndexChanged += new EventHandler(Dropdown_SelectedIndexChanged);
    }
    private void Dropdown_SelectedIndexChanged(object sender, EventArgs e)
    {
           MyWebPart webPart = WebPartToEdit as MyWebPart;
           List<int> list = webPart.MyValueList; //this list is empty since it is regenerated
           DropdownList dd = sender as DropdownList;
           int test = webPart.MyValueList[dd.SelectedIndex];
    }

    }

    Thank you very much for your help!

ตอบทั้งหมด

  • 13 เมษายน 2555 6:08
     
     

    Hi,

    Under the SyncChanges(), just try adding the below code & see if it maintains your selected value.

    public override void SyncChanges()
     {
           this.EnsureChildControls();

      MyWebPart webPart = WebPartToEdit as MyWebPart;

                if (webPart != null)
                {
                    foreach (string text in webPart.SelectedTabs)
                    {
                        dd.Items.FindByText(text).Selected = true;
                    }
                }

           //retrieve MyValueList and display

     }


    Please Mark Post as Answer once you get the solution for your issues.

    Thanks,
    Kunal Govani

  • 13 เมษายน 2555 11:59
     
     

    Kunal,

    Thanks but there is no SelectedTabs property for webPart.

    One thing that bothers me is that MyWebPart is invoked when I select another item in the dropdown.  However, I read in another thread that that is how AutoPostBack works, if it set to true the code goes thru WebPart and EditorPart again... 

    By the way, when I select an item from the dropdown list, as I observer, the following methods are invoked:

    1. MyWebPart()
    2. MyWebPart.CreateChildControls()
    3. MyEditorPart()
    4. MyEditorPart.Dropdown_SelectedIndexChanged()

    SyncChanges() is NOT invoked.


    • แก้ไขโดย mscustomer 13 เมษายน 2555 12:48
    • แก้ไขโดย mscustomer 13 เมษายน 2555 12:49
    •  
  • 13 เมษายน 2555 12:44
     
     
  • 13 เมษายน 2555 14:32
     
     

    Thanks Margriet! But it does not have what I want :(.

    From Dropdown_SelectedIndexChanged() event handler, I would like to retrieve the properties from the WebPart that my dropdown associates with.  However, at that point, my WebPart has been reinitialized and all my properties are reset.  My list is empty even though I already set my list to have some data in CreateChildControls().

    When I select a dropdown box item, SyncChanges() is not invoked, but the EditorPart constructor and WebPart constructor are invoked.


    • แก้ไขโดย mscustomer 13 เมษายน 2555 14:51
    • ทำเครื่องหมายเป็นคำตอบโดย mscustomer 13 เมษายน 2555 14:51
    • แก้ไขโดย mscustomer 13 เมษายน 2555 14:58
    • แก้ไขโดย mscustomer 13 เมษายน 2555 17:08
    • แก้ไขโดย mscustomer 13 เมษายน 2555 18:15
    • ยกเลิกการทำเครื่องหมายเป็นคำตอบโดย mscustomer 13 เมษายน 2555 18:16
    •  
  • 13 เมษายน 2555 19:10
     
     คำตอบ

    My solution for now:

    In SyncChanges(), I set the values into session (e.g Page.Session["myProperty"] = MyValueList).  In my event handler, I retrieve the values from the session.

    • ทำเครื่องหมายเป็นคำตอบโดย Qiao WeiMicrosoft, Moderator 19 เมษายน 2555 9:36
    •