asp.net - 如何在 asp.net 网页中实现 html 表单 - asp.net 中的两种表单问题

标签 asp.net html ajax forms

请看这段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 页做些什么呢?

提前致谢

最佳答案

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

你的选择。

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

因为在这里我看到你发布到 php,我认为你喜欢用 asp.net 保留旧代码,在 asp.net 表单的结束表单之后添加它们。

<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 - 如何在 asp.net 网页中实现 html 表单 - asp.net 中的两种表单问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10242520/

相关文章:

asp.net - Azure Web App - 限制对 API 管理的访问

html - 如何使 "div"按钮提交其所在的表单?

html - 悬停时图片显示文本为 "rollover"

javascript - 如果文本框不包含数据则隐藏文本框

javascript - 输入名称后自动完成表单 PHP

javascript - Opera 在第一次加载页面时发出两次 ajax 请求

c# - 具有单个容器的 Autofac 多个生命周期范围

c# - 根据 future 日期自动删除 Azure 中的 Blob 文件

asp.net - 图像在 VS 设计模式中显示,但在浏览器中不显示

javascript - 如何在页面加载时带来带有所选值的下拉菜单(选择)。?