ASP.NET - dynamic updatepanel and dropdownlistbox

Asked By Marcelo Melo on 21-May-12 07:04 PM
please, I'm not getting success with upadatepanel and dropdownlistbox in dynamic mode....
the postback is not running...


can someone help me, please ?


the code is bellow:
 
              foreach (DataRow askAsk in usr.GetDataByName(txtFindFriendName.Text.ToString()).Rows)
                    {


                        UpdatePanel upFriend = new UpdatePanel();
                        upFriend.ID = "upFriend" + askAsk.ItemArray[0].ToString();
                        upFriend.ChildrenAsTriggers = false;
                        upFriend.UpdateMode = UpdatePanelUpdateMode.Conditional;
                        upFriend.RenderMode = UpdatePanelRenderMode.Block;
 
                        ImageButton imgFindFriendUsrMail = new ImageButton();
                        imgFindFriendUsrMail.ID = "imgFindFriendUsrMail" + askAsk.ItemArray[0].ToString();
                        if (askAsk["foto"].ToString().Length > 0)
                            imgFindFriendUsrMail.ImageUrl = "fig/usrs/" + askAsk["foto"].ToString();
                        else
                            imgFindFriendUsrMail.ImageUrl = "fig/ling/noPhoto.jpg";
                        imgFindFriendUsrMail.OnClientClick = "javascript:return false;";
                        imgFindFriendUsrMail.Width = 150;
                        imgFindFriendUsrMail.Height = 100;




                        upFriend.ContentTemplateContainer.Controls.Add(imgFindFriendUsrMail);
                        upFriend.ContentTemplateContainer.Controls.Add(new LiteralControl("<br />"));


                        Label lblFindFriendUsrName = new Label();
                        lblFindFriendUsrName.ID = "lblFindFriendUsrName" + askAsk["idUSer"].ToString();
                        lblFindFriendUsrName.Text = askAsk["name"].ToString();
                        lblFindFriendUsrName.CssClass = "lblFindFriendUsrName";






                        upFriend.ContentTemplateContainer.Controls.Add(lblFindFriendUsrName);




                        DropDownList ddFindFriendAction = new DropDownList();
                        ddFindFriendAction.ID = "ddFindFriendAction" + askAsk["idUSer"].ToString();
                        ddFindFriendAction.CssClass = "ddFindFriendAction";
                        ddFindFriendAction.AutoPostBack = true;
                        ddFindFriendAction.SelectedIndexChanged += new EventHandler(ddFindFriendAction_SelectedIndexChanged);


                        ListItem chooseAsk0 = new ListItem();
               chooseAsk0.Text = "escolha uma opção";
                        chooseAsk0.Value = "0";
                        ddFindFriendAction.Items.Add(chooseAsk0);
 
                        upFriend.ContentTemplateContainer.Controls.Add(ddFindFriendAction);
                        upFriend.ContentTemplateContainer.Controls.Add(new LiteralControl("<br />"));
 
                        AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
                        trigger.ControlID = "ddFindFriendAction" + askAsk["idUSer"].ToString();
  trigger.EventName = "SelectedIndexChanged";
       
                
                        upFriend.Triggers.Add(trigger);


                        upFriend.Update();
                        PlaceHolder1.Controls.Add(upFriend);
                    }
 
Jitendra Faye replied to Marcelo Melo on 22-May-12 12:57 AM
Concentrate on these lines-

DropDownList ddFindFriendAction = new DropDownList();
                        ddFindFriendAction.ID = "ddFindFriendAction" + askAsk["idUSer"].ToString();
                        ddFindFriendAction.CssClass = "ddFindFriendAction";
                        ddFindFriendAction.AutoPostBack = true;

                        ddFindFriendAction.SelectedIndexChanged += new EventHandler(ddFindFriendAction_SelectedIndexChanged);
    //Put a break point also here and debug your code

Here you are binding ddFindFriendAction_SelectedIndexChanged() method to event , but Have you create any method by name ddFindFriendAction_SelectedIndexChanged() in your code.




Chintan Vaghela replied to Marcelo Melo on 22-May-12 03:21 AM

Hello,

You need to Override OnLoad Event for Added Dynemic Update Panel.

Added Update Panel Dynemic on OnLoad Event.

OverRide OnLoad Event as following way

Declare DropDownList and assign to Update Panel as following way

 

DropDownList ddFindFriendAction;

protected override void OnLoad(EventArgs e)

{

foreach (DataRow askAsk in usr.GetDataByName(txtFindFriendName.Text.ToString()).Rows)

      {

ddFindFriendAction = new DropDownList();

        ddFindFriendAction.ID = "ddFindFriendAction" + askAsk["idUSer"].ToString();

        ddFindFriendAction.CssClass = "ddFindFriendAction";

        ddFindFriendAction.AutoPostBack = true;

        ddFindFriendAction.SelectedIndexChanged += new EventHandler(ddFindFriendAction_SelectedIndexChanged);

 

      }

}

void ddFindFriendAction_SelectedIndexChanged(object sender, EventArgs e)

    {

      string a = ddFindFriendAction.SelectedValue;

    

    }

 

Hope this helpful!

Thanks

 

 

 

Marcelo Melo replied to Chintan Vaghela on 22-May-12 09:19 AM
thanks a lot, R.B. and sorry about my poor english


but


doesnt work here....



two things :


the code is inside

    protected void lkFinfFriendClick_Click(object sender, EventArgs e)
    {
it's a procedure to find friends, and put them inside a MULTIVIEW - PLACEHOLDER 
updatepanels and dropdownlist in dynamics mode
   }


it's not inside the page_load




and I tried put the override, but it got me some error...

no suitable method found to override





ps: the dropdown gives a asyncpostback, but doesnt run my code, and in the second dropdownIndexChanged, the page gets a refresh....





thank you again

[)ia6l0 iii replied to Marcelo Melo on 28-May-12 01:33 PM
Move this piece of code to the Init event.