Add postbackurl as a attribute to button

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.

  • SQL Server Temp table vs Table Variable
  • Getting dates based on week number
  • accessing value from SQL parameter to use it for an XML attribute
  • Create a stored procedure to find out employees who has overtime from timesheet table
  • Selecting a Column based on Child and Parent Category
  • N Hibernate Session.Delete Foreign key
  • 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!

  • SQLException : String or binary data would be truncated
  • MVC 4.0 .NET app and values returned from database after deployment on IIS
  • Using Identity Insert on a linked server
  • FIFO Implementation in Inventory using SQL
  • Sending File through database Mail
  • Get size of all tables in database
  • 2 Solutions collect form web for “Add postbackurl as a attribute to button”

    Button click event cannot perform both at the same time at client side – submit to server and redirect to another page.

    Ideally, you do not want to insert each registration step into database. instead, you want to store them in session state. Then insert them into database at the final page.

    Here is the example. Make sure you check validation on each step –

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

    If I understand your question, you need to do two things when the user clicks the button:

    1. Call “Button1_Click”
    2. Post to “Envia2.apsx”

    The problem is that 1. is itself consists of a form post to the current page (as you may know, ASP.Net server controls, in order to handle events on the server side, trigger a postback behind the scenes). So I could see a few options:

    1. Use OnClick and remove postbackurl — inside the click handler, redirect to “Envia2.aspx”
    2. Remove both OnClick and postbackurl. Instead, make an Ajax call of some kind to invoke the “Button1_Click” code, and then when complete, call form.post() on the form that “Button1” belongs to.
    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.