Friday, 6 July 2012

How to Create a user control and how to use in aspx page.

First Right click on your website -> Add one Ascx Page.
 Next .
//This is a popup window mean show message in a user control
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="U_Message.ascx.cs" Inherits="Website.App_WebControls.UFMS_UserControls.U_Message" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
 <asp:Button runat="server" ID="BtnMessage" Style="display: none" />
    <asp:ModalPopupExtender ID="modalMessage" runat="server" TargetControlID="BtnMessage"
        PopupControlID="PnlMessage" OkControlID="Okbtn" CancelControlID="imgClose" BackgroundCssClass="ModalBackgroundCSS">
    </asp:ModalPopupExtender>
    <asp:Panel runat="Server" ID="PnlMessage" DefaultButton="Okbtn" Style="border: solid 2px #BBBBBB;
        font-family: Arial; width: 325px; height: 150px">
        <table style="width: 100%; border: 0px none;border-collapse:collapse; " cellspacing="-1" cellpadding="-1" >
            <tr style="background-color:Teal; height: 30px; color: White; font-size: 15px;">
                <td colspan="2">
                    <table style="width: 100%;">
                        <tr>
                            <td style="width: 95%">
                                Message from KenCloud
                            </td>
                            <td>
                                <asp:Image ID="imgClose" runat="server" ImageUrl="../../images/quite.png" />
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr style="background-color: White; height: 90px">
                <td align="right">
                    <asp:Image ID="imgError" runat="server" ImageUrl="~/images/error.jpg" />
                </td>
                <td>
                    <div style="margin-left: 2%; width: 90%; font-size: 13px;">
                        <asp:Label ID="LblModalMessage" runat="server" Text=""></asp:Label></div>
                </td>
            </tr>
            <tr style="height: 30px; background-color: Teal">
                <td colspan="2" align="right">
                    <asp:Button ID="Okbtn" runat="server" Text="OK" Width="90px" Font-Size="13px" CausesValidation="false" />
                </td>
            </tr>
        </table>
    </asp:Panel>
----------------------------------------------------------------------------------------------------
Next Add one Aspx page then add this type.

<%@ Register Src="~/App_WebControls/UFMS_UserControls/U_Message.ascx" TagName="Message" TagPrefix="Mess" %> 

 // Some times it will take Problem so to add in write
 <Mess:Message ID="MessageID" runat="server" Visible="false" /> 

// if you want to retrive Parent user control id in child control and also add Usercontrol Path
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<%@ Register Src="~/App_WebControls/UFMS_UserControls/U_Message.ascx" TagName="Mess" TagPrefix="me" %>
<%@ Register Src="~/App_WebControls/UFMS_UserControls/U_VoucherSetup.ascx" TagName="Voucherset" TagPrefix="voucher" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<voucher:Voucherset ID="VouchersSetups" runat="server" />
<me:Mess ID="Meid" runat="server" />
</asp:Content>

  protected void BtnCashPaymentDisp_Click(object sender, EventArgs e)
        {

            a = TxtCashPaymentPrefix.Text;
            if (ChkCashPaymentFYear.Checked)
            {
                b = "2011-2012";
            }
            c = Convert.ToInt32(TxtCashPaymentAutoNoSize.Text);
            for (int i = 1; i < c; i++)
            {
                x += "0";
                Response.Write(x.ToString());
            }
            x = (x + "1");
            string Cashpayment = (a + "/" + b + "/" + x).ToString();
            Message(Cashpayment.ToString());
        }

//To show Popop Window
void Message(string msg)
        {           
            U_Message uc = (U_Message)Parent.FindControl("Meid");

            if (uc != null)
            {
                Label lb = (Label)uc.FindControl("LblModalMessage");
                lb.Text = msg;
                MessageID.Visible = true;
                AjaxControlToolkit.ModalPopupExtender modal = (AjaxControlToolkit.ModalPopupExtender)uc.FindControl("modalMessage");
                modal.Show();
            }
        } 


No comments:

Post a Comment