ASP.NET中的两种形式 [英] Two forms in ASP.NET

查看:90
本文介绍了ASP.NET中的两种形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然,每页只能有一个 form runat ="server" .

You can only have one form runat="server" per page apparently.

我的页面有一种形式,它会在其中装载一个名称列表.此表单还允许您向列表中添加新名称.

My page has one form, where it loads in a list of names. This form allows you to add a new name to the list as well.

我已经为列表视图中的每个名称分配了一个onclick事件.当您单击它时,我需要它使用JavaScript代码将数据加载到编辑表单(添加表单旁边)中;我可以做到的.

I've attatched an onclick event to each name in the listview. When you click on it, I need it to load the data into the edit form (next to the add form) with JavaScript code; I can do this fine.

但是我如何在页面上将其构造为具有两种形式?

But how do I structure it on the page to have two forms?

说明:

<table>
    <tr>
        <td style="width:50%;" valign="top">

            <form runat="server" action="productCats.aspx?action=new&mid=2">
                <div class="subHead">Create New Category</div>
                <table class="settingTable">
                    <tr>
                        <td colspan="2"><b>Category Name</b></td>
                    </tr>
                    <tr>
                        <td>
                            <asp:TextBox ID="catName" runat="server" CssClass="tbox widebox"></asp:TextBox>
                            <asp:RequiredFieldValidator runat="server"
                                      id="ValidatorName"
                                      ControlToValidate="catName"
                                      ErrorMessage="You need to enter a category name"
                                      display="Dynamic" />
                        </td>
                    </tr>
                    <tr>
                        <td>This is the name of your category.</td>
                    </tr>
                    <tr>
                        <td colspan="2"><b>Parent Category</b></td>
                    </tr>
                    <tr>
                        <td>
                            <asp:ListBox SelectionMode="Single" Rows="8" id="parent" runat="server" CssClass="tbox widebox">
                                <asp:ListItem Selected="True" Text="Top Level" Value="0"></asp:ListItem>
                            </asp:ListBox>
                            <asp:RequiredFieldValidator runat="server"
                                      id="RequiredFieldValidator1"
                                      ControlToValidate="parent"
                                      ErrorMessage="You need to select a parent"
                                      display="Dynamic" />
                        </td>
                    </tr>
                    <tr>
                        <td>Choose a parent this category belongs to.</td>
                    </tr>
                </table>
                <asp:Button id="id" text="Create" runat="server" />
            </form>
        </td>
        <td style="width:4%;">
        </td>
        <td valign="top">
        <div class="subHead">Modify Category</div>

            <form id="Form1" action="productCats.aspx?action=update&mid=2">
                <table class="settingTable">
                    <tr>
                        <td colspan="2"><b>Category Name</b></td>
                    </tr>
                    <tr>
                        <td>
                            <asp:TextBox ID="newCatName" runat="server" Enabled="false" CssClass="tbox widebox"></asp:TextBox>
                            <asp:RequiredFieldValidator runat="server"
                                      id="RequiredFieldValidator2"
                                      ControlToValidate="newCatName"
                                      ErrorMessage="Enter a new category name"
                                      display="Dynamic" />
                        </td>
                    </tr>
                </table>
            </form>
        </td>
    </tr>
</table>

推荐答案

ASP.NET Web表单的工作方式是,在页面上仅包含一个< form> 元素,并在每次更改(回发)时将其发布回同一页面.尝试使用多种形式,并在form元素上指定自定义 action 属性与该框架设计要使用的框架背道而驰,这从来都不是一个好主意.

ASP.NET Web Forms works by having just one <form> element on a page, and getting this posted back to the same page every time something changes (postbacks). Trying to use multiple forms, and specifying custom action attributes on the form element is going against what the framework is designed to work with, and that's never really a good idea.

我只是尝试摆脱第二个< form> 元素,并从第一个< form> 中删除 action 属性.此外,如果所有内容都位于表单内,即页面顶部的< table> 标记内,则ASP.NET将更加快乐.

I would just try to get rid of the second <form> element, and remove the action attribute from the first <form>. Also, ASP.NET will be much happier if everything is inside the form, i.e. your <table> tags at the top of the page.

我不确定您的页面在做什么,但是如果您有 TextBox ,并且您正在使用此内容将内容添加到 ListBox ,更像Web表单的方法是在填充 TextBox 后使用某种控件进行回发,然后将 ListBox 重新绑定到某种数据源.如果您想要Ajax回发,也许使用 UpdatePanel .

I'm not sure what your page is doing, but if you've got a TextBox and you're using the contents of this to add items to a ListBox, a more Web Forms-like approach would be to use some control to do a postback when the TextBox has been filled in, and then re-bind the ListBox to some kind of data source. Maybe use an UpdatePanel if you want an Ajax postback.

如果您对JavaScript和查询字符串参数更满意,也许ASP.NET MVC会更合适.

If you're more comfortable with JavaScript and query string parameters, maybe ASP.NET MVC would be a better fit.

这篇关于ASP.NET中的两种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆