【问题标题】:how implement an html form inside an asp.net web page - two forms issue in asp.net如何在 asp.net 网页中实现 html 表单 - asp.net 中的两个表单问题
【发布时间】:2012-05-01 19:41:00
【问题描述】:

请看这个 html 代码:

<!--   PAGE - 5  -->
                <div class="section cc_content_5" id="page5" style="display: none;">
                    <div class="pad_content">
                        <div class="right grid_1">
                            <h1>
                                Contact Address
                            </h1>
                            <img src="Images/photo_5.jpg" alt=""><br />
                            <br />
                            <strong>The Companyname Inc</strong> 8901 Marmora Road,<br />
                            Glasgow, D04 89GR.<br />
                            Telephone: +1 800 123 1234<br />
                            Fax: +1 800 123 1234<br />
                            E-mail: <a href="mailto:#">www.copanyname.com</a><br />
                        </div>
                        <div class="left grid_2">
                            <h1>
                                Contact Form
                            </h1>
                            <div id="note">
                            </div>
                            <div id="fields">
                                <form id="ajax-contact-form" action="javascript:alert('success!');">
                                <label>
                                    Name:</label><input class="textbox" name="name" value="" type="text">
                                <label>
                                    E-Mail:</label><input class="textbox" name="email" value="" type="text">
                                <label>
                                    Subject:</label><input class="textbox" name="subject" value="" type="text">
                                <label>
                                    Comments:</label><textarea class="textbox" name="message" rows="5" cols="25"></textarea>
                                <label>
                                    Captcha</label><img src="Images/captcha.php" style="margin: 3px 0px; width: 200px;
                                        height: 32px;">
                                <div class=" clear">
                                </div>
                                <label>
                                    &nbsp;</label><input class="textbox" name="capthca" value="" type="text">
                                <div class=" clear">
                                </div>
                                <label>
                                    &nbsp;</label><input class="pin" name="submit" value="Submit" type="submit">
                                </form>
                                <div class="clear">
                                </div>
                            </div>
                        </div>
                        <div class="clear">
                        </div>
                    </div>
                </div>

和他们的脚本:

<script type="text/javascript">
    $(document).ready(function () {
        $("#ajax-contact-form").submit(function () {
            var str = $(this).serialize(); $.ajax({ type: "POST", url: "contact.php", data: str, success: function (msg) {
                if (msg == 'OK') // Message Sent? Show the 'Thank You' message and hide the form
                { result = '<div class="notification_ok">Your message has been sent. Thank you!<br /> <a href="#" onclick="freset();return false;">send another mail</a></div>'; $("#fields").hide(); } else
                { result = msg; } $("#note").html(result);
            }
            }); return false;
        });
    });
    function freset()
    { $("#note").html(''); document.getElementById('ajax-contact-form').reset(); $("#fields").show(); }
</script>

我真的很想知道如何将第 5 页放入我的 asp.net 网络表单中?
正如您一样,我们不能在默认的 asp.net 网页表单中使用表单。
那么我可以对第 5 页做些什么呢?

提前致谢

【问题讨论】:

  • 最简单的使用方法是尝试使用webmethod并从codebehind中获取值并与数据库交互以提交数据。

标签: asp.net html ajax forms


【解决方案1】:

你不能在一个 asp.net 页面中拥有两个表单,并且都在后面的代码上工作。

你有的选择。

  1. 将第二个表单放在 asp.net 表单之前或之后
  2. 不要设置第二种形式,只需在后面的代码中读取您感兴趣的输入数据。
  3. 使用 javascript 在页面末尾动态创建表单并发布。

因为在这里我看到你发布到 php 并且我认为你喜欢用 asp.net 保留旧代码,add them after the closing form of the asp.net form.

<form id="AspForm" runat="server">
...
</form>
<form id="ajax-contact-form"  method="post" action="contact.php" >
...
</form>

更极端

因为你们都准备好使用 javascript,所以你们可以做到这一点。将所有内容复制并粘贴到页面末尾的表单中并发布。

    <form id="AspForm" runat="server">
    ...
      <div id="ajax-contact-infos" >
       <label>
          Name:</label><input class="textbox" name="name" value="" type="text">
       <label>
       <input class="pin" name="submit" value="Submit" 
         type="button" onclick="SubmitThisForm();return false;" >
        ...
      </div>
    </form>
    <form id="ajax-contact-form"  method="post" action="contact.php" >
       <div id="PlaceOnMe" ></div>
    </form>

在 javascript 上复制它,然后发布为

<script type="text/javascript">
    function SubmitThisForm()
    {
            // here I copy (doublicate) the content of the data that I like to post
            //  in the form at the end of the page and outside the asp.net
        jQuery("#PlaceOnMe").html(jQuery("#ajax-contact-infos"));
            // Now we submit it as its your old code, with out change anything
        jQuery("#ajax-contact-form").submit();
    }

    // here is the old code.
    $(document).ready(function () {
        $("#ajax-contact-form").submit(function () {
            var str = $(this).serialize(); $.ajax({ type: "POST", url: "contact.php", data: str, success: function (msg) {
                if (msg == 'OK') // Message Sent? Show the 'Thank You' message and hide the form
                { result = '<div class="notification_ok">Your message has been sent. Thank you!<br /> <a href="#" onclick="freset();return false;">send another mail</a></div>'; $("#fields").hide(); } else
                { result = msg; } $("#note").html(result);
            }
            }); return false;
        });
    });
    function freset()
    { $("#note").html(''); document.getElementById('ajax-contact-form').reset(); $("#fields").show(); }
</script>

所有这些都是最小的变化。您可以进行优化,但这是一般的想法。

【讨论】:

  • 感谢回答/我什么时候应该在 asp.net 表单之后动态添加第二个表单?
  • +1 是一个很好的解决方案 - 只有当您希望表单位于您的 asp.net 页面上的特定位置时才会如此痛苦。
  • @dash 他可以使用 javascript 在帖子之前移动它。所有这些都是将其发布到 php 的工作。
  • 当我把这个 jquery "jQuery("#PlaceOnMe").html(jQuery("#ajax-contact-infos"));"提交按钮的点击事件?
  • @SilverLight 我再次更新了答案。查看新的更新。我希望这项工作的错误最小,因为我无法对其进行测试。
【解决方案2】:

您有多种选择;嵌套表单是最糟糕的,因为它会/不会根据浏览器工作。

正如@Aristos 所提到的,您可以采用一种又一种的形式。这是一个很好的解决方案,但就您的网站设计而言不一定是最好的。

第二个是您可以将联系表单嵌入到普通的 HTML 页面中,然后将其包含在 iframe 或类似的父页面中。这可以解决嵌套表单的问题,但可能无法在用户体验方面为您提供您想要的行为。

第三个是您的联系表格更适合成为User Control

您可以将所有联系表单字段放入 ascx 文件中,并在此控件中处理按钮单击以进行提交。处理此提交后,您可以将用户控件替换为感谢用户输入的简单消息。

你也可以把所有需要的表单验证都放在这个里面。您的 ASCX 可能类似于:

<asp:Panel runat="server" id="ContactPanel">
<div class="left grid_2">
        <h1>
            Contact Form
        </h1>
        <div id="note">
        </div>
        <div id="fields">
            <label for="<%=InputContactName.ClientID%>">Name:</label>
            <asp:TextBox runat="server" id="InputContactName" value="" />
            <!-- Rest of Form goes here -->
            <div class=" clear">
            </div>
            <asp:Button runat="server" id="ContactFormSubmit" OnClick="ContactFormSubmit_Click" Text="Send Feedback" />
            <label>&nbsp;</label><input class="pin" name="submit" value="Submit" type="submit">
            <div class="clear"></div>
        </div>
    </div>
</div>
</asp:Panel>
<asp:Panel runat="server" id="ThanksPanel" Visible="false">
    <p>Thank you for your feedback. We'll try not to ignore it. But it is Friday. So we might. At least until Monday.</p>
</asp:Panel>

在您的 ContactFormSubmit_Click 按钮中,您可以从联系表单中获取文本,隐藏 ContactPanel,并显示ThanksPanel。

非常简单,并且封装在用户控件中。

【讨论】:

    猜你喜欢
    • 2015-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    • 1970-01-01
    相关资源
    最近更新 更多