Com And Hosting

Recently in one of my ASP.NET application I had to create a massive form with lot of drop-down lists and check-box list. Some of the dropdown lists have the same values e.g. Yes, No etc. Now instead of creating to many drop-downlist with same parameters, I thought it would be wise if I can create a custom user control for this common drop-down list and re-use it where ever necessary. I wanted to use this user control inside the formview and bind data for update and insert. Here is what I did –

Here is the very simple code for my user control yesno.ascx page –

<%@ Control Language="C#" ClassName="yesno" %>

<script runat="server">

public string DdlSelectedValue
{
get
{
    return DropDownList1.SelectedValue;
}
set
{
    DropDownList1.SelectedValue = value;
}
}

</script>

<asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem Text="%" Value="" />
    <asp:ListItem Text="Yes" Value="Y" />
    <asp:ListItem Text="No" Value="N" />
</asp:DropDownList>

The above user control basically set the selected value of the dropdown list using get and set function.

Now register the user control in aspx page like below

<%@ Page Title="" Language="VB" MasterPageFile="~/Site.master" AutoEventWireup="false" CodeFile="siteDetails.aspx.vb" Inherits="siteDetails" %>
<%@ Register Src="~/controls/yesno.ascx" TagName="yn" TagPrefix="uc"  %>

Once you register the control, you can bind the control for formview data binding. Below is the example I did to bind data inside the formview –

<table> 
<tr><th>Commercial Tours:</th><td>
        <uc:yn ID="dlCT" runat="server" DdlSelectedValue='<%# Bind("COMMERCIAL_TOURS") %>' />

      </td></tr>
      <tr><th>Interp Available:</th><td>
      <uc:yn ID="ys1" runat="server" DdlSelectedValue='<%# Bind("INTERP_AVAILABLE") %>' />

      </td></tr>
        <tr><th>Attraction/Site Fees:</th><td> 
            <asp:TextBox ID="txtAttractionFee" runat="server" Text='<%# Bind("ATTRACTION_FEE") %>' Width="450" MaxLength="200" />
        </td></tr>

        </table>

I haveĀ omittedĀ other codes to make it shorter. and it works fine for updating and creating record.

.

 

Leave a Reply

Your email address will not be published. Required fields are marked *