gpt4 book ai didi

javascript - 如何在 Asp.Net 按钮单击事件上将值从 C# 发送到 javascript?

转载 作者:行者123 更新时间:2023-12-02 16:13:35 28 4
gpt4 key购买 nike

我正在尝试将 C# 值提取到 JavaScript 中,任何人都可以帮助我解决这个问题。

ASP.NET 代码

 <asp:Button runat="server" Text="Submit"   class="btn btn-success btn-lg" ID="btnsubmit" OnClick="btnsubmit_Click"/>

C# 代码

protected void btnsubmit_Click(object sender, EventArgs e)
{
string abc="xyz";
// From here i need to Call JavaScript Function
// suppose code is like myfunction(abc)<-Java script function
myfunction(abc);
}

Javascript

<script type="javascript/text">

function myfunction(abc){
var val = abc
console.log(val);
// here i want to get abc value is this possible
}

</script>

最佳答案

您可以使用ClientScriptManager.RegisterStartupScript方法。

Registers the startup script with the Page object using a type, a key, a script literal.

代码

 protected void btnsubmit_Click(object sender, EventArgs e)
{
string abc="xyz";

//If the string can contain apostrophes or backslashes, you would need to escape them
string value = abc.Replace("\\", "\\\\").Replace("'", "\\'");
Page.ClientScript.RegisterStartupScript(this.GetType(),"CallMyFunction","myfunction('" +value + "')",true);
}

关于javascript - 如何在 Asp.Net 按钮单击事件上将值从 C# 发送到 javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29912905/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com