如何避免在的AutoPostBack一个FormView? [英] How to avoid AutoPostBack in a FormView?

查看:158
本文介绍了如何避免在的AutoPostBack一个FormView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有其中有一个表单标签内的一个FormView继承ASPX页面。

I have an inherited ASPX page which has a FormView within a Form tag.

例如:

<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

正如你可以看到我使用Ajax,因为进一步下跌的形式有CalendarExtender控制处理日期。

As you can see I am using Ajax because further down the form there are CalendarExtender controls to handle dates.

日期文本框领域均具有的AutoPostBack =真正的,因为该程序会做在服务器端什么都没有。即:无事件被触发。 IE,FF和铬 - 这是在所有浏览器发生。在这个阶段不要在乎Safari浏览器等。

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.

问题的关键是,我想我已经陷入自动回地狱,那里是做验证和其它控制操纵太多的控制已AutoPostBacks设置为true。

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

什么是更好的方式来处理这种情况,如文本框,dropdownlists等控件需要在提交表单前执行动作的情况?我想这是任何形式开发项目的共同要求。

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.

我看到的地方(链接 ),该包裹内的FormView控件的的UpdatePanel 的ContentTemplate 避免使用的AutoPostBack的。有人可以解释一些更详细的介绍?

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?

感谢您

更新
设置我的控件的AutoPostBack =true是不是一个解决方案,因为它搅得我DetailsView控件的其他领域,他们做回发为好。我再次怀疑我必须包装里面像一个UpdatePanel FormView控件完全避免自动回或完全在JScript中,这是我讨厌做数据操作。不过,我可能没有任何选择。

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.

推荐答案

这取决于你在做什么样的验证。如果你是做简单的验证(如必填字段或常规前pression验证),那么你可以做JavaScript中的确认;无后回是必需的。甚至还有一个<一个href=\"https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.requiredfieldvalidator%28v=vs.110%29.aspx\"相对=nofollow> &LT; ASP:&的RequiredFieldValidator GT; 和<一个href=\"https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.regularex$p$pssionvalidator%28v=vs.110%29.aspx\"相对=nofollow> &LT; ASP:RegularEx pressionValidator&GT; ,你可以使用

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.

如果您需要验证您的数据对数据库,那么你需要一台服务器调用不知何故。如果你不想做一个完整的页面刷新,那么你需要使用AJAX。最简单的办法是包装,需要在的 &LT;&UpdatePanel的GT; 。例如:

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>

虽然的AutoPostBack 是真实的,它只会更新的&LT的内容;的UpdatePanel&GT; ,所以它会不会破坏其他内容。

Even though AutoPostBack is true, it will only update the contents of the <UpdatePanel>, so it won't disrupt your other content.

作为一个说明,将UpdatePanel的仍然每个部分后发布的所有表单数据的后面。 (这样做的好处是,响应只更新UpdatePanel的,而不是整个页面的内容。)这意味着,如果你有一个大的形式很多领域,你可能会发送大量的数据比你想在电线,因为AJAX回发会后每个字段的值回服务器,当你真的只需要一个字段的值。为了避免这种情况,你可以使用页的方法,而不是更新面板。然而,他们大量的工作,因为你需要写一大堆的JavaScript的。

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.

这篇关于如何避免在的AutoPostBack一个FormView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆