Reputation: 4888
I have an inherited ASPX page which has a FormView within a Form tag.
ie:
<body style="margin:0px;" bgcolor="skyblue">
<form id="form1" runat="server" action="Default.aspx">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div style="background-color: #87ceeb; vertical-align: top; text-align: center;">
<asp:FormView ID="FormView1" runat="server" DefaultMode="Insert" DataSourceID="SqlDataSource1"
OnItemInserting="FormView_Inserting" OnItemInserted="FormView_Inserted" BackColor="#00C0C0" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px">
<InsertItemTemplate>
etc
etc
As you can see I am using Ajax because further down the form there are CalendarExtender controls to handle dates.
The date textbox fields all have AutoPostBack="true"
because the program would do nothing on the server side. ie: no events were firing. This was happening across all browsers - IE, FF and Chrome. Don't care about Safari, etc at this stage.
The point is I think I have fallen into "autopostback hell", where too many controls that do validation and other control manipulation have AutoPostBacks set to "true".
What is the better way to handle situations where controls such as textboxes, dropdownlists, etc need to perform actions before the form is SUBMITted? I would assume this is a common requirement in any form development project.
I saw somewhere (link) that wrapping FormView controls inside an UpdatePanel
and ContentTemplate
avoid the use of AutoPostBack. Can someone please explain some more on that?
Thank you
UPDATE Setting my controls to AutoPostBack="true" is not a solution, because it upsets other areas of my DetailsView control and they do a postback as well. Again I suspect I have to wrap the formview inside something like an UpdatePanel to avoid autopostback entirely or do the data manipulation entirely in JScript, which I hate. But I may not have any choice.
Upvotes: 1
Views: 1174
Reputation: 6830
It depends on what kind of validation you are doing. If you are doing simple validation (like required fields or regular expression validation) then you can just do the validation in javascript; no post back is required. There's even an <asp:RequiredFieldValidator>
and <asp:RegularExpressionValidator>
that you can use.
If you need to validate your data against the database, then you need to make a server call somehow. If you don't want to do a full page refresh, then you need to use AJAX. The easy way is to wrap each field that needs server-side validation in an <UpdatePanel>
. For example:
<asp:UpdatePanel ID="UpdatePanel1" runat="server"
ChildrenAsTriggers="True" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" />
<asp:Label ID="Label1" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
Even though AutoPostBack
is true, it will only update the contents of the <UpdatePanel>
, so it won't disrupt your other content.
As a note, UpdatePanels will still post all of your form data with each partial post back. (The benefit it that the response only updates the contents of the UpdatePanel, not the entire page.) What this means is that if you have a large form with a lot of fields, you could be sending a lot more data than you want over the wire, because the AJAX post back will post the value of every field back to the server, when you really only need the value of one field. To avoid this, you can use page methods instead of update panels. However, they are a lot more work since you need to write a bunch of javascript.
Upvotes: 1