Καλώς ορίσατε στο dotNETZone.gr - Σύνδεση | Εγγραφή | Βοήθεια
σε

 

Αρχική σελίδα Ιστολόγια Συζητήσεις Εκθέσεις Φωτογραφιών Αρχειοθήκες

Adding user controls dynamically problem!

Îåêßíçóå áðü ôï ìÝëïò Letmefly. Τελευταία δημοσίευση από το μέλος Letmefly στις 20-04-2007, 01:53. Υπάρχουν 1 απαντήσεις.
Ταξινόμηση Δημοσιεύσεων: Προηγούμενο Επόμενο
  •  18-04-2007, 21:02 30197

    Adding user controls dynamically problem!

    Hello,
     i ve created a user control that i want to add dynamically to my website when a Linkbutton is pressed
    (ex. if u press the linkbutton twice  the user control  appears twice and holds its view state).

    The user control consists of
    two labels , a  label1 showing the number of times the control was added and a label2 representing text,

    a textbox for entering information

    and a linkbutton that changes label2 text when pressed.



    In the default.aspx page i have created a way  to track when a user control has been added so that it can be re-added to theweb page as additional postback occurs.

    Here is how i implement it :
    When a user control is added ,i add it to an arraylist (which contains all the previous user controls ), and this array list is then added to the session.
    So when the page is loaded , i retrieve that arraylist back from the session and i enter to the page all the previous added user controls..

    All works fine except one thing..

    When i press the Linkbutton of the user control to call the Change_Text method (and change the text of the label)
     , the page postbacks but the method  doesnt execute at all and i cant really figure out what is goin on....

    Any ideas?




    Below i give the code

    *******************Create_Answer.ascx   code*******************

    <%@ Control Language="C#" AutoEventWireup="true"
    CodeFile="Create_Answer.ascx.cs" Inherits="UserControl_Create_Answer" %>

    <br /> <br />

    <table>
      <tr>
       <td colspan="4"><asp:Label ID="lbl_AnswerCount" runat="server" Text="answer"  Font-Overline="False" ForeColor="SlateBlue"></asp:Label></td>   
      </tr>
         
      <tr>
         
        <td>Answer Title</td>
        <td>
            <asp:TextBox ID="TextBox3" runat="server" Columns="25" Rows="2" TextMode="MultiLine"></asp:TextBox></td>
        
        </tr>
       
       <tr>
        <td colspan="4">
            <asp:LinkButton ID="lnkbtn_ChangeText" runat="server" OnClick="Change_Text" >Change label text</asp:LinkButton>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></td>
       </tr>
       </table>


    <br /> <br />



    *********  Create_Answer.ascx.cs   code*************************************

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class UserControl_Create_Answer : System.Web.UI.UserControl
    {

        private string _text;
        
        

        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }



        protected void Page_Load(object sender, EventArgs e)
        {
            lbl_AnswerCount.Text = Text + " answers ";
            
        }

        protected void Change_Text(object sender, EventArgs e)
        {
            Label1.Text = "Text changed";
        }
    }




    *********************Default.aspx code ******************************************

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    <%@ Register src="Create_Answer.ascx" TagName="Create_Answer" TagPrefix="uc1" %>
    <%@ Reference Control="~/Create_Answer.ascx" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
     

    <asp:LinkButton ID="lnkbtn_NewAnswer" runat="server"  OnClick="lnkbtn_Click" font-size="Small" height="8px" >Insert new answer</asp:LinkButton>

    <asp:Panel ID="Panel_NewAnswer" runat="server" Visible="false">

    <asp:PlaceHolder ID="PlaceHolder1" runat="server">
        
         </asp:PlaceHolder>
        
        </asp:Panel>


        
        </div>
        </form>
    </body>
    </html>




    **********Default.aspx.cs code  *********************




    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class _Default : System.Web.UI.Page
    {


        protected void Page_Load(object sender, EventArgs e)
        {

            // We are using events to add user controls dynamically. Specifically because
            // the events may not be raised each time a page postback occurs , you need to create
            // a way to track when a user control has been added so that it can be re-added to the
            // web page as additional postback occurs.

            // A simple way to do this is to use the ASP.NET session to track when the user control
            // is added to the WebPage


            if (!IsPostBack)
            {
                // Instantiates a new array list that will hold how many Create_Answer user controls have
                // been instantiated

                ArrayList AnswerControlsCreated = new ArrayList();
                Session.Add("AnswerControlsCreated", AnswerControlsCreated);
            }

            //Check to see how many (if any) create_answer controls should be added to the page

            if (Session["AnswerControlsCreated"] != null)
            {

                ArrayList AnswerControlsCreated = (ArrayList)Session["AnswerControlsCreated"];
                foreach (UserControl_Create_Answer uc in AnswerControlsCreated)
                {
                    PlaceHolder1.Controls.Add(uc);
                }
            }


        }

        protected void lnkbtn_Click(object sender, EventArgs e)
        {

                    Panel_NewAnswer.Visible = true;
                    //Inserts a new Create_Answer control to the page..
                    //Petrieves the Array List from the Session
                    // Adds the new Create_Answer control to the  arraylist..
                    // Inserts the Array List back into the session

                    UserControl_Create_Answer c1 = (UserControl_Create_Answer)LoadControl("Create_Answer.ascx");
                    ArrayList AnswerControlsCreated = (ArrayList)Session["AnswerControlsCreated"];
                    c1.Text = (AnswerControlsCreated.Count + 1).ToString(); //Sets the user control's "Number of answers" label text  
                    AnswerControlsCreated.Add(c1);
                    Session.Add("AnswerControlsCreated", AnswerControlsCreated);
                    PlaceHolder1.Controls.Add(c1);


        
        }
    }

    Sorry για τα αγγλικα παιδια αν δω οτι υπαρχουν παραπονα θα κανω την μετάφραση...

  •  20-04-2007, 01:53 30398 σε απάντηση της 30197

    Απ: Adding user controls dynamically problem!

    After some reading i found out that during the postbacks except from adding again the user controls you must also attach again the event handlers methods of your user control back to the user control children controls...

    So ,what i ve done so far in order to solve the problem is , every time i iterate in the arraylist that holds my usercontrols i use

    the FindControl method in order to find the linkbutton of the usercontrol and then assing that linkbutton.click event to the event handler of the linkbutton.

     

     foreach (UserControl_Create_Answer uc in AnswerControlsCreated)
                {
                    ((LinkButton)uc.FindControl("lnkbtn_ChangeText")).Click += new EventHandler(Change_Text);
                  
                }

     With this technique the user controls linkbutton control works as desired but the drawback is that i must remove  the event handler "Change_Text" form the user controls behind code and place it in every page that hosts the user control..

    I cant find a way to attach the LinkButton.Click (Child control's event) back to its event in the UserControls.ascx.cs page...
Προβολή Τροφοδοσίας RSS με μορφή XML
Με χρήση του Community Server (Commercial Edition), από την Telligent Systems