Last post Aug 12, 2010 12:19 AM by jkirkerx
Aug 11, 2010 07:42 AM|AkoKamangar|LINK
Hi, I am creating a custom server control in which I place a RequiredFieldValidator. In the the Render method and I want to dynamically create a RequiredFieldValidator and render it to the HtmlTextWriter.
How can I completely render a RequiredFieldValidator to an HtmlTextWriter?
Thanks in advance.
Aug 11, 2010 08:46 AM|Bigtrend|LINK
Why can't you add RequiredFieldValidator into the Controls collection of your custom server control?
Aug 11, 2010 09:17 AM|AkoKamangar|LINK
Well, I can't do that, because my control inherits from DropDownList and in the Render method, I generate some more stuff than a regular DropDownList, and why I do inherit from DropDownList is another story, because I have tried a dozen other approaches
and could't maintain the selected choice of DropDownList after PostBack.
So, I create the RequiredFieldValidator dynamically and render it to the HtmlTextWriter.
I hope I am clear!
Aug 12, 2010 12:19 AM|jkirkerx|LINK
A RFV for a DropDownList is a little more complicated that that.
You have to create the DDL in Protected Overrides OnInit(),
When you create the rfv, it has to be created right after you create the DDL
You have to use the property DefaultValue="--" or something for the RFV to know that the first value is a select statement like choose
Set your validation group so they are the same.
You can load up the DDL in the Page_Load Cycle, or Protected Overrides Sub OnLoad() with data, and it will stick.
ddl.add(New ListItem("Select", "--"))
ddl.add(New ListItem("Blue", "Blue")