简体   繁体   English

如何将 Asp.NET 按钮与代码隐藏链接?

[英]How to link Asp.NET Button with Code-behind?

I am working on an ASP.NET page with a few different bits of functionality.我正在开发一个具有一些不同功能位的 ASP.NET 页面。 My first button works just fine, and looks like this:我的第一个按钮工作得很好,看起来像这样:

<form method="post">
    <h2>View an Existing Reservation</h2>
   <p><sub>Or just enter your email to recover your reservations</sub></p>
   <div>
       <div class="form-row">
            <div class="col-lg-6 col-md-6 col-sm-12">
                  <label for="name">Reservation ID:</label>
                  <asp:TextBox runat="server" ID="ID" Name="ID"  CssClass="form-control"></asp:TextBox><br />
            </div>
            <div class="col-lg-6 col-md-6 col-sm-12">
                  <label for="email">Email</label>
                  <asp:TextBox runat="server" ID="Email" TextMode="Email" CssClass="form-control"></asp:TextBox><br />
            </div>
      </div>
      <div class="form-row justify-content-around">
            <asp:Button ID="Button2" Text="Edit" onclick="Button2_Click" runat="server" CssClass="btn btn-primary"/>

      </div>
</div>

</form>

with the code behind of:后面的代码是:

public void Button2_Click(object sender, EventArgs e){}

In the next button, I had the exact same setup, replacing "Button2" with "Button3."在下一个按钮中,我进行了完全相同的设置,将“Button2”替换为“Button3”。 SO just doesn't allow me to post too much code at once.所以只是不允许我一次发布太多代码。

The page doesn't access the code behind.该页面不访问后面的代码。

What I have Tried so far:到目前为止我已经尝试过:

  • using Button3 to try accessing Button2_Click().使用 Button3 尝试访问 Button2_Click()。 This does not work either (ie, no function runs)这也不起作用(即,没有 function 运行)

  • using "OnClick" instead of "onclick."使用“OnClick”而不是“onclick”。 This also does not run the function.这也不会运行 function。

  • Checking the designer.cs - all of my designer.cs files simply show an empty partial class, and all work just fine.检查designer.cs - 我所有的designer.cs 文件只显示一个空的部分class,一切正常。 I'm updating them using the VS2019 compiler.我正在使用 VS2019 编译器更新它们。

Is there anything missing from the second button that needs to be there?第二个按钮是否缺少任何需要的东西?

EDIT:编辑:

I solved the problem by simply changing how I was doing it (ie, conditional logic in Button2_Click() to have different behavior based on input).我通过简单地更改我的操作方式解决了这个问题(即,Button2_Click() 中的条件逻辑根据输入具有不同的行为)。 If anyone wants to take a crack at the root problem to help anyone with this problem in the future, be my guest.如果有人想解决根本问题以帮助将来解决此问题的任何人,请成为我的客人。

Not really an answer to the root of the question, but if you have two forms operating on the same (or less than each other) inputs, then depending on your business needs, this will work:并不是问题根源的真正答案,但如果您有两个 forms 在相同(或彼此较少)输入上运行,那么根据您的业务需求,这将起作用:

<form method="post">
    <h2>View an Existing Reservation</h2>
   <p><sub>Or just enter your email to recover your reservations</sub></p>
   <div>
       <div class="form-row">
            <div class="col-lg-6 col-md-6 col-sm-12">
                  <label for="name">Reservation ID:</label>
                  <asp:TextBox runat="server" ID="ID" Name="ID"  CssClass="form-control"></asp:TextBox><br />
            </div>
            <div class="col-lg-6 col-md-6 col-sm-12">
                  <label for="email">Email</label>
                  <asp:TextBox runat="server" ID="Email" TextMode="Email" CssClass="form-control"></asp:TextBox><br />
            </div>
      </div>
      <div class="form-row justify-content-around">
            <asp:Button ID="Button2" Text="Edit" onclick="Button2_Click" runat="server" CssClass="btn btn-primary"/>

      </div>
</div>

</form>
public void Button2_Click(object sender, EventArgs e)
        {
            var keys = Request.Params.AllKeys;

           double id = Request.Form["ctl00$MainContent$ctl00$ID"] != "" ? double.Parse(Request.Form["ctl00$MainContent$ctl00$ID"].ToString()) : -1;

            string email = Request.Form["ctl00$MainContent$ctl00$Email"].ToString();
            if(id == -1) // some default value if the field is left empty
            {
                resend(email); // the function supposed to be run by the smaller form
                return;
            }
            var obj = Request.Form;
            Response.Redirect(String.Format("View.aspx?id={0}&email={1}", id, email));
        }

Basically, by seeing if the value not required for the smaller form (in this case, the resend() function) is empty, we see what the user's intent was - by inputting both values, they get one function, and by only submitting one value, they get another function.基本上,通过查看较小形式不需要的值(在本例中为 resend() 函数)是否为空,我们可以看到用户的意图是什么——通过输入两个值,他们得到一个 function,并且只提交一个值,他们得到另一个 function。 This is scalable to as many functions as you want, although it'd start to be a pain eventually.这可以扩展到您想要的任意数量的功能,尽管它最终会开始变得很痛苦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM