Tutorial RSS
 
DB Tutorials Server Intellect Cloud Hosting
Navigator: Home - Retrieve - Creating User Controls in C# Part 2

Creating User Controls in C# Part 2

In Part 1 of the User Control We covered building a user control but it is very basic and really doesn’t work well as a general-purpose control. In part two of this tutorial we will modify the control by allowing the user to specify, retrieve, display and clear the list of items in the target list quickly. Accomplishing this requires that the communication between the host page and the user control is set to share values, read properties, and issue commands.




We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.




What you will need for this tutorial


  • You will need the source file from the first tutorial, if you are just completed the first tutorial you can use the project you just created as we will be building onto it.
  • Microsoft Visual Studio 2010 or Microsoft Visual Web Developer 2010 Express
  • A computer with an internet connection and Microsoft Windows 7 operating system
  • A basic understanding of the web and some programing experience would be helpful

Topics that will be covered


  • Create and add user controls to the host page

Download the Tutorial 1 project source to complete this tutorial

Download the finished project source for both tutorials



Adding user controls to the host page


  1. Open the project you just created from "creating user controls Part 1". If you did not do the first tutorial, you can download a copy of the completed project above.

  2. Open the ListPicker.cs code behind file and copy and paste the following code at the bottom:

  3. protected void AddAll_Click(object sender, EventArgs e)
    public ListItemCollection SelectedItems
    {
    get { return TargetList.Items ; }
    }


    This code will retrieve the values in the TargetList list.


  4. Insert the following code next:

  5. public Boolean AllowDuplicates
    {
    get
    {
    return (Boolean)ViewState["allowDuplicates"];
    }
    set
    {
    ViewState["allowDuplicates"] = value;
    }
    }


    This code will create the AllowDuplicates property which specifies weather or not you want to allow duplicate values in the TargetList list optionally.


  6. Go to the AddItem method we wrote earlier in part 2 of the tutorial (ListPicker.aspx) and copy and paste the following code over it:

  7. protected void AddItem(ListItem li)
    {
    TargetList.SelectedIndex = -1;
    if (this.AllowDuplicates == true)
    {
    TargetList.Items.Add(li);
    }
    else
    {
    if (TargetList.Items.FindByText(li.Text) == null)
    {
    TargetList.Items.Add(li);
    }
    }
    }


    The function being performed is the same as before however the code has been modified to check for the AllowDuplicate property.


  8. Now add a method to the user control by copying and pasting the following code at the bottom of the page:

  9. public void AddSourceItem(String sourceItem)
    {
    SourceList.Items.Add(sourceItem);
    }


  10. Add a final method using the following code:

  11. public void ClearAll()
    {
    SourceList.Items.Clear();
    TargetList.Items.Clear();
    }


  12. We will now remove the test data from the SourceList list, because we will be entering it manually using the interface. Switch to the design view in the ListPicker.ascx file and select the items property in the properties panel.

  13. Click on the ellipsis (…) button. When the ListItem Collection Editor appears, click the remove button for each sample item (A, B, and C) then click OK.

  14. In the file menu, click Save All to save your work so far.




Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.




Adding code to the host page to support the user control


Our final goal is to create some additional user controls on the host page so we can share the values with the user control page.

  1. Open the HostUserControl.aspx page, select source view and set the allow duplicates declaratively as shown below:

  2. User Controls Part 2 Snapshot
  3. Make sure you are on the HostUserControl.aspx page and switch to design view.

  4. From the Standard group in the toolbox, click and drag the text box under the SourceList on the page. The ID of the TextBox is NewItem. Leave the Text Field Empty.

  5. Click and drag a button from the standard group underneath the TextBox. In the properties panel name the ID AddItem and name the text Add Item.

  6. Drag a second button form the standard box under the previous button. In the properties panel name the ID LoadFiles and the Text File List.

  7. Drag a third button from the standard list under the second button. In the properties panel name the ID ClearSelection and the text Clear All.

  8. Click on a checkbox in the standard group and drag it under the third button. In the properties panel be sure to set the AutoPostBack to True, the Checked to True, the ID to AllowDuplicates and the text to Allow Duplicates.

  9. Click on a final button from the standard group and drag it under the checkbox in the design view. In the properties panel name the ID ShowSelection and the Text Show Selection.

  10. Lastly, click on a Label in the standard box and drag it under the button. In the properties panel name the ID Selection and leave the text field empty.

  11. When you are finished the design view should look like the image below:


        User Controls Part 2 Snapshot

    Now all we have left to do is create the event handlers for the controls we just built in the code behind.


  12. Double click the allow duplicates item in design view to go to the code behind page (HostUserControl.aspx.cs) and then copy and paste the following code:

  13. protected void AllowDuplicates_CheckedChanged(Object sender, EventArgs e)
    {
    ListPicker1.AllowDuplicates = AllowDuplicates.Checked;
    }


    This will activate the check box to allow or deny duplicate entries.


  14. Switch back to design view (HostUserControl.aspx) and double click on the AddItem button then copy and paste the following code:

  15. protected void AddItem_Click(object sender, EventArgs e)
    {
    ListPicker1.AddSourceItem(Server.HtmlEncode(NewItem.Text));
    }


    This code will create a ListItemCollection collection in code and will populate the SourceList with sample data.


  16. Go to design view again and double click the LoadFiles (File List) button, then copy and paste the following code:

  17. protected void LoadFiles_Click(object sender, EventArgs e)
    {
    String path = Server.MapPath(Request.ApplicationPath);
    String[] files = System.IO.Directory.GetFiles(path);
    foreach(String filename in files)
    {
    ListPicker1.AddSourceItem(filename);
    }
    }


    This code is similar to the code for the AddItem, only this code adds a list of files in the website root directory.


  18. Switch to design view and double click the ShowSelection button, then copy and paste the following code:

  19. protected void ShowSelection_Click(object sender, EventArgs e)
    {
    String selectedItemsString = "";
    foreach(ListItem lItem in ListPicker1.SelectedItems)
    {
    selectedItemsString += "
    " + lItem.Text;
    }
    Selection.Text = selectedItemsString;
    }


    This code will display the results of the selection stored in the TargetList list.


  20. Switch to Design view one last time and double click the ClearSelection button, then copy and paste the following code:

  21. protected void ClearSelection_Click(object sender, EventArgs e)
    {
    ListPicker1.ClearAll();
    }


    This code will activate the ClearAll method which will remove all items from the SourceList list and the TargetList list.


    That wraps it all up. Now you can test your finished user control.





    We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!