1

I have a button, which OnClick stores some text to tables in a database.

All this in asp.net using c# and .NET Framework 4.0.

However, i need that, when click, the button executes the OnClick method, thing is doing right now, but ALSO, navigates to another page, to continue registration.

The registration form is about 9 pages, i know if i pass postbackurl="url" it navigates to another page/form, but then it doesn't executes the OnClick method.

<div style="text-align:right; margin-top:20px"><asp:Button ID="Button1" runat="server" ValidationGroup="Curriculum" postbackurl="Envia2.aspx" OnClick="Button1_Click" CssClass="my_btn" /></div>

So, how can i 'attach' this attribute to the OnClick method?

Or there is another walkaround to this situation?

Thanks in advance!

4

2 回答 2

1

按钮单击事件不能在客户端同时执行 - 提交到服务器并重定向到另一个页面。

理想情况下,您不希望将每个注册步骤插入数据库。相反,您希望将它们存储在会话状态中。然后将它们插入到最后一页的数据库中。

这是示例。确保检查每个步骤的验证 -

public class Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }
    public string Other { get; set; }
}

// Page1.aspx
<asp:Button runat="server" ID="SubmitButton" Click="SubmitButton_Click"
    Text="Continue to Step 2" />   

// Page1.aspx.cs
protected void SubmitButton_Click(object sender, EventArgs e)
{
    var customer = new Customer
    {
        FirstName = FirstNameTexBox.Text,
        LastName = LastNameTexBox.Text
    };
    Session["Customer"] = customer;

    // Redirect to page 2
    Response.Redirect("Page2.aspx");
}

// Page2.aspx
<asp:Button runat="server" ID="SubmitButton" Click="SubmitButton_Click" 
    Text="Continue to Step 3" />  

// Page2.aspx.cs
protected void SubmitButton_Click(object sender, EventArgs e)
{
    var customer = Session["Customer"] as Customer;
    customer.Address = AddressTextBox.Text;
    Session["Customer"] = customer;

    // Redirect to page 3
    Response.Redirect("Page3.aspx");
}

// Page9.aspx
<asp:Button runat="server" ID="SubmitButton" Click="SubmitButton_Click" 
    Text="Submit" /> 

// Page9.aspx.cs
protected void SubmitButton_Click(object sender, EventArgs e)
{
    var customer = Session["Customer"] as Customer;
    customer.Other = OtherTextBox.Text;

    // Save customer to database.
}
于 2013-10-14T16:07:59.893 回答
1

如果我理解您的问题,当用户单击按钮时,您需要做两件事:

  1. 调用“Button1_Click”
  2. 发布到“Envia2.apsx”

问题是 1. 本身就是由一个表单 post 到当前页面(你可能知道,ASP.Net 服务器控件,为了处理服务器端的事件,在幕后触发一个 postback)。所以我可以看到一些选择:

  1. 使用OnClick和删除postbackurl——在点击处理程序中,重定向到“Envia2.aspx”
  2. 删除OnClickpostbackurl。相反,进行某种 Ajax 调用以调用“Button1_Click”代码,然后在完成后调用form.post()“Button1”所属的表单。
于 2013-10-14T16:00:21.350 回答