Len Smith
Len Smith

Reputation: 197

How to prevent form's action to occur in asp.net

I have a form with action="xyz.aspx:type=new" and some input textboxes and 2 buttons: Submit and Preview. The Submit button updates the database with textbox values and Preview button carries the form textbox values to page "xyz.aspx?type=preview" . The problem is that the functiobalities are achieved but after clicking Preview button, the form's action url gets into action and causes an update in the database which is not wanted. So how do I prevent this from happening? ANy help is appreciated.

Upvotes: 0

Views: 129

Answers (1)

SouthShoreAK
SouthShoreAK

Reputation: 4296

If your two buttons are not asp:Buttons, do that first.

<asp:Button Id="Submit" runat="server" OnClick="Submit_OnClick"/>
<asp:Button Id="Preview" runat="server" OnClick="Preview_OnClick"/>

Now, move your code to update the database into Submit_OnClick

protected void btnExecute_Click(object sender, EventArgs e)
{
    //update your database
}

Your database should only be updated when Submit is clicked.

If you need your Preview button to post to another page, use the PostBackUrl property.

Upvotes: 1

Related Questions