Jonas
Jonas

Reputation: 3283

Check for changes in a ASP.net form

I want to check if there have been any changes to a form on my ASP.NET webpage, What are my options?

Should i check if the viewstate has changed or should create a flag in the code-behind, triggered by webcontrol events like TextChanged for Textboxes or SelectedIndexChanged for Dropdownlists?

Upvotes: 0

Views: 2712

Answers (3)

Derek Wade
Derek Wade

Reputation: 746

Set the proper OnChange event (different for some controls, i.e. Droplist:OnSelectedIndexChanged) for all controls to call a single form_Changed function. In that function, set a global variable to true. Then in the Button Click Handler, check that value. All "Changed" events trigger before the button Click handler.

ASPX

<asp:CheckBox runat="server" id="loginallowed" Checked="true" OnCheckedChanged="form_Changed" />
<asp:TextBox ID="tbFirst" runat="server" CssClass="form-control required" OnTextChanged="form_Changed"/>

ASPX.CS

private Boolean formChanged = false;
protected void form_Changed(object sender, EventArgs e)
{
    formChanged=true;
}
protected void btn_Click()
{
    if(!formChanged) return;
}

Upvotes: 0

ACB
ACB

Reputation: 1000

You could store the sent values in attributes. Something like:

Textbox1.Text = <Sent Text>
Textbox1.Attributes.Add "OldText",Textbox1.Text

On postback, you can compare:

If Textbox1.Text <> Textbox1.Attributes("OldText") Then
   ' Text is different

You would have to do that for every control in your form. Of course, you could write a procedure to do this in a more automatic way, like iterating through all your controls.

Upvotes: 2

Rubens Farias
Rubens Farias

Reputation: 57946

Easy way: submit that form, and at server-side, compare sent values with those stored in your data layer.

Upvotes: 1

Related Questions