如何发布和验证“命名”在C#中输入?
我需要用 C# 将表单发布到第三方 URL。输入字段的命名必须符合第三方的期望(在本例中为“电子邮件地址”等)。如果我使用标准的“输入”标签,我没有很好的方法来验证控件。如果我像本示例中那样使用“asp:TextBox”,它将重命名我的字段并搞砸我的帖子。帮助!
<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" />
所以上面的结果如下。我需要名称为“电子邮件地址”,或者我需要一种好方法来验证输入字段(此示例经过简化,有更多字段和更多正则表达式验证)。
<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("ctl00$ContentPlaceHolder1$ctl00", "", true, "", "http://thirdpartyurl.com", false, false))" value="Submit" name="ctl00$ContentPlaceHolder1$ctl00">
</div>
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("ctl00$ContentPlaceHolder1$ctl00", "", true, "", "http://thirdpartyurl.com", false, false))" value="Submit" name="ctl00$ContentPlaceHolder1$ctl00">
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,尝试将 ClientIDMode 设置为静态。我知道这将使 ID 保持静态,但不确定名称属性。
您也可以尝试在不使用 asp:TextBox 的情况下保留您的输入,但无论如何都将 runat="server" 放在上面,看看它是否保留您的名字。像这样的事情:
顺便说一下,为什么你的变量名中有空格?不会计算。
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:
By the way, why would you have spaces in your variable name?? Does not compute.
如果您直接从表单发布到第三方而不发回服务器,那么传统 ASP.NET Web 表单中最简单的方法是创建表单,然后将其操作显式设置为第三方 URL 并使用普通的旧式基本 HTML,带有标签来介绍内容。然后,您引入 JavaScript 来验证客户端的字段。
如果您发回您的服务器,然后发布到第三方,则没有任何问题,因为这样您就可以按照您喜欢的方式格式化您的帖子。
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.