簡體   English   中英

ScriptManager.RegisterStartupScript()方法不起作用 - ASP.NET,C#

[英]ScriptManager.RegisterStartupScript() method not working - ASP.NET, C#

我使用了ScriptManager.RegisterStartupScript()方法,以便在后端發生特定事件時顯示警告。它在頁面加載方法中工作正常,但在單擊特定按鈕時調用的特定方法中沒有。 我無法找到解決方案,因為在另一個頁面中,它在頁面加載和方法中都能正常工作。

腳本管理器RegisterStartupScript方法

ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", "alert('msg');", true);

HTML

<asp:HiddenField runat="server" ClientIDMode="Static" ID="PostBackController"/>
<button class="some-class" id="btnSave" runat="server" onclick="btnSave_clientClick();">SAVE</button>

使用Javascript

function btnSave_clientClick() {

     // code

     if (some_condition) {

        $('#PostBackController').val("btn_save");
        __doPostBack();

     }
}

頁面加載方法

protected void Page_Load(object sender, EventArgs e)
{
    if (PostBackController.Value == "btn_save")
    {
        uploadDocSave_ServerClick();
    }
}

按鈕單擊時應調用方法Wish

protected void uploadDocSave_ServerClick()
{
    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", "alert('msg');", true);
}

如果您仍然需要使用Webforms,我對OP表示最深切的哀悼。 這是您在減少流量的同時以最小的方式解決它的方法:

Codebehind示例:

public partial class About : Page, IPostBackEventHandler
{
    protected void Page_Init(object sender, EventArgs e)
    {
        // Unless the button is serverside clicking it will reload the page
        // registering the page like this prevents a page reload.
        var scriptManager = ScriptManager.GetCurrent(Page);
        scriptManager?.RegisterAsyncPostBackControl(Page);
    }

    /// <inheritdoc />
    public void RaisePostBackEvent(string eventArgument)
    {
        var javascriptCode = $"alert('server method called and triggered client again. {DateTime.Now.ToString("s")}');";

        // if your key isn't changed this script will only execute once
        ScriptManager.RegisterStartupScript(udpMain, typeof(UpdatePanel), Guid.NewGuid().ToString(), javascriptCode, true);

        // updating the updatepanel will inject your script without reloading anything else
        udpMain.Update();
    }
}

Webforms樣本:

<script type="text/javascript">
    function clientFunction(sender, args) {

        alert('client function called');

        <%= Page.ClientScript.GetPostBackEventReference(Page, "callServerFunction") %>

        args.preventDefault();
    }
</script>

<asp:UpdatePanel runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional" ID="udpMain">
    <ContentTemplate>
        <h2><%: Title %>.</h2>
        <h3>Your application description page.</h3>
        <p>Use this area to provide additional information.</p>
        <button onclick="clientFunction(); return false;">raise server function</button>
    </ContentTemplate>
</asp:UpdatePanel>

試試這個:

客戶端:

Use Literal Control
<asp:Literal ID="IMsg" runat="server" Text=""/>

服務器端:

string msg = "<script>alert('Change successfully');</script>";
IMsg.Text = msg;

我認為它會對你有所幫助。 它在我的項目中工作正常。

試試這個:

如果您使用了更新面板,那么您可以使用:

ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "javascriptFunction();", true);

其他明智的你可以使用

ClientScript.RegisterStartupScript
        (GetType(),Guid.NewGuid().ToString(), "javascriptFunction();",true);

嘗試刪除包含以下內容的行

__doPostBack();

在JavaScript代碼中。

對不起,我遲到了。 使用RegisterStartupScript方法的這個重載 ,並將第一個參數設置為要單擊的按鈕以注冊javascript函數。 喜歡:

ScriptManager.RegisterStartupScript(btnSave, this.GetType(), "alert", "alert('msg');", true);

干杯!

真正的簡單方法

您可以使用onserverclick屬性將<button runat="server"></button>到服務器事件:

HTML:

<button class="some-class" id="btnSave" onserverclick="uploadDocSave_ServerClick" runat="server">SAVE</button>

C#處理程序:

protected void uploadDocSave_ServerClick(Object sender, EventArgs args)
{
    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", "alert('msg');", true);
}

與客戶端驗證

如果要在回發之前添加某種JavaScript檢查,可以從onclick屬性執行此操作:

HTML:

<button class="some-class" id="btnSave" onclick="return btnSave_clientClick();" onserverclick="uploadDocSave_ServerClick" runat="server">SAVE</button>

JavaScript的:

function btnSave_clientClick() {

     // code

     if (some_condition) {

        return true; // allows the control to do a postback

     }
}

C#處理程序:

protected void uploadDocSave_ServerClick(Object sender, EventArgs args)
{
    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", "alert('msg');", true);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM