如何发布和验证“命名”在C#中输入?

发布于 2024-10-28 06:22:07 字数 1390 浏览 1 评论 0原文

我需要用 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(&quot;ctl00$ContentPlaceHolder1$ctl00&quot;, &quot;&quot;, true, &quot;&quot;, &quot;http://thirdpartyurl.com&quot;, 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你げ笑在眉眼 2024-11-04 06:22:07

首先,尝试将 ClientIDMode 设置为静态。我知道这将使 ID 保持静态,但不确定名称属性。

您也可以尝试在不使用 asp:TextBox 的情况下保留您的输入,但无论如何都将 runat="server" 放在上面,看看它是否保留您的名字。像这样的事情:

<input type="text" name="EmailAddress" id="EmailAddress" 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:

<input type="text" name="EmailAddress" id="EmailAddress" runat="server" />

By the way, why would you have spaces in your variable name?? Does not compute.

悲歌长辞 2024-11-04 06:22:07

如果您直接从表单发布到第三方而不发回服务器,那么传统 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文