Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Return List<> on button click of user control.

Return List<> on button click of user control.

Scheduled Pinned Locked Moved ASP.NET
questiondesigntutorial
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zeego
    wrote on last edited by
    #1

    Hi I want to return a list of records on button click of user control. I already have the collection ready , but I dont know how to return the collection to the calling page. How do I bind results of user control with gridview on parent page on user control button click ? My user control code

    public partial class assets_WebControls_ftExclusions : System.Web.UI.UserControl
    {
    private int _id;
    private ftExclusionsList myftExclusions;

    public int ID
    {
        get{return \_id;}
        set{\_id=value;}
    }
    
    public ftExclusionsList selectedExclusions
    {
        get { return myftExclusions; }
        set { myftExclusions = value; }
    }
    
    protected void Page\_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            cblExclusions.DataBind();
            ftExclusionsList myExcluList = ftExclusionsManager.ftExclusionsById(ID);
            if (myExcluList != null)
            {
                if (myExcluList.Count &gt; 0 &amp;&amp; myExcluList != null)
                {
                    foreach (ftExclusions ex in myExcluList)
                    {
                        cblExclusions.Items.FindByValue(ex.excluId.ToString()).Selected = true;
                    }
                }
            }
        }
        
    }
    protected void btnUpdate\_Click(object sender, EventArgs e)
    {
        ftExclusionsList myFTExclusionList = new ftExclusionsList();
        foreach (ListItem li in cblExclusions.Items)
        {
            if (li.Selected == true)
            {
                ftExclusions myftExclusion = new ftExclusions();
                myftExclusion.excluId = Convert.ToInt32(li.Value);
                myftExclusion.excludesc = li.Text.ToString();
                myFTExclusionList.Add(myftExclusion);
            }
            
        }
        selectedExclusions = myFTExclusionList;
    

    // how do I return this list and bind it with gridview on calling page?
    }
    }

    B A 2 Replies Last reply
    0
    • Z zeego

      Hi I want to return a list of records on button click of user control. I already have the collection ready , but I dont know how to return the collection to the calling page. How do I bind results of user control with gridview on parent page on user control button click ? My user control code

      public partial class assets_WebControls_ftExclusions : System.Web.UI.UserControl
      {
      private int _id;
      private ftExclusionsList myftExclusions;

      public int ID
      {
          get{return \_id;}
          set{\_id=value;}
      }
      
      public ftExclusionsList selectedExclusions
      {
          get { return myftExclusions; }
          set { myftExclusions = value; }
      }
      
      protected void Page\_Load(object sender, EventArgs e)
      {
          if (!Page.IsPostBack)
          {
              cblExclusions.DataBind();
              ftExclusionsList myExcluList = ftExclusionsManager.ftExclusionsById(ID);
              if (myExcluList != null)
              {
                  if (myExcluList.Count &gt; 0 &amp;&amp; myExcluList != null)
                  {
                      foreach (ftExclusions ex in myExcluList)
                      {
                          cblExclusions.Items.FindByValue(ex.excluId.ToString()).Selected = true;
                      }
                  }
              }
          }
          
      }
      protected void btnUpdate\_Click(object sender, EventArgs e)
      {
          ftExclusionsList myFTExclusionList = new ftExclusionsList();
          foreach (ListItem li in cblExclusions.Items)
          {
              if (li.Selected == true)
              {
                  ftExclusions myftExclusion = new ftExclusions();
                  myftExclusion.excluId = Convert.ToInt32(li.Value);
                  myftExclusion.excludesc = li.Text.ToString();
                  myFTExclusionList.Add(myftExclusion);
              }
              
          }
          selectedExclusions = myFTExclusionList;
      

      // how do I return this list and bind it with gridview on calling page?
      }
      }

      B Offline
      B Offline
      Brij
      wrote on last edited by
      #2

      AFAIK, You can not return something from click event. To achieve your goal, you can have a public property which holds the list of records. Assign this property with the list in click event. And after this, you can access this property from the page to get the list of records.

      Cheers!! Brij Check my latest Article :URL Routing with ASP.NET 4.0

      Z 1 Reply Last reply
      0
      • B Brij

        AFAIK, You can not return something from click event. To achieve your goal, you can have a public property which holds the list of records. Assign this property with the list in click event. And after this, you can access this property from the page to get the list of records.

        Cheers!! Brij Check my latest Article :URL Routing with ASP.NET 4.0

        Z Offline
        Z Offline
        zeego
        wrote on last edited by
        #3

        Thanks for the reply. How about I define a private variable gridview inside my user control.Pass the value of parent page gridview to this private variable of user control. And on button click of user control, bind the private gridview with collection ? I cant get it to work though but just a question.

        M 1 Reply Last reply
        0
        • Z zeego

          Thanks for the reply. How about I define a private variable gridview inside my user control.Pass the value of parent page gridview to this private variable of user control. And on button click of user control, bind the private gridview with collection ? I cant get it to work though but just a question.

          M Offline
          M Offline
          michaelschmitt
          wrote on last edited by
          #4

          Hi, if i were you, i would not start passing controls from one page or usercontrol to another. The method explained in the above post is the way to go. Always keep in mind that your controls/classes should have a (clean seperated) job. Maybe you wanna reuse this control somewhere else where the calling page doesnt have/need a gridview? keep your "interfaces" as simple as possible. So, in your UserControls' Button click event, raise an event or something, which is subscribed in Page load of your Page class. This ways, your page can access the mentioned public property of your usercontrol once the event occurs. Or you could even pass your "result" though this event/delegate and access it without the necessity of a public/internal property.

          Z 1 Reply Last reply
          0
          • M michaelschmitt

            Hi, if i were you, i would not start passing controls from one page or usercontrol to another. The method explained in the above post is the way to go. Always keep in mind that your controls/classes should have a (clean seperated) job. Maybe you wanna reuse this control somewhere else where the calling page doesnt have/need a gridview? keep your "interfaces" as simple as possible. So, in your UserControls' Button click event, raise an event or something, which is subscribed in Page load of your Page class. This ways, your page can access the mentioned public property of your usercontrol once the event occurs. Or you could even pass your "result" though this event/delegate and access it without the necessity of a public/internal property.

            Z Offline
            Z Offline
            zeego
            wrote on last edited by
            #5

            Hello Michael. Thanks for the reply. Actually I have to show hide the user control on button clicks on page. That is why there is a update button on the user control, which when clicked would update the gridview on parent page and hide the control. If I were to store the values in the checkbox list click event I still have to pass them on to the parent gridview. Can you elaborate a little with some example or hint what you want me to do ?

            M 1 Reply Last reply
            0
            • Z zeego

              Hello Michael. Thanks for the reply. Actually I have to show hide the user control on button clicks on page. That is why there is a update button on the user control, which when clicked would update the gridview on parent page and hide the control. If I were to store the values in the checkbox list click event I still have to pass them on to the parent gridview. Can you elaborate a little with some example or hint what you want me to do ?

              M Offline
              M Offline
              michaelschmitt
              wrote on last edited by
              #6

              Hi, i was talking about the button click event in your usercontrol. You have to somehow let the page know that the user clicked the button within your usercontrol. You could do this by using a delegate similar to this. I made a comment there with an example. You have to replace the text-parameter by your custom item collection. This would work. (Or you just notify the page and the page accesses the items though a public property of your control). Does this help or am i confusing?

              Z 1 Reply Last reply
              0
              • M michaelschmitt

                Hi, i was talking about the button click event in your usercontrol. You have to somehow let the page know that the user clicked the button within your usercontrol. You could do this by using a delegate similar to this. I made a comment there with an example. You have to replace the text-parameter by your custom item collection. This would work. (Or you just notify the page and the page accesses the items though a public property of your control). Does this help or am i confusing?

                Z Offline
                Z Offline
                zeego
                wrote on last edited by
                #7

                Hi thanks for the reply, Ermm its not confusing but maybe too hitech for me. I took the update button out of user control and made its method as a private method inside user control , I am trying to expose the updated collection though a public property of user control now. Only thing bothering me is that I cant call the private method inside user control and populate the collection on call of the public method of user control. In presentation page I am calling the the public attribute but it is returning null right now because the private method is not called.

                public partial class assets_WebControls_ftExclusions : System.Web.UI.UserControl
                {
                private int _id;
                private ftExclusionsList _myftExclusions;

                public int ID
                {
                    get{return \_id;}
                    set{\_id=value;}
                }
                
                public ftExclusionsList selectedExclusions
                {
                    get { return \_myftExclusions; }
                    set { \_myftExclusions = value; }
                }
                
                protected void Page\_Load(object sender, EventArgs e)
                {
                    if (!Page.IsPostBack)
                    {
                        cblExclusions.DataBind();
                        ftExclusionsList myExcluList = ftExclusionsManager.ftExclusionsById(ID);
                        if (myExcluList != null)
                        {
                            if (myExcluList.Count > 0 && myExcluList != null)
                            {
                                foreach (ftExclusions ex in myExcluList)
                                {
                                    cblExclusions.Items.FindByValue(ex.excluId.ToString()).Selected = true;
                                }
                            }
                        }
                    }
                    
                }
                
                private void SelectedExclusions()
                {
                    ftExclusionsList myFTExclusionList = new ftExclusionsList();
                    foreach (ListItem li in cblExclusions.Items)
                    {
                        if (li.Selected == true)
                        {
                            ftExclusions myftExclusion = new ftExclusions();
                            myftExclusion.excluId = Convert.ToInt32(li.Value);
                            myftExclusion.excludesc = li.Text.ToString();
                            myFTExclusionList.Add(myftExclusion);
                        }
                     }
                     selectedExclusions = myFTExclusionList;    
                }
                

                }

                M 1 Reply Last reply
                0
                • Z zeego

                  Hi I want to return a list of records on button click of user control. I already have the collection ready , but I dont know how to return the collection to the calling page. How do I bind results of user control with gridview on parent page on user control button click ? My user control code

                  public partial class assets_WebControls_ftExclusions : System.Web.UI.UserControl
                  {
                  private int _id;
                  private ftExclusionsList myftExclusions;

                  public int ID
                  {
                      get{return \_id;}
                      set{\_id=value;}
                  }
                  
                  public ftExclusionsList selectedExclusions
                  {
                      get { return myftExclusions; }
                      set { myftExclusions = value; }
                  }
                  
                  protected void Page\_Load(object sender, EventArgs e)
                  {
                      if (!Page.IsPostBack)
                      {
                          cblExclusions.DataBind();
                          ftExclusionsList myExcluList = ftExclusionsManager.ftExclusionsById(ID);
                          if (myExcluList != null)
                          {
                              if (myExcluList.Count &gt; 0 &amp;&amp; myExcluList != null)
                              {
                                  foreach (ftExclusions ex in myExcluList)
                                  {
                                      cblExclusions.Items.FindByValue(ex.excluId.ToString()).Selected = true;
                                  }
                              }
                          }
                      }
                      
                  }
                  protected void btnUpdate\_Click(object sender, EventArgs e)
                  {
                      ftExclusionsList myFTExclusionList = new ftExclusionsList();
                      foreach (ListItem li in cblExclusions.Items)
                      {
                          if (li.Selected == true)
                          {
                              ftExclusions myftExclusion = new ftExclusions();
                              myftExclusion.excluId = Convert.ToInt32(li.Value);
                              myftExclusion.excludesc = li.Text.ToString();
                              myFTExclusionList.Add(myftExclusion);
                          }
                          
                      }
                      selectedExclusions = myFTExclusionList;
                  

                  // how do I return this list and bind it with gridview on calling page?
                  }
                  }

                  A Offline
                  A Offline
                  Andreas X
                  wrote on last edited by
                  #8

                  i would create an interface that contains the method to update the page gridview.

                  public interface IMyInterface
                  {
                  public void UpdateGridview(ftExclusionsList theList);
                  }

                  implement the interface on the page.

                  protected void btnUpdate_Click(object sender, EventArgs e)
                  {
                  ftExclusionsList myFTExclusionList = new ftExclusionsList();
                  foreach (ListItem li in cblExclusions.Items)
                  {
                  if (li.Selected == true)
                  {
                  ftExclusions myftExclusion = new ftExclusions();
                  myftExclusion.excluId = Convert.ToInt32(li.Value);
                  myftExclusion.excludesc = li.Text.ToString();
                  myFTExclusionList.Add(myftExclusion);
                  }

                      }
                      selectedExclusions = myFTExclusionList;
                      // how do I return this list and bind it with gridview on calling page?
                      ((IMyInterface)this.Page).UpdateGridview(selectedExclusions);
                  }
                  

                  this is the way i do it and i works like a charm :)

                  Andreas Johansson
                  IT Professional at Office IT Partner i Norrbotten Sweden
                  What we don't know. We learn.
                  What you don't know. We teach

                  1 Reply Last reply
                  0
                  • Z zeego

                    Hi thanks for the reply, Ermm its not confusing but maybe too hitech for me. I took the update button out of user control and made its method as a private method inside user control , I am trying to expose the updated collection though a public property of user control now. Only thing bothering me is that I cant call the private method inside user control and populate the collection on call of the public method of user control. In presentation page I am calling the the public attribute but it is returning null right now because the private method is not called.

                    public partial class assets_WebControls_ftExclusions : System.Web.UI.UserControl
                    {
                    private int _id;
                    private ftExclusionsList _myftExclusions;

                    public int ID
                    {
                        get{return \_id;}
                        set{\_id=value;}
                    }
                    
                    public ftExclusionsList selectedExclusions
                    {
                        get { return \_myftExclusions; }
                        set { \_myftExclusions = value; }
                    }
                    
                    protected void Page\_Load(object sender, EventArgs e)
                    {
                        if (!Page.IsPostBack)
                        {
                            cblExclusions.DataBind();
                            ftExclusionsList myExcluList = ftExclusionsManager.ftExclusionsById(ID);
                            if (myExcluList != null)
                            {
                                if (myExcluList.Count > 0 && myExcluList != null)
                                {
                                    foreach (ftExclusions ex in myExcluList)
                                    {
                                        cblExclusions.Items.FindByValue(ex.excluId.ToString()).Selected = true;
                                    }
                                }
                            }
                        }
                        
                    }
                    
                    private void SelectedExclusions()
                    {
                        ftExclusionsList myFTExclusionList = new ftExclusionsList();
                        foreach (ListItem li in cblExclusions.Items)
                        {
                            if (li.Selected == true)
                            {
                                ftExclusions myftExclusion = new ftExclusions();
                                myftExclusion.excluId = Convert.ToInt32(li.Value);
                                myftExclusion.excludesc = li.Text.ToString();
                                myFTExclusionList.Add(myftExclusion);
                            }
                         }
                         selectedExclusions = myFTExclusionList;    
                    }
                    

                    }

                    M Offline
                    M Offline
                    michaelschmitt
                    wrote on last edited by
                    #9

                    If it is just about the collection you want to expose now, you can either do this:

                    public ftExclusions GetSelectedExclusions()
                    {
                    ftExclusionsList myFTExclusionList = new ftExclusionsList();

                    foreach (ListItem li in cblExclusions.Items)
                    {
                    if (li.Selected == true)
                    {
                    ftExclusions myftExclusion = new ftExclusions();
                    myftExclusion.excluId = Convert.ToInt32(li.Value);
                    myftExclusion.excludesc = li.Text.ToString();
                    myFTExclusionList.Add(myftExclusion);
                    }
                    }
                    return myFTExclusionList;
                    }

                    or this (property):

                    public ftExclusionsList SelectedExclusions
                    {
                    get { return GetSelectedExclusions(); }
                    }

                    private ftExclusions GetSelectedExclusions()
                    {
                    ftExclusionsList myFTExclusionList = new ftExclusionsList();

                    foreach (ListItem li in cblExclusions.Items)
                    {
                    if (li.Selected == true)
                    {
                    ftExclusions myftExclusion = new ftExclusions();
                    myftExclusion.excluId = Convert.ToInt32(li.Value);
                    myftExclusion.excludesc = li.Text.ToString();
                    myFTExclusionList.Add(myftExclusion);
                    }
                    }
                    return myFTExclusionList;
                    }

                    Then use the property OR method in the desired event of your page. This is all assuming that the rest of the code works. A hint: Check for null before checking the count of a collection. Otherwise, you could get an exception. It is also usefull to check like

                    if (null==something)

                    // instead of
                    if (something == null)

                    This way, you can prevent typing mistakes like If (something = null)... GL GL

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups