title: ASP.NET UserControl input parameters
body: <p>I have written my first UserControl like this:</p>
  
  <pre><code>public partial class DefectMap : System.Web.UI.UserControl
  {
      const string imageProviderUrl = "~/DefectMapImageProvider.ashx?";
  
      public long MapID { get; set; }
      public int Rows { get; set; }
      public int Cols { get; set; }
      public int? Width { get; set; }
      public int? Height { get; set; }
  
      protected void Page_Load(object sender, EventArgs e)
      {
          ComposeImageUrl();
          GenerateTableStructure();
      }
  
      void ComposeImageUrl()
      {
          StringBuilder builder = new StringBuilder(imageProviderUrl);
          builder.AppendFormat("DefectMapId={0}", MapID);
  
          // set up width &amp; height
          if (Width != null &amp;&amp; Width &gt; 0)
          {
              builder.AppendFormat("&amp;Width={0}", Width);
              MapImage.Width = (Unit)Width;
          }
  
          if (Height != null &amp;&amp; Height &gt; 0)
          {
              builder.AppendFormat("&amp;Height={0}", Height);
              MapImage.Height = (Unit)Height;
          }
  
          MapImage.ImageUrl = builder.ToString();
      }
  
      void GenerateTableStructure()
      {
          if (Rows &gt; 0 &amp;&amp; Cols &gt; 0)
          {
              TableHelper.CreateStructure(MapTable, Rows, Cols);
          }
      }
  }
  </code></pre>
  
  <p>If I add this control on some page and set values in markup</p>
  
  <pre><code>&lt;uc:DefectMap ID="DefectMap" runat="server" Height="80" MapID="1" /&gt;
  </code></pre>
  
  <p>it works like I expect. But if I am trying to set up values (Rows, MapID etc.) in code, it doesn't works. Do you see why not? Should I use different method (than Page_Load) to process controls logic? I use this control as child of GridView, trying this:</p>
  
  <pre><code>protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
      {
          GridView grid = (GridView)sender;
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
              DefectMap defectMap = (DefectMap)e.Row.FindControl("DefectMap");
              DEFECTMAP data = (DEFECTMAP)e.Row.DataItem;
  
              defectMap.MapID = data.ID_DEFECTMAP;
              defectMap.Rows = data.ROWS;
              defectMap.Cols = data.COLS;
          }
      }
  </code></pre>