views:

117

answers:

3

Hi,

I have an asp page that renders controls based on a Request parameter. Simplified example:

<% if (Request.QueryString["personType"] == "D") { %>
    <asp:TextBox ID="TextBoxName" runat="server" Text='<%# Bind("first_name") %>' />
    <asp:TextBox ID="TextBoxSurname" runat="server" Text='<%# Bind("surname") %>' />
<% } else { %>
    <asp:TextBox ID="TextBoxName" runat="server" Text='<%# Bind("first_name") %>' />
<% } %>
<asp:RequiredFieldValidator ID="RequiredFieldValidator" runat="server"
ControlToValidate="TextBoxName" ErrorMessage="Please enter an name." />

However, the compiler and runtime are complaining that the TextBoxName is not a unque ID. But surely it is, if the output is conditional and exclusive?

I could rename them TextBoxNameA (if block) and TextBoxNameB (else block), but the odd thing is that the validator still seems to fire on TextBoxNameB, even if the validator is inside the else block and the code runs through the first block at runtime

How can I set the page to render one or the other boxes, without conflicting IDs and conflicting Validators?

Thanks for any help

Ryan

A: 

But server side, how is the parser supposed to distinguish the two?

And while parsing it will not be able to determine that one should be displayed and one shouldn't.

You need to give each server side control a different ID, regardless of it being displayed to the client.

You can restructure you code like this, as TextBoxName will always display (and with the same text):

<asp:TextBox ID="TextBoxName" runat="server" Text='<%# Bind("first_name") %>' />
<% if (Request.QueryString["personType"] == "D") { %>
    <asp:TextBox ID="TextBoxSurname" runat="server" Text='<%# Bind("surname") %>' />
<% } %>

And no more problem.

Oded
Thank you for your response. I am happy to do this, but how can I stop the validator from firing and failing? I don't really understand why the validator still fires if I move it inside the else block, but at runtime the code executes the if block. Am I missing something?Thank you
Ryan
Your validator isn't in the if block and since the name textbox is now out of it as well, there shouldn't be an issue since the validator is validating that name textbox control.
ryanulit
Ah, the odd thing (which I think lies at the root of my question) is that when putting the validator in an if block, the validator fires, even if the if block is not executed! (the error is not displayed, but I can see from debug that Page.isValid is false, as is the Validator)
Ryan
A: 

Since the Name textbox will show up regardless of the condition, you can just take that out of your if block all together and then just conditionally show the SurName textbox and that should solve your problem.

ryanulit
+1  A: 

How about moving around your logic?

    <asp:TextBox ID="TextBoxName" runat="server" Text='<%# Bind("first_name") %>' />
<% if (Request.QueryString["personType"] == "D") { %>
<asp:TextBox ID="TextBoxSurname" runat="server" Text='<%# Bind("surname") %>' />
<% } %>
<asp:RequiredFieldValidator ID="RequiredFieldValidator" runat="server"
ControlToValidate="TextBoxName" ErrorMessage="Please enter an name." />
Blounty
Thank you. But there are many controls that are rendered conditionally and I wanted to limit the number of if/else blocks. So I wantif (A) TextBoxA1 TextBoxA2 ValidatorAelse TextBoxB2 TextBoxB3 TextBoxB4 TextBoxB5 ValidatorBDo you see what I mean?
Ryan