简体   繁体   中英

Custom control and required field validator

i am creating a custom control for the textbox.Let say when the textbox's action is edit , the textbox will render same as textbox .On the other hand , when the textbox is action is view , then textbox will render as label.But now i got a problem , how can i validate my textbox with the required field validator when the textbox's action is edit?Here is my coding

[ToolboxData(@"<{0}:cusTextBox runat=""server"" Action=""Edit"" />")]

public class cusTextBox:TextBox
{
    public string Action
    {
        get
        {
            String s = (String)ViewState["Action"];
            return ((s == null) ? String.Empty : s);
        }
        set
        {
            ViewState["Action"] = value;
        }
    }

    public override void RenderControl(HtmlTextWriter writer)
    {
        if (Action ==gbcCommandVariable.CommandEdit)
        {
            base.RenderControl(writer);

        }

        else if (Action == gbcCommandVariable.CommandView)
        {

            writer.Write(String.Format(@"<span ID=""{0}"" style=""display:inline-block; white-space:inherit"" >{1}</span> ", this.ClientID, this.Text));
        }
    }

}

My front end

             <cc1:cusTextBox ID="cusTextBox1" runat="server" Action="View" ></cc1:cusTextBox>
             <asp:Button ID="Button1" runat="server" onclick="Button1_Click1" 
                 Text="Button" />
             <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                 ControlToValidate="cusTextBox1" 
    ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>

Please help me .Urgent.THanks

maybe

   [ValidationProperty("Text")]
   public class cusTextBox:TextBox

attribute helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM