i am creating a custom control for the textbox.Let say when the textbox's action is edit , the textbox will render 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 suggestion would be to set some code checking the custTextBox1.Action property. When View set RequiredFieldValidator1.Enabled = false else Enabled = true.
Remember to mark as answer if this post answered or solved your problem.
Hi, did u got other better suggestion?because i may have many RequiredFieldValidator1 in my webform application. It causing me alot of coding. Btw thanks for your suggesion
That was the quick and dirty suggestion, the more correct suggestion would be to build it into your custom textbox control. This would involve quite a bit of coding as well but would only be one time. In there you can check to see if your Action property
is set to and add or enable/disable the validator there.
Remember to mark as answer if this post answered or solved your problem.
Otherwise you can create your custom validator control, overriding the render method to check the condition, and using it insted of normal RequiredFieldValidator.
custom
Marco Cavallo
WebMaster & Programmer
Il mio sito in continua evoluzione...
Vieni a trovarmi!
http://www.artcava.net/
In my mind,I think if you only want to do validations to check whether the TextBox is empty or not,Just Do checks in the RenderControl by checking the Text property inherited from TextBox control,Something like this following:
if (Action ==gbcCommandVariable.CommandEdit)
{
base.RenderControl(writer);
if(Text=="")
{
//Do what you want here....
}
}
koolll
Member
38 Points
39 Posts
Custom control and required field validator
Jan 05, 2012 05:41 PM|LINK
i am creating a custom control for the textbox.Let say when the textbox's action is edit , the textbox will render 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)); } } }When the textbox render as label the required field validatior will always return error,even the label is not a emtpy string .Please help ugentb471code3
Star
13877 Points
2598 Posts
Re: Custom control and required field validator
Jan 05, 2012 06:40 PM|LINK
My suggestion would be to set some code checking the custTextBox1.Action property. When View set RequiredFieldValidator1.Enabled = false else Enabled = true.
koolll
Member
38 Points
39 Posts
Re: Custom control and required field validator
Jan 06, 2012 06:25 AM|LINK
Hi, did u got other better suggestion?because i may have many RequiredFieldValidator1 in my webform application. It causing me alot of coding. Btw thanks for your suggesion
b471code3
Star
13877 Points
2598 Posts
Re: Custom control and required field validator
Jan 06, 2012 01:20 PM|LINK
That was the quick and dirty suggestion, the more correct suggestion would be to build it into your custom textbox control. This would involve quite a bit of coding as well but would only be one time. In there you can check to see if your Action property is set to and add or enable/disable the validator there.
ArtCava
Member
18 Points
9 Posts
Re: Custom control and required field validator
Jan 06, 2012 09:37 PM|LINK
Otherwise you can create your custom validator control, overriding the render method to check the condition, and using it insted of normal RequiredFieldValidator.
custom
WebMaster & Programmer
Il mio sito in continua evoluzione...
Vieni a trovarmi!
http://www.artcava.net/
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Custom control and required field validator
Jan 07, 2012 01:27 AM|LINK
Hello koolll:)
In my mind,I think if you only want to do validations to check whether the TextBox is empty or not,Just Do checks in the RenderControl by checking the Text property inherited from TextBox control,Something like this following:
if (Action ==gbcCommandVariable.CommandEdit) { base.RenderControl(writer); if(Text=="") { //Do what you want here.... } }