首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Attributes.add,ScriptManager.RegisterStartupScript,ScriptManager.RegisterClientScriptBlock不工作

Attributes.add,ScriptManager.RegisterStartupScript,ScriptManager.RegisterClientScriptBlock不工作
EN

Stack Overflow用户
提问于 2015-02-17 21:13:21
回答 1查看 747关注 0票数 0

我在webform上有一个按钮,在Ajax表容器中的更新面板中,它将文本框中的数据保存到数据库中。我有五条if语句,用于验证页面上的日期范围,并在范围无效时发出警告(例如):

代码语言:javascript
复制
If <condition> Then
    ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "alertMessage", "alert('Actual end date cannot be earlier than Sign date.');", True)
    updateFlag = False
End If

这个代码很好用。设置了用于更新数据的标志。问题是,如果代码通过if进入更新部分:在更新完成后,我放在那里的“保存数据”的alert不会弹出。我使用了以下每一种方法:

代码语言:javascript
复制
ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "alertMessage", "alert('Job saved.');", True)
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "alertMessage", "alert('Job saved.');", True)
Me.btnSave.Attributes.Add("onclick", "alert('Job saved.');")
ClientScript.RegisterStartupScript() 'don't have the rest of this line

他们都没有工作过。数据按其应有的方式保存;就像代码跳过alert行一样。如果有人能帮我,我将非常感激。

编辑:澄清我将Me.btnSave.Attributes.Add("onclick", "alert('Job saved.');")放在Page_Load中以及update语句之后,但在实际保存数据之前触发,因此在“保存”数据之后接收错误消息。点错了,不能用。

编辑2:张贴更多的代码。代码背后:

代码语言:javascript
复制
Protected Sub SaveAll()
    If <condition> Then
        ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "alertMessage", "alert('Actual end date cannot be earlier than Sign date.');", True)
        updateFlag = False
    End If
    ...
    If updateFlag Then
        'add values to SqlDataAdapter
        ...
        con.Open()
        da.SelectCommand.ExecuteNonQuery()
        con.Close()
        da.Dispose()
        ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "alertMessage", "alert('Job saved.');", True)
        'ScriptManager.RegisterStartupScript(Me, Me.GetType(), "alertMessage", "alert('Job saved.');", True)
        'Me.btnSave.Attributes.Add("onclick", "alert('Job saved.');")
        Response.Redirect("Edit.aspx?ID=" & txtID.Text)
    End If
End Sub

aspx:

代码语言:javascript
复制
<ajaxToolkit:TabContainer ID="TabContainer1" 
    runat="server" TabStripPlacement="Top">
<ajaxToolkit:TabPanel runat="server" ID="JobPanel" HeaderText="Job Info">
<ContentTemplate>
<asp:UpdatePanel ID="updatePanel1" runat="server">
<ContentTemplate>
<table>
    <tr>
        <td>
            <asp:Button ID="btnSave" OnClick="SaveAll" 
                runat="server" Text="Save" />
        </td>
    </tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</ContentTemplate>
</ajaxToolkit:TabPanel>
</ajaxToolkit:TabContainer>

如果你需要更多代码,请告诉我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-18 14:19:17

在这个特定的场景中,您不需要UpdatePanel,甚至AJAX。

代码语言:javascript
复制
Protected Sub SaveAll()
    If <condition> Then
        ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "alertMessage", "alert('Actual end date cannot be earlier than Sign date.');", True)
        updateFlag = False
    End If
    ...
    If updateFlag Then
        'add values to SqlDataAdapter
        ...
        con.Open()
        da.SelectCommand.ExecuteNonQuery()
        con.Close()
        da.Dispose()
        Response.Redirect("Edit.aspx?ID=" & txtID.Text & "&message=Job%20Saved")
    End If
End Sub

您试图运行某个客户端脚本,但随后重定向,从而结束响应。因此,解决方案是将消息传递给下一页,并让下一页显示确认消息。Edit.aspx页面应该检查页面加载上的查询字符串,如果有消息,则显示它。但不要使用alert()。那些太烂了。它们吸引了用户的注意力,不能被设计成样式。找到一个不错的客户端通知库。我真的很喜欢诺蒂

我不是VB程序员,但您需要将数据库连接封装在using语句中,这样它们才能正确地释放(或任何与VB等价的东西)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28571217

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档