混合控制Asp.net

时间:2011-10-14 07:22:03

标签: asp.net

通过在login.cs文件中动态添加html控件来创建混合控件

login.cs

的代码
namespace WebApplication6
{
    public class LoginStandards: CompositeControl
    {
        private TextBox txtusername;
        private TextBox txtpassword;
    public string UserName
    {
        get
        {
            EnsureChildControls();
            return txtusername.Text;
        }
        set
        {
            EnsureChildControls();
            txtpassword.Text = value;
        }
    }

    public string Password
    {
        get
        {
            EnsureChildControls();
            return txtpassword.Text;
        }
        set
        {
            EnsureChildControls();
            txtpassword.Text = value;
        }
    }

    protected override void CreateChildControls()
    {
        txtusername = new TextBox();
        txtusername.ID = "txtusername";
        this.Controls.Add(txtusername);

        txtpassword = new TextBox();
        txtpassword.ID = "txtPassword";
        txtpassword.TextMode = TextBoxMode.Password;
        this.Controls.Add(txtpassword);
    }

    protected override void RenderContents(HtmlTextWriter writer)
    {
        writer.AddStyleAttribute("float", "left");
        writer.RenderBeginTag(HtmlTextWriterTag.Div);
        writer.AddStyleAttribute(HtmlTextWriterStyle.Padding, "3px");
        writer.RenderBeginTag(HtmlTextWriterTag.Div);
        writer.AddAttribute(HtmlTextWriterAttribute.For, txtusername.ClientID);
        writer.RenderBeginTag(HtmlTextWriterTag.Label);
        writer.Write("User Name:");
        writer.RenderEndTag();
        writer.RenderEndTag();

        writer.AddStyleAttribute(HtmlTextWriterStyle.Padding, "3px");
        writer.RenderBeginTag(HtmlTextWriterTag.Div);
        writer.AddAttribute(HtmlTextWriterAttribute.For, txtpassword.ClientID);
        writer.RenderBeginTag(HtmlTextWriterTag.Label);
        writer.Write("Password:");
        writer.RenderEndTag();
        writer.RenderEndTag();
        writer.RenderEndTag();

        writer.AddStyleAttribute("float", "left");
        writer.RenderBeginTag(HtmlTextWriterTag.Div);
        writer.AddStyleAttribute(HtmlTextWriterStyle.Padding, "3px");
        writer.RenderBeginTag(HtmlTextWriterTag.Div);
        txtusername.RenderControl(writer);
        writer.RenderEndTag();

        writer.AddStyleAttribute(HtmlTextWriterStyle.Padding, "3px");
        writer.RenderBeginTag(HtmlTextWriterTag.Div);
        txtpassword.RenderControl(writer);
        writer.RenderEndTag();
        writer.RenderEndTag();
        writer.Write("<br style=’clear:left’ />");
    }

        protected override HtmlTextWriterTag TagKey
        {
            get
                {
                return HtmlTextWriterTag.Div;
                }
            }
}
}

我想在正常的.aspx页面中使用这个cs文件..我通过创建一个对象来做..但它不起作用.. 如何在.aspx页面中调用login.cs文件。任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

你有register控件前缀吗?

<%@ Register tagprefix="My"
   namespace="WebApplication6"
   assembly="WebApplication6" %>

然后你可以使用:

<my:LoginStandards runat="server" />
你代码中的