views:

945

answers:

2

Hi,

I have created a custom TextBox control which also contains a RequiredFieldValidator. Everything works fine but the problem is in display. The display is something like this:

[TextBox Control] [Validation Error Message]

I want the display to be something like this:

[Validation Error Messsage] [TextBox Control]

(the validation error message should be at the top of the textbox)

My control inherits from the TextBox control. How can I create the display like above. I tried to use the Table control and insert TextBox and Validation control inside the Table but it gave me some sort of stack over flow error.

Here is the code:

protected override void Render(System.Web.UI.HtmlTextWriter writer) { base.Render(writer);

        if(_req != null)
        _req.RenderControl(writer);
    }
A: 

Here is the ugly solution:

protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
           if (_req != null)
           {
               writer.Write("<div style='float:top;'>");
               _req.RenderControl(writer);
               writer.Write("</div>");
           }

           base.Render(writer);
        }
azamsharp
technically float top isn't really doing anything. the reason this works is because you're calling base.Render after _req.RenderControl as opposed to before.
bendewey
the reason its displaying on top is because the default behavior of div is display:block;
bendewey
finally its okay to post your own answer to the question, but you should not post updates, like code samples as answers, you should edit your original question to include that. can you update your question and then delete the first answer?
bendewey
sure I will do that! Thanks!
azamsharp
sorry I didn't mean to be confusing, the bad code should go in the question and then the solution code will be down here in the answer. This way this question looks nice for people who look later. Also you will want to make this as the accepted answer.
bendewey
Currently I am not able to accept my own answer. It says wait for 48 hours!
azamsharp
oh I didn't realize, Can you provide the bad code that was here previously in the question?
bendewey
ohh boy!! now I am really confused!
azamsharp
This is perfect, thanks. Sorry to be picky. Now you have a nice clean question with a nice clean answer. Anyone who ever has the same problem will no exactly how to fix it.
bendewey
@azamsharp - it's ok to accept your answer by clicking on the checkmark. In that way it will not show up in the system as an *unanswered* question.
ichiban
+2  A: 

You're really close to doing it right! Try this:

protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
    if(_req != null)
        _req.RenderControl(writer);
    base.Render(writer);
}

That will render the validation control before your TextBox instead of afterwards. If you want to change the appearance further then css is probably the way to go.

teedyay