2010-08-25 183 views
0

顯然每頁只能有一個form runat="server"ASP.NET中的兩種形式

我的頁面有一個窗體,它載入一個名稱列表。這個表格也允許你在列表中添加一個新名字。

我已經在列表視圖中爲每個名稱指定了一個onclick事件。當你點擊它時,我需要用JavaScript代碼將數據加載到編輯表單(在添加表單旁邊);我可以做到這一點。

但是,我如何在頁面上構造它有兩種形式?

一個例證:

<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> 

回答

1

ASP.NET Web Forms作品在一個頁面上只有一個<form>元素,並獲得該回發到同一頁,每次有新的變化(回傳)。嘗試使用多個表單,並在表單元素上指定自定義action屬性違背了框架設計用於處理的內容,而這絕對不是一個好主意。

我只想嘗試擺脫第二個<form>元素,並從第一個<form>中刪除action屬性。此外,如果所有內容都在表單內,即ASP.NET頁面頂部的<table>標籤,ASP.NET將會更加快樂。

我不知道你的網頁是幹什麼的,但如果你已經有了一個TextBox和你使用了這樣的內容項添加到ListBox,更加以網絡 窗體類的方法是當TextBox已被填寫時,使用一些控件進行回發,然後將ListBox重新綁定到某種數據源。如果您想要Ajax回發,也許使用UpdatePanel

如果你更熟悉JavaScript和查詢字符串參數,也許ASP.NET MVC會更適合。

0

通過我的理解你的解釋,你需要一個功能在你的這個頁面上,當你點擊列表中的一個listitem時,form2中的元素需要顯示並修改細節......我認爲是...使用兩個面板和onclick ListBox中的項目,您可以顯示編輯面板和更改細節和其他事件,如按鈕單擊..您可以顯示列表框面板更改的細節。

是的,因爲格雷厄姆已經提到你可以使用Ajax更新面板來實現這一點。