Wednesday, 29 August 2012

How to create a user control and set its event and property.

DesignPage->
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="EditableDropDown.ascx.cs" Inherits="EditableDropDown" %>
<link href="../HomeMaster.css" rel="stylesheet" type="text/css" />
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<table width="100px">
 <tr>
  <td Width="100px">
    <asp:DropDownList ID="DDlState" runat="server" width="231px" CssClass="ddlclass" 
    AutoPostBack="True" OnSelectedIndexChanged="DDlState_SelectedIndexChanged">
    </asp:DropDownList>
    <asp:TextBox ID="TxtState" runat="server" CssClass="ddlclass" width="212px" AutoPostBack="True" OnTextChanged="TxtState_TextChanged"></asp:TextBox>
    <cc1:TextBoxWatermarkExtender ID="txtmark" TargetControlID="TxtState" WatermarkText="Please Select Remark" runat="server"></cc1:TextBoxWatermarkExtender>
     <asp:RequiredFieldValidator ID="rfv_devremark" runat="server" ErrorMessage="Plese Select Closing Remark." ControlToValidate="DDlState" SetFocusOnError="true" Display="None"  InitialValue="--Select--"></asp:RequiredFieldValidator>
  </td>
 </tr>
</table>


CodingPage-->
using System;
using System.Data;
public partial class EditableDropDown : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public void BindData(DataSet ds)
    {
        DDlState.DataSource = ds;
        DDlState.DataTextField = ds.Tables[0].Columns[0].ColumnName;
        DDlState.DataValueField = ds.Tables[0].Columns[0].ColumnName;
        DDlState.DataBind();
        DDlState.Items.Insert(0, "--Select--");
    }
    public event System.EventHandler DDlSelectIndexChanged;
    public event System.EventHandler TxtTextChanged;
    public int DDlSelectedIndex
    {
        set { this.DDlState.SelectedIndex = value; }
        get { return DDlState.SelectedIndex; }
    }
    public string DDlSelectedItem
    {
        get { return DDlState.SelectedItem.ToString(); }
    }
    public string DDlSelectedValue
    {
        get { return DDlState.SelectedValue.ToString(); }
    }
    public string TextBoxValue
    {
        set { this.TxtState.Text =value; }
        get { return this.TxtState.Text; }
    }
    public bool TxtEnable
    {
        set { this.TxtState.Enabled = value; }
    }
    public bool DDlEnable
    {
        set { this.DDlState.Enabled = value; }
    }
    public string Validate
    {
        set { this.rfv_devremark.ValidationGroup = value; }
    }
    public DataSet BindDropdownlist
    {
        set
        {
           this.BindData(value);
        }
    }
    protected virtual void OnSeletChanged(object sender)
    {       
        if (this.DDlSelectIndexChanged != null)
            this.DDlSelectIndexChanged(sender, new EventArgs());
    }
    protected virtual void OnTextboxTextChanged(object sender)
    {
        if (this.TxtTextChanged != null)
            this.TxtTextChanged(sender, new EventArgs());
    }
    protected void DDlState_SelectedIndexChanged(object sender, EventArgs e)
    {
        OnSeletChanged(sender);
    }
    protected void TxtState_TextChanged(object sender, EventArgs e)
    {
        OnTextboxTextChanged(sender);
    }

}

No comments:

Post a Comment