繁体   English   中英

提交时asp.net中的ScriptManager问题

[英]ScriptManager issue in asp.net on Submit

我有以下代码:

<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btnSave" EventName="Click" />
    </Triggers>
    <ContentTemplate>
        <asp:Button ID="btnSave" runat="server" Text="Submit"/>
        <asp:Panel ID="pnlMyView" runat="server">
            <asp:GridView Control here...
        </Panel>
            <asp:Panel ID="pnlInfo" runat="server" Visible="False">
                 <div>
                     Some information...
                 </div>
        </Panel>
    </ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="UpdateProgress1" runat="server">
    <ProgressTemplate>
        Update in progress...
    </ProgressTemplate>
</asp:UpdateProgress>

<script language="javascript" type="text/javascript">
$(window).load(function() {
    $("input:submit[id$='btnSave']").click(function() {
    //jQuery code to validate gridview enteries...
})
});
</script>

脚步:

1.第一次刷新页面或加载页面,然后单击Submit按钮以验证GridView中的条目

2.验证正确完成,并将更改保存到数据库。

3.在gridview条目中进行一些更改,然后单击Submit按钮

4.验证未发生,并将条目(即错误的条目)保存在数据库中。

5.刷新页面,即按F5,现在一切正常。

如何在步骤4解决问题。

----------------- PS ----------------

实际上我有两个面板pnlMyView和pnlInfo.So在页面加载时我只对pnlMyView可见,而其他则不可见。 如果我转到pnlInfo并回到pnlMyView并提交一次,则不会进行任何验证。

谢谢

问题是您的HTML被重新加载,因此按钮上的事件处理程序丢失了。

您可以尝试以下代码,以确保处理程序保持原位:

<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div id="root">
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="btnSave" EventName="Click" />
        </Triggers>
        <ContentTemplate>
            <asp:Button ID="btnSave" runat="server" Text="Submit"/>
            <asp:Panel ID="pnlMyView" runat="server">
                <asp:GridView Control here...
            </Panel>
         </ContentTemplate>
    </asp:UpdatePanel>
    <asp:UpdateProgress ID="UpdateProgress1" runat="server">
        <ProgressTemplate>
            Update in progress...
        </ProgressTemplate>
    </asp:UpdateProgress>
</div>    
<script language="javascript" type="text/javascript">
$(window).load(function() {
    $("input:submit[id$='btnSave']").on("click", ".root", function() {
    //jQuery code to validate gridview enteries...
})
});
</script>

这样做是不同地注册处理程序。 您需要使用类root将UpdatePanel包装在div中。 然后像这样注册处理程序:

$("input:submit[id$='btnSave']").on("click", ".root", function() {});

这告诉jQuery在名为root的元素上注册处理程序,但仅在执行input:submit...时才执行。

由于div未删除,因此即使替换了HTML,它仍将检测处理程序。

这些类型的事件称为delegated events 您可以在jQuery文档中阅读更详尽的解释: http : //api.jquery.com/on/

更好地使用Button控件的OnClientClick属性:

<asp:Button ID="btnSave" runat="server" Text="Submit" OnClientClick="return validate()" />

function validate(){
 //jQuery code to validate gridview enteries...
 //if validation isn't succeeded return false, otherwise return true
}

嘿朋友们,我找到了解决方案。我使用live ,现在一切正常。

$("input:submit[id$='btnSave']").live("click", function(event) {

谢谢大家的回应

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM