如何通过代码更改C#.NET中的表单操作?

时间:2016-07-14 00:52:25

标签: c# asp.net visual-studio-2013

我正在使用C#.NET中的一个项目,它只允许我在.aspx文件中使用一个表单。

<form id="form1" runat="server" action="#1">

如何通过方法中的C#代码更改表单操作? 我试过这个:

protected void Button1_Click(object sender, EventArgs e)
{
    form1.Action = "#2";
}

但它没有用。提前谢谢......

3 个答案:

答案 0 :(得分:0)

(将其移至顶部,因为它是对新理解的问题的回答。不是如何更改表单操作,而是如何使用多个表单。)

如果您想要一个已经有服务器表单的页面上的服务器表单,那么第二个“表单”可能应该是User Control。这样它就位于主机页面的服务器表单中,但不需要自己的表单。它是独立的,能够包含处理回发时所需的任何逻辑。

以下是简单用户控件的示例。您可以从添加&gt;创建一个;新商品&gt;网络&gt; Web窗体用户控件。

<%@ Control Language="C#" AutoEventWireup="true" 
    CodeBehind="OtherForm.ascx.cs" Inherits="WebApplication1.OtherForm" %>
<label for="<% = OtherFormTextInput.ClientID %>">
    This is some other form on the same page
</label>
<asp:TextBox runat="server" ID="OtherFormTextInput"></asp:TextBox>
<asp:Button runat="server" ID="Submit" Text="Submit this other form"/>

它看起来像.aspx页面,但它没有任何形式。它仍然可以拥有自己的代码,可以与其包含的其他服务器控件进行交互,就像.aspx页面一样。

然后将该控件添加到“主”页面:

<%@ Page Language="C#" AutoEventWireup="true" 
    CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!-- Register the user control -->
<%@ Register TagPrefix="uc" TagName="other" Src="~/OtherForm.ascx" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <!-- This form has its own stuff, but also contains the "other" form. -->
        <uc:other runat="server" ID="TheOtherForm"></uc:other>

    </form>
</body>
</html>

我建议使用面板(仍然可以使用),因为如果您在一个页面上放置两个表单,则可能在某些时候您可能希望将辅助表单移动到另一个页面或重新使用它。这种方法使其完全独立。它的代码隐藏与.aspx页面不在同一个文件中。您可以根据需要将其放在任意数量的页面上。

原始的“字面”答案,解决了最初理解的问题。

runat="server"表单的存在完全是为了允许ASP.NET在回发期间与页面及其服务器控件进行交互。它是webforms工作方式的核心。如果您更改操作,那么从技术上讲,您拥有的不再是webforms页面。

那很好(我甚至不喜欢webforms)但它可能导致一些奇怪的行为。如果您有触发回发的控件,那么通常它们将在同一页面上处理,您的用户只会看到(希望)快速刷新。现在他们可能会被发送到另一个页面。

如果您完全删除了该表单并添加了自己的表单,该怎么办?那么你的.aspx页面就会更像.html页面。

添加了所有免责声明,说明为什么不这样做,您可以使用JavaScript更改操作。这是一个示例:

<%@ Page Language="C#" AutoEventWireup="true" 
    CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server" ClientIDMode="Static">
        <asp:Button runat="server" text="Causes postback"/>
        <asp:CheckBox runat="server" AutoPostBack="True"/>
    </form>

    <script>
        document.getElementById("form1").action = "http://stackoverflow.com";
    </script>
</body>
</html>

某些浏览器可能不允许更改表单的操作。

我把复选框放在那里只是为了好玩(我一定很无聊)来展示它可能有的奇怪的副作用,你可以点击一个复选框并重定向到另一个页面。

答案 1 :(得分:0)

基于对您的评论问题。 asp:Panel控制可以帮助你。

一个非常粗略的例子

ASPX:

<form id="form1" runat="server">
    <asp:Panel id="Form1" runat="server">
        <!-- Form 1 Stuff -- >
        <asp:Button ID="Button1" runat="server" Text="Submit" 
           OnClick="Button1_Click" />
    </panel>
     <asp:Panel id="Form2" runat="server" Visible="false">
        <!-- Form 2 Stuff -- >
        <asp:Button ID="Button2" runat="server" Text="Submit" 
           OnClick="Button2_Click" />
    </panel>
</form>

C#

protected void Button1_Click(object sender, EventArgs e)
{
    //HIde "Form"1
    Form1.Visible = false;
    //Show "Form"2
    Form2.Visible = true;
    //Do other stuff
}

protected void Button2_Click(object sender, EventArgs e)
{
    //Do Final Processig
}

另请查看Panel的DefaultButton属性

答案 2 :(得分:0)

您可以在Asp.net端编写一个response.write()来打印一些javascript或jQuery代码!正如@Scott Hannen写了一些像这样的javascript:

Response.Write("<script>document.getElementById('YOURFORMID').action = 'YOUR URL';</script>");

或使用jQuery

Response.Write("<script>$('#YOUR FORM ID').attr('action', 'YOUR URL');</script>");

顺便说一句,如果你有权访问.html或.js文件,你可以直接把这个jQuery代码放在没有任何C#代码的地方!