<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>Gabriel Bog&#233;a Perez - Home</title>
  <id>tag:www.gbogea.com,2008:mephisto/</id>
  <generator uri="http://mephistoblog.com" version="0.7.3">Mephisto Noh-Varr</generator>
  <link href="http://www.gbogea.com/feed/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="http://www.gbogea.com/" rel="alternate" type="text/html"/>
  <updated>2008-07-22T12:17:55Z</updated>
  <entry xml:base="http://www.gbogea.com/">
    <author>
      <name>gabriel</name>
    </author>
    <id>tag:www.gbogea.com,2008-07-22:29</id>
    <published>2008-07-22T00:59:00Z</published>
    <updated>2008-07-22T12:17:55Z</updated>
    <category term="Tips and Tutorials"/>
    <link href="http://www.gbogea.com/2008/7/22/add-controls-dynamically-part-4" rel="alternate" type="text/html"/>
    <title>Add Controls Dynamically - Part 4</title>
<content type="html">
            &lt;p&gt;The part 3 of this series was getting a bit long for my taste so I decided to break it in 2. Here we&#8217;ll continue dealing with the controls added dynamically to our page only now we&#8217;re going to access them by ID.&lt;/p&gt;


Related posts:
	&lt;ul&gt;
	&lt;li&gt;&lt;a href=&quot;http://www.gbogea.com/2008/7/6/add-controls-dynamically&quot;&gt;Add Controls Dynamically &#8211; Part 1&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href=&quot;http://www.gbogea.com/2008/7/14/add-controls-dynamically-part-2&quot;&gt;Add Controls Dynamically &#8211; Part 2&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href=&quot;http://www.gbogea.com/2008/7/22/add-controls-dynamically-part-3&quot;&gt;Add Controls Dynamically &#8211; Part 3&lt;/a&gt;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;h2&gt;Naming the controls&lt;/h2&gt;


	&lt;p&gt;By now you have noticed that we haven&#8217;t explicitly set the ID of any of our controls. Of course the controls have ID&#8217;s but they are defined automatically by the Asp.Net framework when the controls are instantiated. The reason this works is because the framework will create the names consistently the same, again and again as long as you create the controls always in the same order (we are doing that).&lt;/p&gt;


	&lt;p&gt;It is this automatically naming feature that allows our components to keep their state between requests. When the page is submitted all of the controls states are sent back from the browser to the server, but the server doesn&#8217;t know how to create the controls that&#8217;s why we had to do it every time the page is submitted. After the control has been created the Asp.Net framework is able to get it&#8217;s state from the page based on it&#8217;s name.&lt;/p&gt;


	&lt;p&gt;Ok, enough theory.  Let&#8217;s give our controls and ID manually. Since the controls are added dynamically the ID&#8217;s have to be something that can be predicted. Let&#8217;s say: TextBox + a number. For this will change the &lt;i&gt;createDynamicControls&lt;/i&gt; method to add a parameter which will be the number of the control added.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
    //this method takes care of creating the controls
    private void createDynamicControls(int count)
    {
        TextBox tb = new TextBox();
        tb.TextChanged += TextBox_TextChanged;
        //now we set the control ID manually
        tb.ID = &quot;TextBox&quot; + count;
        PlaceHolder1.Controls.Add(tb);
        controlsList.Add(tb);
    }
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;When the add control button is clicked we need to pass the number to the method. We can use the control count that we&#8217;re storing in the ViewState.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
    protected void Button1_Click(object sender, EventArgs e)
    {
        createDynamicControls((int)ViewState[&quot;count&quot;]);
        //increment the number of controls
        ViewState[&quot;count&quot;] = (int)ViewState[&quot;count&quot;] + 1;
    }
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Another place we need to change is the Page_Load event when the controls are re-created. Here for loop when we pass in the new parameter:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
            for (int i = 0; i &amp;lt; controlCount; i++)
            {
                //notice that now we pass the i as parameter
                createDynamicControls(i);
            }
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;To test our new functionality we will add TextBox, a Button and a Label. We are going to inform the ID of the TextBox we want to get the value for. When the button is clicked the event will get the control and display it&#8217;s value in the label.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
        &amp;lt;asp:TextBox ID=&quot;TextBoxControlId&quot; runat=&quot;server&quot;&amp;gt;&amp;lt;/asp:TextBox&amp;gt;
        &amp;lt;asp:button ID=&quot;ButtonGetValue&quot; runat=&quot;server&quot; text=&quot;Get value&quot; 
            onclick=&quot;ButtonGetValue_Click&quot; /&amp;gt;
        Valor: &amp;lt;asp:Label ID=&quot;LabelValue&quot; runat=&quot;server&quot; Text=&quot;&quot;&amp;gt;&amp;lt;/asp:Label&amp;gt;
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;And here is the event where we get the TextBox we want:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
    protected void ButtonGetValue_Click(object sender, EventArgs e)
    {
        //find the control we want using the name informed by the user in the 
        //TextBoxControlId control
        TextBox tb = (TextBox)PlaceHolder1.FindControl(TextBoxControlId.Text);
        LabelValue.Text = tb.Text;
    }
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Now if you add 3 TextBoxes (which will have id&#8217;s TextBox0, TextBox1 and TextBox2) and you input TextBox0 in the interface and hit the button the LabelValue control will show the value in the first TextBox.&lt;/p&gt;


	&lt;p&gt;This one was a bit longer than I would like to so I&#8217;ll try to make the next article shorter.&lt;/p&gt;


	&lt;p&gt;Here is the final version of our page after all of our modifications:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
&amp;lt;%@ Page Language=&quot;C#&quot; %&amp;gt;

&amp;lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&amp;gt;

&amp;lt;script runat=&quot;server&quot;&amp;gt;

    System.Collections.Generic.List&amp;lt;TextBox&amp;gt; controlsList = new System.Collections.Generic.List&amp;lt;TextBox&amp;gt;();

    void Page_Load(object sender, EventArgs e)
    {
        //when the user first enters the page set the count as zero
        if (!IsPostBack)
        {
            ViewState[&quot;count&quot;] = 0;
        }
        //on every subsequent postback, check the control count
        //and recreated all the controls
        else
        {
            int controlCount = (int)ViewState[&quot;count&quot;];
            for (int i = 0; i &amp;lt; controlCount; i++)
            {
                //notice that now we pass the i as parameter
                createDynamicControls(i);
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        createDynamicControls((int)ViewState[&quot;count&quot;]);
        //increment the number of controls
        ViewState[&quot;count&quot;] = (int)ViewState[&quot;count&quot;] + 1;
    }

    //this method takes care of creating the controls
    private void createDynamicControls(int count)
    {
        TextBox tb = new TextBox();
        tb.TextChanged += TextBox_TextChanged;
        //now we set the control ID manually
        tb.ID = &quot;TextBox&quot; + count;
        PlaceHolder1.Controls.Add(tb);
        controlsList.Add(tb);
    }

    private void TextBox_TextChanged(object sender, EventArgs e)
    {
        //the sender is the control that fired the event
        //that is the control we want to change the color
        TextBox tb = (TextBox)sender;
        tb.BackColor = System.Drawing.Color.Yellow ;
    }

    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        int value = 0;
        foreach (TextBox tb in controlsList)
        {
            value += int.Parse(tb.Text);
        }
        LabelTotal.Text = value.ToString();
    }

    protected void ButtonGetValue_Click(object sender, EventArgs e)
    {
        //find the control we want using the name informed by the user in the 
        //TextBoxControlId control
        TextBox tb = (TextBox)PlaceHolder1.FindControl(TextBoxControlId.Text);
        LabelValue.Text = tb.Text;
    }
&amp;lt;/script&amp;gt;

&amp;lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&amp;gt;
&amp;lt;head id=&quot;Head1&quot; runat=&quot;server&quot;&amp;gt;
    &amp;lt;title&amp;gt;Untitled Page&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;form id=&quot;form1&quot; runat=&quot;server&quot;&amp;gt;
    &amp;lt;div&amp;gt;
        &amp;lt;asp:PlaceHolder ID=&quot;PlaceHolder1&quot; runat=&quot;server&quot;&amp;gt;&amp;lt;/asp:PlaceHolder&amp;gt;
        &amp;lt;br /&amp;gt;
        &amp;lt;asp:Button ID=&quot;Button1&quot; runat=&quot;server&quot; Text=&quot;Add TextBox&quot; 
            onclick=&quot;Button1_Click&quot; /&amp;gt;
        &amp;lt;br /&amp;gt;
        &amp;lt;asp:Button ID=&quot;ButtonAdd&quot; runat=&quot;server&quot; Text=&quot;Add all values&quot; 
            onclick=&quot;ButtonAdd_Click&quot; /&amp;gt;
        &amp;lt;br /&amp;gt;
        Total:&amp;lt;asp:Label ID=&quot;LabelTotal&quot; runat=&quot;server&quot; Text=&quot;&quot;&amp;gt;&amp;lt;/asp:Label&amp;gt;
        &amp;lt;br /&amp;gt;
        &amp;lt;br /&amp;gt;
        &amp;lt;asp:TextBox ID=&quot;TextBoxControlId&quot; runat=&quot;server&quot;&amp;gt;&amp;lt;/asp:TextBox&amp;gt;
        &amp;lt;asp:button ID=&quot;ButtonGetValue&quot; runat=&quot;server&quot; text=&quot;Get value&quot; 
            onclick=&quot;ButtonGetValue_Click&quot; /&amp;gt;
        Value: &amp;lt;asp:Label ID=&quot;LabelValue&quot; runat=&quot;server&quot; Text=&quot;&quot;&amp;gt;&amp;lt;/asp:Label&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;/form&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;See you next time.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.gbogea.com/">
    <author>
      <name>gabriel</name>
    </author>
    <id>tag:www.gbogea.com,2008-07-22:28</id>
    <published>2008-07-22T00:55:00Z</published>
    <updated>2008-07-22T01:10:12Z</updated>
    <category term="Tips and Tutorials"/>
    <link href="http://www.gbogea.com/2008/7/22/add-controls-dynamically-part-3" rel="alternate" type="text/html"/>
    <title>Add Controls Dynamically - Part 3</title>
<content type="html">
            &lt;p&gt;On the last article of this series we saw how we could assign event handlers to dynamically created controls and how the &lt;a href=&quot;http://www.google.com.br/url?sa=t&#38;ct=res&#38;cd=1&#38;url=http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fsystem.web.ui.webcontrols.placeholder.aspx&#38;ei=1LyESM-HHZyQ8wTtoezkCw&#38;usg=AFQjCNHOxV3c6z18CTHqCAGcYa8_ySR--Q&#38;sig2=Tg1qyS0ac61UNUbmV5q4Wg&quot;&gt;PlaceHolder control&lt;/a&gt;  could help us add the controls in a more organized way.&lt;/p&gt;


Related posts:
	&lt;ul&gt;
	&lt;li&gt;&lt;a href=&quot;http://www.gbogea.com/2008/7/6/add-controls-dynamically&quot;&gt;Add Controls Dynamically &#8211; Part 1&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href=&quot;http://www.gbogea.com/2008/7/14/add-controls-dynamically-part-2&quot;&gt;Add Controls Dynamically &#8211; Part 2&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href=&quot;http://www.gbogea.com/2008/7/22/add-controls-dynamically-part-4&quot;&gt;Add Controls Dynamically &#8211; Part 4&lt;/a&gt;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;In this article I want to show how you can keep a reference of a dynamically created control so that you can use it later somewhere else in your code. For this I&#8217;ll continue using the code created add the end of the &lt;a href=&quot;http://www.gbogea.com/2008/7/14/add-controls-dynamically-part-2&quot;&gt;last article of this series&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Now that you can add as many TextBoxes to the form as you want let&#8217;s say that you want to use them to input numeric values and then add up all the values for display. The first thing then is to decide where do we want to keep the reference to the controls. I chose to use a generic list of TextBox (List&amp;lt;textbox&gt;) but you could have chosen another structure that you feel more comfortable with.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
System.Collections.Generic.List&amp;lt;TextBox&amp;gt; controlsList = new System.Collections.Generic.List&amp;lt;TextBox&amp;gt;();
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Now that the list is declared and created the next step is deciding when we are going to add the controls to the list. To me using the method that adds the controls dynamically feels like the best choice, maybe someone has another idea (please share if you do). So the method would look like this:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
    private void createDynamicControls()
    {
        TextBox tb = new TextBox();
        tb.TextChanged += TextBox_TextChanged;
        PlaceHolder1.Controls.Add(tb);
        controlsList.Add(tb);
    }
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Easy, right? Ok, the last step now is adding a button to the form that will call the logic to add the values within all the controls and add a label that will display the result of the addition. The logic for adding the values is very simple, once you have a list with all the controls you only need to iterate through the list, get the value of the control and add it to a variable. Here is the code:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        int value = 0;
        foreach (TextBox tb in controlsList)
        {
            value += int.Parse(tb.Text);
        }
        LabelTotal.Text = value.ToString();
    }
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Please notice that I have assumed that all the TextBoxes are provided with valid numeric values. We could improve the code to check for empty values and conversion errors however this is not the main goal of this article. Here is a complete version of the page after all our changes were made:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
&amp;lt;%@ Page Language=&quot;C#&quot; %&amp;gt;

&amp;lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&amp;gt;

&amp;lt;script runat=&quot;server&quot;&amp;gt;

    System.Collections.Generic.List&amp;lt;TextBox&amp;gt; controlsList = new System.Collections.Generic.List&amp;lt;TextBox&amp;gt;();

    void Page_Load(object sender, EventArgs e)
    {
        //when the user first enters the page set the count as zero
        if (!IsPostBack)
        {
            ViewState[&quot;count&quot;] = 0;
        }
        //on every subsequent postback, check the control count
        //and recreated all the controls
        else
        {
            int controlCount = (int)ViewState[&quot;count&quot;];
            for (int i = 0; i &amp;lt; controlCount; i++)
            {
                createDynamicControls();
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        createDynamicControls();
        //increment the number of controls
        ViewState[&quot;count&quot;] = (int)ViewState[&quot;count&quot;] + 1;        
    }

    //this method takes care of creating the controls

    private void createDynamicControls()
    {
        TextBox tb = new TextBox();
        tb.TextChanged += TextBox_TextChanged;
        PlaceHolder1.Controls.Add(tb);
        controlsList.Add(tb);
    }

    private void TextBox_TextChanged(object sender, EventArgs e)
    {
        //the sender is the control that fired the event
        //that is the control we want to change the color
        TextBox tb = (TextBox)sender;
        tb.BackColor = System.Drawing.Color.Yellow ;
    }

    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        int value = 0;
        foreach (TextBox tb in controlsList)
        {
            value += int.Parse(tb.Text);
        }
        LabelTotal.Text = value.ToString();
    }
&amp;lt;/script&amp;gt;

&amp;lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&amp;gt;
&amp;lt;head id=&quot;Head1&quot; runat=&quot;server&quot;&amp;gt;
    &amp;lt;title&amp;gt;Untitled Page&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;form id=&quot;form1&quot; runat=&quot;server&quot;&amp;gt;
    &amp;lt;div&amp;gt;
        &amp;lt;asp:PlaceHolder ID=&quot;PlaceHolder1&quot; runat=&quot;server&quot;&amp;gt;&amp;lt;/asp:PlaceHolder&amp;gt;
        &amp;lt;br /&amp;gt;
        &amp;lt;asp:Button ID=&quot;Button1&quot; runat=&quot;server&quot; Text=&quot;Add TextBox&quot; 
            onclick=&quot;Button1_Click&quot; /&amp;gt;
        &amp;lt;br /&amp;gt;
        &amp;lt;asp:Button ID=&quot;ButtonAdd&quot; runat=&quot;server&quot; Text=&quot;Add all values&quot; 
            onclick=&quot;ButtonAdd_Click&quot; /&amp;gt;
        &amp;lt;br /&amp;gt;
        Total:&amp;lt;asp:Label ID=&quot;LabelTotal&quot; runat=&quot;server&quot; Text=&quot;&quot;&amp;gt;&amp;lt;/asp:Label&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;/form&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;
&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://www.gbogea.com/">
    <author>
      <name>gabriel</name>
    </author>
    <id>tag:www.gbogea.com,2008-07-19:27</id>
    <published>2008-07-19T13:00:00Z</published>
    <updated>2008-07-19T13:01:21Z</updated>
    <category term="Tips and Tutorials"/>
    <link href="http://www.gbogea.com/2008/7/19/visual-studio-2008-webforms-designer-annoyingly-slow" rel="alternate" type="text/html"/>
    <title>Visual Studio 2008: WebForms Designer Annoyingly Slow</title>
<content type="html">
            &lt;p&gt;Lately one of the developers of my team had been complaining that the WebForms designer was very slow. Editing properties of controls was taking forever and switch from code view to design view was a test to his patience. He was the only one in the team having this problem but it didn&#8217;t take very long for me to find out that Microsoft has published a hotfix to deal with this issue among others.&lt;/p&gt;


	&lt;p&gt;If your Visual Studio is really slow take a look at the &lt;a href=&quot;http://blogs.msdn.com/webdevtools/archive/2008/02/09/downloadable-hotfix-performance-and-editor-fixes-for-microsoft-visual-studio-2008-and-visual-web-developer-express-2008.aspx&quot;&gt;Visual Web Developer Team Blog&lt;/a&gt; for the full list of issues addressed and the download link.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.gbogea.com/">
    <author>
      <name>gabriel</name>
    </author>
    <id>tag:www.gbogea.com,2008-07-16:26</id>
    <published>2008-07-16T15:31:00Z</published>
    <updated>2008-07-16T15:32:26Z</updated>
    <category term="Tips and Tutorials"/>
    <link href="http://www.gbogea.com/2008/7/16/design-pattern-singleton" rel="alternate" type="text/html"/>
    <title>Design Pattern: Singleton</title>
<content type="html">
            &lt;p&gt;This is the first post on a series about Design Patterns using C#. I will not publish all the articles in a sequence but you can expect from time to time to have a new post about some pattern.&lt;/p&gt;


	&lt;p&gt;The first pattern I want to talk about is the Singleton.&lt;/p&gt;


	&lt;h2&gt;Objective&lt;/h2&gt;


	&lt;p&gt;You need a singleton when your application require one and only one instance of a single class and also that this instance can be accessed whenever necessary.&lt;/p&gt;


	&lt;h2&gt;Case Scenario&lt;/h2&gt;


	&lt;p&gt;Imagine that you want a Logger class that will store all messages in a internal list. You need the hold the all the messages in a single place. If you had more than one instance of a Logger class you would have the messages scattered over the various instances. To avoid this will use a singleton.&lt;/p&gt;


	&lt;h2&gt;The Solution&lt;/h2&gt;


	&lt;p&gt;We are going to create a Logger class in our example.&lt;/p&gt;


	&lt;p&gt;The first issue the singleton should handle is how to prevent users from creating multiple instances of a class. To achieve this you have to block (hide) the constructor. This can be done making it private.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
private Logger() { }
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Now the that the constructor is private how can you instantiate the class? You are right, you can&#8217;t do it from outside the class so we are going to do it from the inside. Since we need to keep the one instance we create we need a variable to hold it. We are going to declare this variable as static so that we don&#8217;t need the class to be instantiated in order to keep the value.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
//this will hold our unique instance
private static Logger instance = null;
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;The next step is to come up with a way to create the only instance and at the same time gain Access to that instance. This can be done using a static method that will check the instance variable and if it is null it will create a new instance and return it, if it’s not null it will return the previously create instance.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
        public static Logger Instance()
        {
            //if the instance is null you have to instantiate it
            if (instance == null)
            {
                instance = new Logger();
            }
            return instance;
        }
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;This is all we need for our singleton but we still need to add the functionality for the Logger class. We need a list to store the messages, a method to add new messages and a method to print all the messages already stored. Here is the code for it:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
        //holds all the messages
        private List&amp;lt;string&amp;gt; messages = new List&amp;lt;string&amp;gt;();

        //add a new message to the Logger
        public void Log(string message)
        {
            messages.Add(message);
        }

        public void PrintAllMessages()
        {
            foreach (string message in messages)
            {
                Console.WriteLine(message);
            }
        }
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Here is the code for the whole class:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
    public class Logger
    {
        private static Logger instance = null;

        private Logger() { }

        public static Logger Instance()
        {
            //if the instance is null you have to instantiate it
            if (instance == null)
            {
                instance = new Logger();
            }
            return instance;
        }

        //holds all the messages
        private List&amp;lt;string&amp;gt; messages = new List&amp;lt;string&amp;gt;();

        //add a new message to the Logger
        public void Log(string message)
        {
            messages.Add(message);
        }

        public void PrintAllMessages()
        {
            foreach (string message in messages)
            {
                Console.WriteLine(message);
            }
        }
    }
&lt;/code&gt;
&lt;/pre&gt;

	&lt;h2&gt;How to use it&lt;/h2&gt;


	&lt;p&gt;Using the singleton is easy, the only difference from normal class use is that when you need an instance of the object you will not be able to call the constructor (it’s private now) so you need to request an instance through the Instance() method that we created.&lt;/p&gt;


	&lt;p&gt;In our usage examples I&#8217;ll declare two logger variables and pass different messages to each one. Since there&#8217;s only one instance both these variables will have references to the same object. This can be verified when you call the PrintAllMessages method of any of the loggers. You&#8217;ll notice that they will print all the messages that were passed to any of the variables.&lt;/p&gt;


	&lt;p&gt;Here is the code so you can test it at home:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
    class Program
    {
        static void Main(string[] args)
        {
            Logger logger1 = Logger.Instance();
            Logger logger2 = Logger.Instance();

            logger1.Log(&quot;loading one&quot;);
            logger2.Log(&quot;loading two&quot;);

            logger1.Log(&quot;Success&quot;);
            logger2.Log(&quot;Failure&quot;);

            logger1.PrintAllMessages();
        }
    }
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;That&#8217;s it for the Singleton, I hope this is useful to someone somewhere :-)&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.gbogea.com/">
    <author>
      <name>gabriel</name>
    </author>
    <id>tag:www.gbogea.com,2008-07-14:25</id>
    <published>2008-07-14T12:59:00Z</published>
    <updated>2008-07-22T01:07:08Z</updated>
    <category term="Tips and Tutorials"/>
    <link href="http://www.gbogea.com/2008/7/14/add-controls-dynamically-part-2" rel="alternate" type="text/html"/>
    <title>Add Controls Dynamically - Part 2</title>
<content type="html">
            &lt;p&gt;This post continues what&#8217;s been done in the &lt;a href=&quot;http://www.gbogea.com/2008/7/6/add-controls-dynamically&quot;&gt;Part 1&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;On my last post I talked about creating controls dynamically in asp.net. In this post I&#8217;m going to continue with common issues or needs that users have based on what I noticed on the &lt;a href=&quot;http://forums.asp.net&quot;&gt;Asp.Net Forums&lt;/a&gt;&lt;/p&gt;


Related posts:
	&lt;ul&gt;
	&lt;li&gt;&lt;a href=&quot;http://www.gbogea.com/2008/7/6/add-controls-dynamically&quot;&gt;Add Controls Dynamically &#8211; Part 1&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href=&quot;http://www.gbogea.com/2008/7/22/add-controls-dynamically-part-3&quot;&gt;Add Controls Dynamically &#8211; Part 3&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href=&quot;http://www.gbogea.com/2008/7/22/add-controls-dynamically-part-4&quot;&gt;Add Controls Dynamically &#8211; Part 4&lt;/a&gt;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;First I want to talk about assigning events to the controls that we are creating, this is a simple task but still and important topic.&lt;/p&gt;


	&lt;h3&gt;Adding Events&lt;/h3&gt;


	&lt;p&gt;The first thing we need to do is create a method that will be the handler for the event. When the event is fired on the control this is the method we want to be called to handle the event. In our example we are using TextBoxes so we want to handle the TextChanged event and make the background of the control yellow. Here is the method:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
    private void TextBox_TextChanged(object sender, EventArgs e)
    {
        //the sender is the control that fired the event
        //that is the control we want to change the color
        TextBox tb = (TextBox)sender;
        tb.BackColor = System.Drawing.Color.Yellow ;
    }
&lt;/code&gt;
&lt;/pre&gt; 

	&lt;p&gt;The name of the method is irrelevant but it&#8217;s return type and parameters are not. If you create an event through VisualStudio you&#8217;ll notice that the signature of the method is the same.&lt;/p&gt;


	&lt;p&gt;Now that we have the method we need to set this method as the handler for the event on the controls that we are creating. Adding the method to the controls handler is as simples as this:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
tb.TextChanged += TextBox_TextChanged;
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;If you add your controls now all of them will have this handler and it&#8217;s background will turn yellow (after submit) if you change the content of the textbox.&lt;/p&gt;


	&lt;h3&gt;PlaceHolder&lt;/h3&gt;


	&lt;p&gt;Up until now we&#8217;ve been adding the controls directly to the Form. This will cause the controls to be added to the end of the page after all other controls. This is fine as an example but might not be you desired result. We will you the PlaceHolder control as a container to our controls, this way we can put the PlaceHolder wherever we want in the page and the controls will be added within it.&lt;/p&gt;


	&lt;p&gt;Another advantage of the PlaceHolder control is that it doesn&#8217;t generate any html, it&#8217;s just the to serve (as it&#8217;s own name says) as a place holder. So you don&#8217;t need to worry about adding extra html just to have a place to put your controls in.&lt;/p&gt;


	&lt;p&gt;In our example I will put the PlaceHolder before the button that we are using just to show that the controls will no longer be added to the end of the page.&lt;/p&gt;


	&lt;p&gt;The new page with the events and the PlaceHolder control will be like this:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
&amp;lt;script runat=&quot;server&quot;&amp;gt;

    void Page_Load(object sender, EventArgs e)
    {
        //when the user first enters the page set the count as zero
        if (!IsPostBack)
        {
            ViewState[&quot;count&quot;] = 0;
        }
        //on every subsequent postback, check the control count
        //and recreated all the controls
        else
        {
            int controlCount = (int)ViewState[&quot;count&quot;];
            for (int i = 0; i &amp;lt; controlCount; i++)
            {
                createDynamicControls();
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        createDynamicControls();
        //increment the number of controls
        ViewState[&quot;count&quot;] = (int)ViewState[&quot;count&quot;] + 1;        
    }

    //this method takes care of creating the controls
    private void createDynamicControls()
    {
        TextBox tb = new TextBox();
        tb.TextChanged += TextBox_TextChanged;
        PlaceHolder1.Controls.Add(tb);
    }

    private void TextBox_TextChanged(object sender, EventArgs e)
    {
        //the sender is the control that fired the event
        //that is the control we want to change the color
        TextBox tb = (TextBox)sender;
        tb.BackColor = System.Drawing.Color.Yellow ;
    }
&amp;lt;/script&amp;gt;

&amp;lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&amp;gt;
&amp;lt;head id=&quot;Head1&quot; runat=&quot;server&quot;&amp;gt;
    &amp;lt;title&amp;gt;Untitled Page&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;form id=&quot;form1&quot; runat=&quot;server&quot;&amp;gt;
    &amp;lt;div&amp;gt;
        &amp;lt;asp:PlaceHolder ID=&quot;PlaceHolder1&quot; runat=&quot;server&quot;&amp;gt;&amp;lt;/asp:PlaceHolder&amp;gt;
        &amp;lt;asp:Button ID=&quot;Button1&quot; runat=&quot;server&quot; Text=&quot;Add TextBox&quot; 
            onclick=&quot;Button1_Click&quot; /&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;/form&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Stay tuned for more about dynamically created controls.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.gbogea.com/">
    <author>
      <name>gabriel</name>
    </author>
    <id>tag:www.gbogea.com,2008-07-06:22</id>
    <published>2008-07-06T21:35:00Z</published>
    <updated>2008-07-22T01:06:08Z</updated>
    <category term="Tips and Tutorials"/>
    <link href="http://www.gbogea.com/2008/7/6/add-controls-dynamically" rel="alternate" type="text/html"/>
    <title>Add Controls Dynamically</title>
<content type="html">
            &lt;p&gt;Dynamic controls are an interesting topic that keep showing up in the asp.net forums. I&#8217;ll try to explain the main points about creating and working with dynamically created controls in a few articles (this one would be too long otherwise).&lt;/p&gt;


First of all there are two points you need to understand:
	&lt;ol&gt;
	&lt;li&gt;Adding the controls dynamically is easy, the tricky part is getting them to stick around. &lt;/li&gt;
		&lt;li&gt;The controls are not kept between postbacks therefore you have to keep recreating them every time there is a postback.&lt;/li&gt;
		&lt;li&gt;The best place to recreate the controls in the Page_Load or Page_Init events.&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;Creating a new control is easy, it&#8217;s just a matter of instantiate the class. Adding the control to the page is also easy you only need to add the new control to the page controls.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
//create a new textbox
TextBox tb = new TextBox();
//add the new textbox to the page
Page.Form.Controls.Add(tb);
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;You&#8217;ve seen how easy it is to add the controls but if you don&#8217;t keep recreating them when you submit the page the dynamic controls will be gone.&lt;/p&gt;


	&lt;p&gt;First understand that this need to be recreated is not exclusive for dynamic created controls. Every time there is a postback the whole page is recreated. The difference is that all the controls that are declared in the aspx page are created automatically what gives the user the impression that the controls are always there (they are not!).&lt;/p&gt;


	&lt;p&gt;Since dynamically  created controls are not hard coded anywhere (obviously) you are the one responsible for recreating them.&lt;/p&gt;


	&lt;p&gt;If you create a control in the Page_Load event you&#8217;ll notice that it works just like any control already in the aspx page:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
&amp;lt;script runat=&quot;server&quot;&amp;gt;

    void Page_Load(object sender, EventArgs e)
    {
        TextBox tb = new TextBox();
        Page.Form.Controls.Add(tb);
    }
&amp;lt;/script&amp;gt;

&amp;lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&amp;gt;
&amp;lt;head runat=&quot;server&quot;&amp;gt;
    &amp;lt;title&amp;gt;Untitled Page&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;form id=&quot;form1&quot; runat=&quot;server&quot;&amp;gt;
    &amp;lt;div&amp;gt;

    &amp;lt;/div&amp;gt;
    &amp;lt;/form&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Granted that if you could create the control like this you might declare it in the aspx page as well. Ok, so let&#8217;s improve our example and create a button that adds one and only one edit to the page. If the edit already exists the button won&#8217;t do any thing.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
&amp;lt;script runat=&quot;server&quot;&amp;gt;

    void Page_Load(object sender, EventArgs e)
    {
        //means that the control has been created already so you 
        //need to recreate it
        if (ViewState[&quot;tb&quot;] != null)
        {
            createDynamicControls();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        //means the control has not been created yet
        //create the control and store the viewstate
        if (ViewState[&quot;tb&quot;] == null)
        {
            createDynamicControls();
            ViewState[&quot;tb&quot;] = true;
        }        
    }

    //this method takes care of creating the controls
    private void createDynamicControls()
    {
        TextBox tb = new TextBox();
        Page.Form.Controls.Add(tb);
    }

&amp;lt;/script&amp;gt;

&amp;lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&amp;gt;
&amp;lt;head runat=&quot;server&quot;&amp;gt;
    &amp;lt;title&amp;gt;Untitled Page&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;form id=&quot;form1&quot; runat=&quot;server&quot;&amp;gt;
    &amp;lt;div&amp;gt;
        &amp;lt;asp:Button ID=&quot;Button1&quot; runat=&quot;server&quot; Text=&quot;Add TextBox&quot; 
            onclick=&quot;Button1_Click&quot; /&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;/form&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Notice that in this version of the code I created a method responsible for creating the dynamic control. This will help to encapsulate the method creation and avoid duplicating code. Also notice that I&#8217;m using the ViewState to help maintain a variable that indicates if the control has been added yet in order to avoid duplication.&lt;/p&gt;


	&lt;p&gt;When the user clicks the button I check my ViewState variable to see if the TextBox has been created, if it hasn&#8217;t I create the TextBox, add it to the page and set the ViewState variable. Now, as long as you don&#8217;t leave the page the control and it&#8217;s state will be maintained.&lt;/p&gt;


	&lt;p&gt;We could also let the user add an undefined number of TextBoxes to the page making some small changes.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
&amp;lt;script runat=&quot;server&quot;&amp;gt;

    void Page_Load(object sender, EventArgs e)
    {
        //when the user first enters the page set the count as zero
        if (!IsPostBack)
        {
            ViewState[&quot;count&quot;] = 0;
        }
        //on every subsequent postback, check the control count
        //and recreated all the controls
        else
        {
            int controlCount = (int)ViewState[&quot;count&quot;];
            for (int i = 0; i &amp;lt; controlCount; i++)
            {
                createDynamicControls();
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        createDynamicControls();
        //increment the number of controls
        ViewState[&quot;count&quot;] = (int)ViewState[&quot;count&quot;] + 1;        
    }

    //this method takes care of creating the controls
    private void createDynamicControls()
    {
        TextBox tb = new TextBox();
        Page.Form.Controls.Add(tb);
    }
&amp;lt;/script&amp;gt;

&amp;lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&amp;gt;
&amp;lt;head runat=&quot;server&quot;&amp;gt;
    &amp;lt;title&amp;gt;Untitled Page&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;form id=&quot;form1&quot; runat=&quot;server&quot;&amp;gt;
    &amp;lt;div&amp;gt;
        &amp;lt;asp:Button ID=&quot;Button1&quot; runat=&quot;server&quot; Text=&quot;Add TextBox&quot; 
            onclick=&quot;Button1_Click&quot; /&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;/form&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt; 
&lt;/pre&gt;

	&lt;p&gt;Now you are able to add any number of controls keeping the state of them all. On my next post I&#8217;ll keep working with dynamic created controls so stay tuned.&lt;/p&gt;


Related posts:
	&lt;ul&gt;
	&lt;li&gt;&lt;a href=&quot;http://www.gbogea.com/2008/7/14/add-controls-dynamically-part-2&quot;&gt;Add Controls Dynamically &#8211; Part 2&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href=&quot;http://www.gbogea.com/2008/7/22/add-controls-dynamically-part-3&quot;&gt;Add Controls Dynamically &#8211; Part 3&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href=&quot;http://www.gbogea.com/2008/7/22/add-controls-dynamically-part-4&quot;&gt;Add Controls Dynamically &#8211; Part 4&lt;/a&gt;&lt;/li&gt;
	&lt;/ul&gt;
          </content>  </entry>
  <entry xml:base="http://www.gbogea.com/">
    <author>
      <name>gabriel</name>
    </author>
    <id>tag:www.gbogea.com,2008-07-02:20</id>
    <published>2008-07-02T01:12:00Z</published>
    <updated>2008-07-02T01:13:35Z</updated>
    <category term="Tips and Tutorials"/>
    <link href="http://www.gbogea.com/2008/7/2/bind-dropdownlist-to-enum" rel="alternate" type="text/html"/>
    <title>Bind DropDownList to Enum</title>
<content type="html">
            &lt;p&gt;Unfortunately you can&#8217;t Databind an Enum to a DropDownList or to any control for that matter. You can however transform the Enum into something &lt;b&gt;&#8220;bindable&#8221;&lt;/b&gt; like a HashTable.&lt;/p&gt;


	&lt;p&gt;First let&#8217;s define an Enum to use in our example:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
public enum Cars
{
    Ford = 1,
    Kia = 2, 
    Mitsubishi = 3,
    Volkswagen = 4
}
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Now let&#8217;s write a method that will read the enum and convert it into a HashTable.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
    public Hashtable GetEnumForBind(Type enumeration)
    {
        //returns all the names in the enum
        string[] names = Enum.GetNames(enumeration);

        //return all the values in the enum
        Array values = Enum.GetValues(enumeration);

        Hashtable ht = new Hashtable();
        for (int i = 0; i &amp;lt; names.Length; i++)
        {
            //adds each item pair (value/name) to the Hash
            ht.Add(Convert.ToInt32(values.GetValue(i)).ToString(), names[i]);
        }
        return ht;
    }
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Now that we have a method that will allow an enum to a HashTable all we need to do is bind the hash to the DropDownList.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
        //call our previously defined method to create the HashTable
        Hashtable ht = GetEnumForBind(typeof(Cars));

        //set the HashTable as the DataSource
        DropDownList1.DataSource = ht;
        //set the value of the Hash as the display
        DropDownList1.DataTextField = &quot;value&quot;;
        //set the key of the Hash as the value
        DropDownList1.DataValueField = &quot;key&quot;;

        DropDownList1.DataBind();
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;That is it, it should now bind without problems. There are alternate solutions that do not involve binding but instead adding each item of the enum to the DropDownList. You can take a look at the &lt;a href=&quot;http://forums.asp.net/p/1269514/2395348.aspx#2395348&quot;&gt;asp.net forums&lt;/a&gt; to see alternate solutions proposed by other members.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.gbogea.com/">
    <author>
      <name>gabriel</name>
    </author>
    <id>tag:www.gbogea.com,2008-06-17:18</id>
    <published>2008-06-17T15:28:00Z</published>
    <updated>2008-06-17T15:30:27Z</updated>
    <category term="Tips and Tutorials"/>
    <link href="http://www.gbogea.com/2008/6/17/access-asp-net-webservice-from-php-client" rel="alternate" type="text/html"/>
    <title>Access Asp.Net WebService from PHP Client</title>
<content type="html">
            &lt;p&gt;Creating and consuming webservices in asp.net is an easy task with the help of Visual Studio. Although most of the scenarios I worked with were always with asp.net applications consuming asp.net web services, which makes things even easier. This week however I learned something new when I had to get a &lt;span class=&quot;caps&quot;&gt;PHP&lt;/span&gt; client application to connect to my asp.net webservices.&lt;/p&gt;


	&lt;p&gt;One of out clients was called saying that the service was not working. When I tested his &lt;span class=&quot;caps&quot;&gt;PHP&lt;/span&gt; client I noticed the the parameters that he was passing where not getting to my webservice even though the service was being called. The only problem then was passing the parameters. I Googled a little and got my own &lt;span class=&quot;caps&quot;&gt;PHP&lt;/span&gt; client running.&lt;/p&gt;


	&lt;p&gt;For the sake of this post let&#8217;s say that our webservice gets an string as a parameters and returns the same value, this will let us test our code. So here is the asp.net webservice:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
[WebService(Namespace = &quot;http://tempuri.org/&quot;)]
public class Service : System.Web.Services.WebService
{

    [WebMethod]
    public string MyTestMethod(string myParameter)
    {
        return &quot;The value of the parameter is: &quot; + myParameter;
    }

}
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;As you can see there&#8217;s nothing to it.&lt;/p&gt;


	&lt;p&gt;The &lt;span class=&quot;caps&quot;&gt;PHP&lt;/span&gt; code is not much harder once you Google a little. First you have to have &lt;a href=&quot;http://sourceforge.net/projects/nusoap/&quot;&gt;NuSoap Library&lt;/a&gt; which has the webservices infrastructure for &lt;span class=&quot;caps&quot;&gt;PHP&lt;/span&gt;. You may put it in the same directory of your test page. Then you code a simple client to access our service:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
&amp;lt;?php

    require_once('./nusoap.inc');

    $wsdl = &quot;http://mysite/myapp/Service.asmx?wsdl&quot;;

    $mynamespace = &quot;http://tempuri.org/&quot;;

    $theVariable = array('myParameter'=&amp;gt; 'gabriel');

    $s = new SoapClient($wsdl,true);

    $result = $s-&amp;gt;call('MyTestMethod',$theVariable);

    echo &quot;&amp;lt;h1&amp;gt;Resultado:&amp;lt;/h1&amp;gt;&quot;;

    echo &quot;&amp;lt;pre&amp;gt;&quot;; print_r($result); print &quot;&amp;lt;/pre&amp;gt;&quot;;
?&amp;gt;
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;After I had the code I could verify for my self that my client was right. The parameters in the above code are not getting to the webservice. The coding is right, I checked in several sites in the Internet, so where could the problem be?&lt;/p&gt;


Well, &lt;span class=&quot;caps&quot;&gt;SOAP&lt;/span&gt; specification allows for two formatting options: Style and Use. Style handles the formatting of the Body element of the envelope. Style allows for 2 values: 
	&lt;ol&gt;
	&lt;li&gt;&lt;span class=&quot;caps&quot;&gt;RPC&lt;/span&gt; (Remote Procedure Call) which is the older format;&lt;/li&gt;
		&lt;li&gt;Document which is a newer format and is VisualStudio default format.&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;By now I bet you have figured out the solution, right? If you said that &lt;span class=&quot;caps&quot;&gt;PHP&lt;/span&gt; default encoding is &lt;b&gt;&lt;span class=&quot;caps&quot;&gt;RPC&lt;/span&gt;&lt;/b&gt; and asp.net default encoding is &lt;b&gt;Document&lt;/b&gt; then you&#8217;re right on.&lt;/p&gt;


	&lt;p&gt;How to solve the problem? Well, you can solve the problem on both ends, it all depends on your choice. First let&#8217;s fix it on the asp.net side, it&#8217;s really simple. All you have to do is use the SoapRpcMethod attribute on WebMethod, like this:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
[WebService(Namespace = &quot;http://tempuri.org/&quot;)]
public class Service : System.Web.Services.WebService
{

    [SoapRpcMethod()]
    [WebMethod]
    public string MyTestMethod(string myParameter)
    {
        return &quot;The value of the parameter is: &quot; + myParameter;
    }

}
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;That&#8217;s it, the &lt;span class=&quot;caps&quot;&gt;PHP&lt;/span&gt; client starts to work instantly.&lt;/p&gt;


This is a good solution if you only had &lt;span class=&quot;caps&quot;&gt;PHP&lt;/span&gt; clients, since that was not my case I didn&#8217;t want to mess with my webservice and have to make changes to all other asp.net clients. I wanted to solve the problem on the &lt;span class=&quot;caps&quot;&gt;PHP&lt;/span&gt; client, so let&#8217;s see how we could code the &lt;span class=&quot;caps&quot;&gt;PHP&lt;/span&gt; client to fix the problem.

 &lt;pre&gt;
&lt;code&gt;
&amp;lt;?php

    require_once('./nusoap.inc');

    $wsdl = &quot;http://mysite/myapp/Service.asmx?wsdl&quot;;

    $mynamespace = &quot;http://tempuri.org/&quot;;

    $theVariable = array('myParameter'=&amp;gt; 'gabriel');

    $s = new SoapClient($wsdl,true);

    //here is the only change you need to do
    $result = $s-&amp;gt;call('MyTestMethod',array('parameters' =&amp;gt;  $theVariable));

    echo &quot;&amp;lt;h1&amp;gt;Resultado:&amp;lt;/h1&amp;gt;&quot;;

    echo &quot;&amp;lt;pre&amp;gt;&quot;; print_r($result); print &quot;&amp;lt;/pre&amp;gt;&quot;;
?&amp;gt;
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;What I had to do in the &lt;span class=&quot;caps&quot;&gt;PHP&lt;/span&gt; code was to add an extra array around the array with the original variables. This handles the difference in the Document encoding used by asp.net.&lt;/p&gt;


	&lt;p&gt;This problem really got on my nerves last week and I&#8217;m really glad to have found a solution. As usual I wanted to share this with the world and maybe avoid the same headache to others. Ohh, and I also got to learn a little php on the way&#8230;&lt;/p&gt;


	&lt;p&gt;Happy coding friends!&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.gbogea.com/">
    <author>
      <name>gabriel</name>
    </author>
    <id>tag:www.gbogea.com,2008-06-14:17</id>
    <published>2008-06-14T18:03:00Z</published>
    <updated>2008-06-14T20:16:46Z</updated>
    <link href="http://www.gbogea.com/2008/6/14/dataset-designer-missing" rel="alternate" type="text/html"/>
    <title>DataSet Designer (Editor) Missing</title>
<content type="html">
            &lt;p&gt;This week one of my co-workers had a strange problem with his VisualStudio 2008. All the sudden he couldn&#8217;t view the DataSet designer in any applications. When the DataSet file was clicked all that it showed was the &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt; file, no sign of the Designer.&lt;/p&gt;


	&lt;p&gt;We don&#8217;t know what happened but as I tried to figure out the problem I found that there&#8217;s very few information about this problem on the Internet, so I decide to blog about it so that it might help someone in distress.&lt;/p&gt;


	&lt;p&gt;If you&#8217;re having this problem you might first try to right click the DataSet file and then select the &lt;b&gt;Open With&#8230;&lt;/b&gt; option. A list of editors will show, if you don&#8217;t see the &lt;b&gt;DataSet Editor&lt;/b&gt; then you you have the same problem we did.&lt;/p&gt;


	&lt;p&gt;&lt;img src=&quot;http://www.gbogea.com/assets/2008/6/14/dataseteditor.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;


I have found a few posts about it, here are the solutions proposed:
	&lt;ul&gt;
	&lt;li&gt;Run the following command in the VisualStudio prompt: devenv /resetsettings &lt;/li&gt;
		&lt;li&gt;Run the following command in the VisualStudio prompt: devenv /setup&lt;/li&gt;
		&lt;li&gt;With Visual Studio setup disc select the Repair option.&lt;/li&gt;
		&lt;li&gt;Re-install Visual Studio.&lt;/li&gt;
	&lt;/ul&gt;


All these are valid solutions and I found people on the web who claimed that this helped them but for my unfortunate teammate it didn&#8217;t help. The only thing that did it for us was:
	&lt;ol&gt;
	&lt;li&gt;Remove VisualStudio&lt;/li&gt;
		&lt;li&gt;Delete any folder left from VS installation&lt;/li&gt;
		&lt;li&gt;Install VisualStudio&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;Finally, problem solved! I know this is not an optimal solution, far from it but at the end of the day it works. Nothing else did. I hope this post helps some else.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.gbogea.com/">
    <author>
      <name>gabriel</name>
    </author>
    <id>tag:www.gbogea.com,2008-06-06:16</id>
    <published>2008-06-06T03:11:00Z</published>
    <updated>2008-06-06T03:12:44Z</updated>
    <category term="Tips and Tutorials"/>
    <link href="http://www.gbogea.com/2008/6/6/problem-with-formview-inside-updatepanel" rel="alternate" type="text/html"/>
    <title>Problem with FormView inside UpdatePanel</title>
<content type="html">
            &lt;p&gt;Today I struggled for a while with a FormView that for no apparent reason wasn&#8217;t updating some of my fields to the database. After sometime I noticed that the fields that were not getting inserted into the database where the ones I had wrapped inside an UpdatePanel. Coincidence? I don&#8217;t think so&#8230;&lt;/p&gt;


	&lt;p&gt;I did a few tests and noticed that that was the problem indeed. I can&#8217;t say exactly what is the problem but it seems like that the FormView doesn&#8217;t like to have it&#8217;s fields inside an UpdatePanel.&lt;/p&gt;


	&lt;p&gt;The way I found to get around this issue is to populate the parameters manually in the events of the ObjectDataSource (ODS). So if you&#8217;re trying to insert a record you might using the Inserting event of the &lt;span class=&quot;caps&quot;&gt;ODS&lt;/span&gt; to populate the problematic paramters:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
    protected void ObjectDataSource1_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
    {
        e.InputParameters[&quot;FirstName&quot;] = ((TextBox)FormView1.FindContro(&quot;TextBoxFirstName&quot;)).Text;
        e.InputParameters[&quot;LastName&quot;] = ((TextBox)FormView1.FindContro(&quot;TextBoxLastName&quot;)).Text;
        e.InputParameters[&quot;City&quot;] = ((TextBox)FormView1.FindContro(&quot;TextBoxCity&quot;)).Text;
    }
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;In the Inserting event of the &lt;span class=&quot;caps&quot;&gt;ODS&lt;/span&gt; the InputParamters have already been populated (at least the ones outside the UpdatePanel) but the record hasn&#8217;t been inserted yet, so you are intercepting the parameters, adjusting it&#8217;s values and then letting it continue with the insertion.&lt;/p&gt;


	&lt;p&gt;I hope this code spares someone to have to go through the tests I had to do.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.gbogea.com/">
    <author>
      <name>gabriel</name>
    </author>
    <id>tag:www.gbogea.com,2008-06-04:15</id>
    <published>2008-06-04T18:54:00Z</published>
    <updated>2008-06-04T19:13:20Z</updated>
    <category term="News"/>
    <link href="http://www.gbogea.com/2008/6/4/passed-70-528-today" rel="alternate" type="text/html"/>
    <title>Passed 70-528 today</title>
<content type="html">
            &lt;p&gt;Well, today I got one more certification out of the way. I have just arrived from the Prometric test center where I passed the 70-528 (aka Microsoft .NET Framework 2.0 – Web-Based Client Development). Now I&#8217;m a Microsoft Certified Technology Specialist (MCTS): .NET Framework 2.0 Web Applications.&lt;/p&gt;


	&lt;p&gt;Overall I considered the test to be easy compared to the 70-536. If you use the Microsoft certification books and you already have hands on experience you shouldn&#8217;t have any trouble passing this exam.&lt;/p&gt;


	&lt;p&gt;Now I will concentrate in the 70-529 (aka Microsoft .NET Framework 2.0 &#8211; Distributed Application Development) exam and I think this will keep me busy for another 2 months or so. My goal is to get as many certifications as I can until next year when I planning to move to Canada. Since I don&#8217;t know any one there I figure that the certifications are the most universal way of proving my knowledge.&lt;/p&gt;


	&lt;p&gt;Thanks to everyone that helped me!&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.gbogea.com/">
    <author>
      <name>gabriel</name>
    </author>
    <id>tag:www.gbogea.com,2008-05-30:14</id>
    <published>2008-05-30T02:42:00Z</published>
    <updated>2008-05-30T02:42:54Z</updated>
    <category term="Tips and Tutorials"/>
    <link href="http://www.gbogea.com/2008/5/30/how-to-clear-all-controls-at-once" rel="alternate" type="text/html"/>
    <title>How to clear all controls at once</title>
<content type="html">
            &lt;p&gt;I hate to do repetitive tasks and I think this is a good thing because it always makes me think about ways to improve my coding skills.&lt;/p&gt;


	&lt;p&gt;One thing that is pretty common and repetitive is having to clear the controls in a form. We always have to do the same coding clearing control by control, right? Well, one good way to avoid this repetition is creating a method that would iterate over all the controls in a page and clear each one of them.&lt;/p&gt;


	&lt;p&gt;If your Page has nothing but a form with Edits and CheckBoxes the task is very simple. The following code will clear all asp.net controls at once.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
        foreach (Control control in form1.Controls)
        {
            IEditableTextControl textControl = control as IEditableTextControl;
            if (textControl != null)
            {
                textControl.Text = string.Empty;

            }
            ListControl listControl = control as ListControl;
            if (listControl != null)
            {
                listControl.SelectedIndex = -1;
            }
            ICheckBoxControl checkBoxControl = control as ICheckBoxControl;
            if (checkBoxControl != null)
            {
                checkBoxControl.Checked = false;
            }
        }
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;The code above basically tests all controls within the form, and if it is one of the kind of controls I want to clear the the control gets cleared, each one in it&#8217;s own manner. TextBoxes are set to empty, list controls have it&#8217;s index set to -1 and check controls get set as false.&lt;/p&gt;


	&lt;p&gt;This code is great but it has one problem, if inside the form you have other panels this method won&#8217;t clear the controls inside the panels. To do this we need to navigate the controls recursively. Next I provide a complete page that adds the recursive call to check each control sub controls.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
&amp;lt;%@ Page Language=&quot;C#&quot; %&amp;gt;

&amp;lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&amp;gt;

&amp;lt;script runat=&quot;server&quot;&amp;gt;

    protected void Button1_Click(object sender, EventArgs e)
    {
        ClearControls(form1);
    }

    private void ClearControls(Control containerControl)
    {
        foreach (Control control in containerControl.Controls)
        {
            IEditableTextControl textControl = control as IEditableTextControl;
            if (textControl != null)
            {
                textControl.Text = string.Empty;

            }
            ListControl listControl = control as ListControl;
            if (listControl != null)
            {
                listControl.SelectedIndex = -1;
            }
            ICheckBoxControl checkBoxControl = control as ICheckBoxControl;
            if (checkBoxControl != null)
            {
                checkBoxControl.Checked = false;
            }
            //this is the recursive call
            ClearControls(control);
        }
    }

&amp;lt;/script&amp;gt;

&amp;lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&amp;gt;
&amp;lt;head runat=&quot;server&quot;&amp;gt;
    &amp;lt;title&amp;gt;Untitled Page&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;form id=&quot;form1&quot; runat=&quot;server&quot;&amp;gt;
    &amp;lt;div&amp;gt;
        &amp;lt;asp:Button ID=&quot;Button1&quot; runat=&quot;server&quot; OnClick=&quot;Button1_Click&quot; Text=&quot;Button&quot; /&amp;gt;
        &amp;lt;br /&amp;gt;
        &amp;lt;asp:TextBox ID=&quot;TextBox1&quot; runat=&quot;server&quot; Text=&quot;hello&quot;&amp;gt;&amp;lt;/asp:TextBox&amp;gt;
        &amp;lt;br /&amp;gt;
        &amp;lt;asp:DropDownList ID=&quot;DropDownList1&quot; runat=&quot;server&quot;&amp;gt;
            &amp;lt;asp:ListItem&amp;gt;1&amp;lt;/asp:ListItem&amp;gt;
            &amp;lt;asp:ListItem&amp;gt;1&amp;lt;/asp:ListItem&amp;gt;
            &amp;lt;asp:ListItem&amp;gt;3&amp;lt;/asp:ListItem&amp;gt;
        &amp;lt;/asp:DropDownList&amp;gt;
        &amp;lt;br /&amp;gt;
        &amp;lt;asp:RadioButton ID=&quot;RadioButton1&quot; runat=&quot;server&quot; /&amp;gt;
        &amp;lt;br /&amp;gt;
        &amp;lt;asp:CheckBox ID=&quot;CheckBox1&quot; runat=&quot;server&quot; /&amp;gt;
        &amp;lt;br /&amp;gt;
        &amp;lt;asp:RadioButtonList ID=&quot;RadioButtonList1&quot; runat=&quot;server&quot;&amp;gt;
            &amp;lt;asp:ListItem&amp;gt;A&amp;lt;/asp:ListItem&amp;gt;
            &amp;lt;asp:ListItem&amp;gt;B&amp;lt;/asp:ListItem&amp;gt;
            &amp;lt;asp:ListItem&amp;gt;C&amp;lt;/asp:ListItem&amp;gt;
        &amp;lt;/asp:RadioButtonList&amp;gt;
        &amp;lt;asp:CheckBoxList ID=&quot;CheckBoxList1&quot; runat=&quot;server&quot;&amp;gt;
            &amp;lt;asp:ListItem&amp;gt;A&amp;lt;/asp:ListItem&amp;gt;
            &amp;lt;asp:ListItem&amp;gt;B&amp;lt;/asp:ListItem&amp;gt;
            &amp;lt;asp:ListItem&amp;gt;C&amp;lt;/asp:ListItem&amp;gt;
        &amp;lt;/asp:CheckBoxList&amp;gt;
        &amp;lt;asp:Panel ID=&quot;panel1&quot; runat=&quot;server&quot;&amp;gt;
            &amp;lt;br /&amp;gt;
            &amp;lt;asp:TextBox ID=&quot;TextBox2&quot; runat=&quot;server&quot; Text=&quot;hello&quot;&amp;gt;&amp;lt;/asp:TextBox&amp;gt;
            &amp;lt;br /&amp;gt;
            &amp;lt;asp:DropDownList ID=&quot;DropDownList2&quot; runat=&quot;server&quot;&amp;gt;
                &amp;lt;asp:ListItem&amp;gt;1&amp;lt;/asp:ListItem&amp;gt;
                &amp;lt;asp:ListItem&amp;gt;1&amp;lt;/asp:ListItem&amp;gt;
                &amp;lt;asp:ListItem&amp;gt;3&amp;lt;/asp:ListItem&amp;gt;
            &amp;lt;/asp:DropDownList&amp;gt;
            &amp;lt;br /&amp;gt;
            &amp;lt;asp:RadioButton ID=&quot;RadioButton2&quot; runat=&quot;server&quot; /&amp;gt;
            &amp;lt;br /&amp;gt;
            &amp;lt;asp:CheckBox ID=&quot;CheckBox2&quot; runat=&quot;server&quot; /&amp;gt;
            &amp;lt;br /&amp;gt;
            &amp;lt;asp:RadioButtonList ID=&quot;RadioButtonList2&quot; runat=&quot;server&quot;&amp;gt;
                &amp;lt;asp:ListItem&amp;gt;A&amp;lt;/asp:ListItem&amp;gt;
                &amp;lt;asp:ListItem&amp;gt;B&amp;lt;/asp:ListItem&amp;gt;
                &amp;lt;asp:ListItem&amp;gt;C&amp;lt;/asp:ListItem&amp;gt;
            &amp;lt;/asp:RadioButtonList&amp;gt;
            &amp;lt;asp:CheckBoxList ID=&quot;CheckBoxList2&quot; runat=&quot;server&quot;&amp;gt;
                &amp;lt;asp:ListItem&amp;gt;A&amp;lt;/asp:ListItem&amp;gt;
                &amp;lt;asp:ListItem&amp;gt;B&amp;lt;/asp:ListItem&amp;gt;
                &amp;lt;asp:ListItem&amp;gt;C&amp;lt;/asp:ListItem&amp;gt;
            &amp;lt;/asp:CheckBoxList&amp;gt;
        &amp;lt;/asp:Panel&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;/form&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;You can go even further than this and create a specialized user control or a server control in the form of a button that when clicked would clear of controls of the form. I&#8217;ll can give you a starting point with a user control,which is an easier example but you could go ahead and get pretty sophisticated with it if you feel like it. Here&#8217;s the code for the user control:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
&amp;lt;%@ Control Language=&quot;C#&quot; ClassName=&quot;ButtonClear&quot; %&amp;gt;

&amp;lt;script runat=&quot;server&quot;&amp;gt;

    //a simple property to allow you to set the inner button text
    public string Text
    {
        get { return Button1.Text; }
        set { Button1.Text = value; }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        ClearControls(Page);
    }

    private void ClearControls(Control containerControl)
    {
        foreach (Control control in containerControl.Controls)
        {
            IEditableTextControl textControl = control as IEditableTextControl;
            if (textControl != null)
            {
                textControl.Text = string.Empty;

            }
            ListControl listControl = control as ListControl;
            if (listControl != null)
            {
                listControl.SelectedIndex = -1;
            }
            ICheckBoxControl checkBoxControl = control as ICheckBoxControl;
            if (checkBoxControl != null)
            {
                checkBoxControl.Checked = false;
            }
            ClearControls(control);
        }
    }

&amp;lt;/script&amp;gt;

&amp;lt;asp:Button ID=&quot;Button1&quot; runat=&quot;server&quot; Text=&quot;Button&quot; onclick=&quot;Button1_Click&quot; /&amp;gt;
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Now all you have to do is drop the control in the page and you done.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.gbogea.com/">
    <author>
      <name>gabriel</name>
    </author>
    <id>tag:www.gbogea.com,2008-05-21:11</id>
    <published>2008-05-21T13:25:00Z</published>
    <updated>2008-05-21T14:19:11Z</updated>
    <category term="Tips and Tutorials"/>
    <link href="http://www.gbogea.com/2008/5/21/force-file-download" rel="alternate" type="text/html"/>
    <title>Force File Download</title>
<content type="html">
            &lt;p&gt;One handy feature that browser&#8217;s offer is the ability to automatically open know file types in their respective program. There are however times when you want to force the user to download the file, you want the &lt;strong&gt;Save As&lt;/strong&gt; dialog box to appear to the user.&lt;/p&gt;


	&lt;p&gt;In order to avoid the default behavior and stop file from being open automatically you can use the content-disposition header. This is an &lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt; header, and is not asp.net specific.&lt;/p&gt;


	&lt;table&gt;
		&lt;tr&gt;
			&lt;td&gt; &lt;strong&gt;Content-disposition: attachment; filename=fname.ext&lt;/strong&gt;&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;




	&lt;p&gt;To do this using asp.net resources you can use the Response object to set the header like this:&lt;/p&gt;


	&lt;table&gt;
		&lt;tr&gt;
			&lt;td&gt; &lt;strong&gt;Response.AppendHeader(&#8220;Content-disposition&#8221;, &#8220;attachment; filename=&#8221; + fileName);&lt;/strong&gt;&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;




	&lt;p&gt;It&#8217;s very simple and very handy.  I wrote a working example below:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
&amp;lt;%@ Page Language=&quot;C#&quot; %&amp;gt;

&amp;lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&amp;gt;

&amp;lt;script runat=&quot;server&quot;&amp;gt;

    protected void Button1_Click(object sender, EventArgs e)
    {
        string fileName = &quot;MyFile.doc&quot;;
        string filePath = Server.MapPath(&quot;MyFiles/&quot;+fileName);
        Response.AppendHeader(&quot;Content-disposition&quot;, &quot;attachment; filename=&quot; + fileName);
        Response.ContentType = &quot;Application/msword&quot;;
        Response.WriteFile(filePath);
        Response.End();
    }
&amp;lt;/script&amp;gt;

&amp;lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&amp;gt;
&amp;lt;head runat=&quot;server&quot;&amp;gt;
    &amp;lt;title&amp;gt;Untitled Page&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;form id=&quot;form1&quot; runat=&quot;server&quot;&amp;gt;
    &amp;lt;div&amp;gt;
        &amp;lt;asp:Button ID=&quot;Button1&quot; runat=&quot;server&quot; Text=&quot;Download&quot; 
            onclick=&quot;Button1_Click&quot; /&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;/form&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;

&lt;/code&gt;
&lt;pre&gt;
          </content>  </entry>
  <entry xml:base="http://www.gbogea.com/">
    <author>
      <name>gabriel</name>
    </author>
    <id>tag:www.gbogea.com,2008-05-17:10</id>
    <published>2008-05-17T22:08:00Z</published>
    <updated>2008-05-27T15:49:47Z</updated>
    <category term="Tips and Tutorials"/>
    <link href="http://www.gbogea.com/2008/5/17/exposing-events-in-web-user-controls" rel="alternate" type="text/html"/>
    <title>Exposing events in Web User Controls</title>
<content type="html">
            &lt;p&gt;Web User Controls are a way encapsulating code that would have to be repeated in various pages, they are definitely a huge help.&lt;/p&gt;


	&lt;p&gt;VisualStudio makes so simple to create these user controls that even novice users can do it. When the page doesn&#8217;t need to interact with the user control it is really simple, and there&#8217;s nothing to it but putting the controls you want in the user control. However the controls that are inside the user control are isolated from the Page and you can&#8217;t use it&#8217;s events. This post will show you how easy it is to overcome this issue.&lt;/p&gt;


	&lt;p&gt;Let&#8217;s say we want to create a simple control with a DropDownList, a Button and a Label. When the button is clicked the selected value in the DropDownList is copied to the label. Here&#8217;s the code for it:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
&amp;lt;%@ Control Language=&quot;C#&quot; ClassName=&quot;MyUserControl&quot; %&amp;gt;

&amp;lt;script runat=&quot;server&quot;&amp;gt;

    protected void ButtonCopy_Click(object sender, EventArgs e)
    {
        LabelText.Text = DropDownListNames.SelectedValue;
    }
&amp;lt;/script&amp;gt;
&amp;lt;asp:DropDownList ID=&quot;DropDownListNames&quot; runat=&quot;server&quot;&amp;gt;
    &amp;lt;asp:ListItem&amp;gt;Elisa&amp;lt;/asp:ListItem&amp;gt;
    &amp;lt;asp:ListItem&amp;gt;Gabriel&amp;lt;/asp:ListItem&amp;gt;
    &amp;lt;asp:ListItem&amp;gt;Rafaela&amp;lt;/asp:ListItem&amp;gt;
&amp;lt;/asp:DropDownList&amp;gt;
&amp;lt;br /&amp;gt;
&amp;lt;br /&amp;gt;
&amp;lt;asp:Button ID=&quot;ButtonCopy&quot; runat=&quot;server&quot; Text=&quot;Copy SelectedValue to Label&quot; 
    onclick=&quot;ButtonCopy_Click&quot; /&amp;gt;
&amp;lt;br /&amp;gt;
&amp;lt;br /&amp;gt;
&amp;lt;asp:Label ID=&quot;LabelText&quot; runat=&quot;server&quot; Text=&quot;Label&quot; Font-Size=&quot;X-Large&quot;&amp;gt;&amp;lt;/asp:Label&amp;gt;
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;This is simple enough, right? All you have to do is place this control in any page and this behavior is going to be replicated. What I notice that most novice programmers miss is if you need the control to send some kind of information to the page.&lt;/p&gt;


	&lt;p&gt;Say you want to have a label in your page showing the time that the Copy button of the user control is clicked. It would be easy if you could access the button click event of the button inside the user control but unfortunately you can&#8217;t. What you want is to make the event of the inner button accessible to outside the control, this is done creating a new event exposing the event you want.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
&amp;lt;%@ Control Language=&quot;C#&quot; ClassName=&quot;MyUserControl&quot; %&amp;gt;

&amp;lt;script runat=&quot;server&quot;&amp;gt;

    //this is the event that will be exposed
    public event EventHandler ButtonClick;

    protected void ButtonCopy_Click(object sender, EventArgs e)
    {
        LabelText.Text = DropDownListNames.SelectedValue;

        //this tests if the event has been subscribed by any method
        if (ButtonClick != null)
        {
            //fires the event passing the same arguments of the button
            //click event
            ButtonClick(sender, e);
        }
    }
&amp;lt;/script&amp;gt;
&amp;lt;asp:DropDownList ID=&quot;DropDownListNames&quot; runat=&quot;server&quot;&amp;gt;
    &amp;lt;asp:ListItem&amp;gt;Elisa&amp;lt;/asp:ListItem&amp;gt;
    &amp;lt;asp:ListItem&amp;gt;Gabriel&amp;lt;/asp:ListItem&amp;gt;
    &amp;lt;asp:ListItem&amp;gt;Rafaela&amp;lt;/asp:ListItem&amp;gt;
&amp;lt;/asp:DropDownList&amp;gt;
&amp;lt;br /&amp;gt;
&amp;lt;br /&amp;gt;
&amp;lt;asp:Button ID=&quot;ButtonCopy&quot; runat=&quot;server&quot; Text=&quot;Copy SelectedValue to Label&quot; 
    onclick=&quot;ButtonCopy_Click&quot; /&amp;gt;
&amp;lt;br /&amp;gt;
&amp;lt;br /&amp;gt;
&amp;lt;asp:Label ID=&quot;LabelText&quot; runat=&quot;server&quot; Text=&quot;Label&quot; Font-Size=&quot;X-Large&quot;&amp;gt;&amp;lt;/asp:Label&amp;gt;
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Now you are able to put the control in a page and use it&#8217;s event, like in the following example:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
&amp;lt;%@ Page Language=&quot;C#&quot; %&amp;gt;

&amp;lt;%@ Register Src=&quot;MyUserControl.ascx&quot; TagName=&quot;MyUserControl&quot; TagPrefix=&quot;uc1&quot; %&amp;gt;
&amp;lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&amp;gt;

&amp;lt;script runat=&quot;server&quot;&amp;gt;
    protected void MyUserControl1_ButtonClick(object sender, EventArgs e)
    {
        LabelTime.Text = DateTime.Now.ToString();
    }
&amp;lt;/script&amp;gt;

&amp;lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&amp;gt;
&amp;lt;head runat=&quot;server&quot;&amp;gt;
    &amp;lt;title&amp;gt;Untitled Page&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;form id=&quot;form1&quot; runat=&quot;server&quot;&amp;gt;
    &amp;lt;div&amp;gt;
        &amp;lt;!-- Notice here that I use ButtonClick event I just created in the user control --&amp;gt;
        &amp;lt;!-- Also notice that I didn't misspell the event, the On is added by a VisualStudio convention   --&amp;gt;
        &amp;lt;uc1:MyUserControl ID=&quot;MyUserControl1&quot; runat=&quot;server&quot; OnButtonClick=&quot;MyUserControl1_ButtonClick&quot; /&amp;gt;
        &amp;lt;br /&amp;gt;
        &amp;lt;asp:Label ID=&quot;LabelTime&quot; runat=&quot;server&quot; Text=&quot;Label&quot; Font-Bold=&quot;True&quot; 
            Font-Italic=&quot;True&quot; ForeColor=&quot;#FF3300&quot;&amp;gt;&amp;lt;/asp:Label&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;/form&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;You could also subscribe to the event in the code behind just like you do with asp.net controls. Here is the code for it:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
&amp;lt;%@ Page Language=&quot;C#&quot; %&amp;gt;

&amp;lt;%@ Register Src=&quot;MyUserControl.ascx&quot; TagName=&quot;MyUserControl&quot; TagPrefix=&quot;uc1&quot; %&amp;gt;
&amp;lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&amp;gt;

&amp;lt;script runat=&quot;server&quot;&amp;gt;
    public void Page_Load(object sender, EventArgs e)
    {
        MyUserControl1.ButtonClick += MyUserControl1_ButtonClick;
    }

    protected void MyUserControl1_ButtonClick(object sender, EventArgs e)
    {
        LabelTime.Text = DateTime.Now.ToString();
    }
&amp;lt;/script&amp;gt;

&amp;lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&amp;gt;
&amp;lt;head runat=&quot;server&quot;&amp;gt;
    &amp;lt;title&amp;gt;Untitled Page&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;form id=&quot;form1&quot; runat=&quot;server&quot;&amp;gt;
    &amp;lt;div&amp;gt;
        &amp;lt;!-- Notice here that I use ButtonClick event I just created in the user control --&amp;gt;
        &amp;lt;!-- Also notice that I didn't misspell the event, the On is added by a VisualStudio convention   --&amp;gt;
        &amp;lt;uc1:MyUserControl ID=&quot;MyUserControl1&quot; runat=&quot;server&quot; /&amp;gt;
        &amp;lt;br /&amp;gt;
        &amp;lt;asp:Label ID=&quot;LabelTime&quot; runat=&quot;server&quot; Text=&quot;Label&quot; Font-Bold=&quot;True&quot; 
            Font-Italic=&quot;True&quot; ForeColor=&quot;#FF3300&quot;&amp;gt;&amp;lt;/asp:Label&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;/form&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;I wrote this post because I helped a few people in the &lt;a href=&quot;http://forums.asp.net&quot;&gt;asp.net forums&lt;/a&gt; and I hope it can help other people that are beginning to write web user controls. Happy coding guys.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.gbogea.com/">
    <author>
      <name>gabriel</name>
    </author>
    <id>tag:www.gbogea.com,2008-05-09:8</id>
    <published>2008-05-09T13:26:00Z</published>
    <updated>2008-05-09T13:27:41Z</updated>
    <category term="News"/>
    <link href="http://www.gbogea.com/2008/5/9/top-10-answerer-on-asp-net-forum" rel="alternate" type="text/html"/>
    <title>Top 10 Answerer on Asp.Net Forum</title>
<content type="html">
            &lt;p&gt;Today I made it to the Top 10 Answerers of the &lt;a href=&quot;http://forum.asp.net&quot;&gt;asp.net forums&lt;/a&gt; for the first time and I&#8217;m really happy with it.&lt;/p&gt;


	&lt;p&gt;I just hope it&#8217;s not too geeky of me to be celebrating this&#8230; My wife seems to think so!&lt;/p&gt;


	&lt;p&gt;I like the forums because they always give me new challenges to help me learn more. I get problems from people all over the world and it makes me learn a lot of things that I wouldn&#8217;t on my day to day job. If that makes me a geek so be it.&lt;/p&gt;


	&lt;p&gt;&lt;img src=&quot;http://www.gbogea.com/assets/2008/5/9/TopAnswerers2.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
          </content>  </entry>
</feed>
