user1077685
user1077685

Reputation:

How to use a custom web control?

I would like to create a control that encapsulates the following controls:

Controls

I've recreated the HTML (excuse the ugly table usage until I clean it up with divs and CSS) like so in a control .ascx file:

<%@ Control Language="VB" AutoEventWireup="false" 
CodeFile="MultiLevelReportFilter.ascx.vb" 
Inherits="controls.MultiLevelReportFilter" %>

<%@ Register TagPrefix="cc1" Namespace="UNLV.IAP.WebControls" 
Assembly="DropDownCheckList" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 25 March 2009), see www.w3.org" />

  <title></title>
  <style type="text/css">
/*<![CDATA[*/
  tr.c2 {display: none;}
  td.c1 {font-weight: bolder; color: Red; font-size: 10pt;}
  /*]]>*/
  </style>
</head>

<body>
  <table width="98%" cellspacing="0" cellpadding="15" align="center">
    <tr>
      <td width="40%" valign="top">
        <table width="600" cellspacing="0" cellpadding="4">
          <tr>
            <td colspan="2" class="txtLabel c1" align="center">Sales as of</td>
          </tr>

          <tr>
            <td align="right" class="txtLabel" width="200">Period&nbsp;</td>

            <td class="txtLabel" width="400"></td>
          </tr>

          <tr class="c2">
            <td align="right" class="txtLabel" width="200">Year&nbsp;</td>

            <td class="txtLabel" width="400">&nbsp; Month&nbsp;</td>
          </tr>

          <tr>
            <td align="right" class="txtLabel" width="200">Accounts Filter&nbsp;</td>

            <td></td>
          </tr>

          <tr>
            <td align="right" class="txtLabel" width="200">Brands Filter&nbsp;</td>

            <td></td>
          </tr>

          <tr id="Tr1" runat="server" visible="false">
            <td align="right" valign="top" class="txtLabel" width="200">Business
            Groups</td>

            <td></td>

            <td></td>
          </tr>

          <tr>
            <td align="right" valign="top" class="txtLabel" width="200">Measurement</td>

            <td>&nbsp;&nbsp; <span class="txtLabel">Display</span> &nbsp;&nbsp;</td>

            <td></td>
          </tr>
        </table>
      </td>

      <td width="60%" valign="top">
        <table width="200">
          <tr>
            <td width="20"><img src="/images/ico_graph1.gif" runat="server" id=
            "iGenerate" /></td>

            <td width="180">Generate&nbsp;Report</td>
          </tr>

          <tr>
            <td width="20"><img src="/images/ico_print.gif" runat="server" id=
            "iPrint" /></td>

            <td width="180">Print</td>
          </tr>

          <tr>
            <td width="20"><img src="/images/ico_excel.jpg" runat="server" id=
            "iExport" /></td>

            <td width="180">Export&nbsp;To&nbsp;Excel</td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
</body>
</html>

My question is two-fold:

How do I include this on another page?

I've tried to register my control and reference it, but I'm missing the Assembly information. I can't seem to find any information on how to register the control in the Assembly:

<%@ Register TagPrefix="cc1" Namespace="Controls.MultiLevelReportFilter" 
Assembly="MultiLevelReportFilter" %>

Visual studio reports errors on the line above because the Assembly is incorrect.

Is it possible to hide certain controls when calling this from another page?

Upvotes: 0

Views: 376

Answers (3)

Mausimo
Mausimo

Reputation: 8168

I usually just add the custom controls to my web.config:

<system.web>
  <pages>
    <controls>
      <add tagPrefix="CC1" src="~/UserControls/ControlName.ascx" tagName="TagName" />
    </controls>
  </pages>
</system.web>

Now in your page you can do:

<CC1:TagName runat="server" ID="TagName1" />

In your user control you can create functions or properties that will hide whatever controls you like. Then from the parent page call:

TagName1.Function(); 

or

TagName1.Property = value;

Upvotes: 1

nunespascal
nunespascal

Reputation: 17724

User controls are not custom server controls.

If you have your ascx in the same project just go into design mode and drag it onto the page where you want it.

If this is something you want to share across multiple projects in the same solution, Scott Gu wrote an article explaining how to reference and use your user controls.

If not, it is best that you create a server control.

Upvotes: 1

Colin
Colin

Reputation: 4135

Your control inherits controls.MultiLevelReportFilter (little c) while your Register tag uses Controls.MultiLevelReportFitler (big C), which may be part of your problem.

Alternately, you can use the syntax:

<%@ Register TagPrefix="cc1" TagName="MultiLevelReportFitler" Src="/Controls/MultiLevelReportFitler.ascx" %>

Note that the user control is assumed to exist in the same web application you use it in.

As far as hiding controls, you can do whatever you want. Just set the visible property to false per whatever criteria you wish (e.g. set the user control's properties, look at QueryString values or the URL of the current page, etc.)

Upvotes: 1

Related Questions