How To Post and Validate "Named" Input in C#?
I need to post a form to a 3rd party URL in C#. The input fields must be named to match what the third party is expecting (in this case "Email Address", etc). If I use a standard "input" tag I don't have a great way to validate the control. If I use an "asp:TextBox" like in this example, it will rename my field and screw up my post. HELP!
<asp:TextBox id="CustomerEmailInput" runat="server"
name="Email Address"
CssClass="tb5a" >
</asp:TextBox>
<asp:RequiredFieldValidator ID="customerEmailRequired" runat="server"
ControlToValidate="CustomerEmailInput"
display="Dynamic"
ErrorMessage="*">
</asp:RequiredFieldValidator>
<asp:Button runat="server"
PostBackUrl="http://ThirdPartyUrl.com"
Text="Submit" />
So the above renders the following. I need the name to be "Email Address", or I need a good way to validate the input field (this example is simplified, there are more fields and more regex validations).
<div>
<input type="text" name="ctl00$ContentPlaceHolder1$CustomerEmailInput" class="tb5a" id="ctl00_ContentPlaceHolder1_CustomerEmailInput">
<span style="color: Red; display: none;" id="ctl00_ContentPlaceHolder1_customerEmailRequired">*</span>
<input type="submit" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl开发者_StackOverflow中文版00$ContentPlaceHolder1$ctl00", "", true, "", "http://thirdpartyurl.com", false, false))" value="Submit" name="ctl00$ContentPlaceHolder1$ctl00">
</div>
First, try setting ClientIDMode to static. I know that will keep the ID static, but not sure about the name attribute.
You can also try to keep your input without using asp:TextBox, but put runat="server" on it anyway and see if it keeps your name. Something like this:
<input type="text" name="EmailAddress" id="EmailAddress" runat="server" />
By the way, why would you have spaces in your variable name?? Does not compute.
If you are posting straight from your form to the 3rd party without posting back to your server, then the simplest approach in traditional asp.net web forms would be to create your form, and then set it's action to the third party URL explicitly and use plain old basic HTML with tags to introduce the content. Then, you introduce javascript to validate the fields on the client side.
If you post back to your server, and then post to the third party, it's no problem whatsoever because then you can format your post any way you like.
精彩评论