gpt4 book ai didi

c# - 单击 ASP.NET 按钮后调用 javascript 函数,然后调用 C# 函数

转载 作者:行者123 更新时间:2023-11-28 10:33:10 25 4
gpt4 key购买 nike

我有这个严重的问题:

我有 ASP.NET 页面,该页面内容包含带有 ASP.NET 控件的更新面板。

我有 Java 脚本函数来进行验证,因此当我单击按钮时,我将使用 onclientclick 调用 java 函数来进行验证,完成此操作后应该从后面的代码中调用事件单击按钮函数。

我尝试了 vew 方法,但它们对我不起作用。

这是我的代码示例,单击按钮后 onclientclick 将调用 java 脚本函数进行验证,如果验证正常,则应调用 onclick 事件。

......................java脚本函数......................

    <script type="text/javascript" >

function add(){
if (tag == trye) {


document.getElementById('<%=btnInfor.ClientID%>').click();
alert("DataAdded")



}
else {

alert("Requiered Field Missing.")
return false;

}
}

</script>
.....................
ASP.NET button
...................
<asp:Button ID="btnInfor" runat="server" Text="Add Information" Style="position: absolute;
top: 1659px; left: 433px;"
onclientclick="JavaScript: return myAdd()" />
....................
code behind in C#
......................
protected void btnInfor_Click(object sender, EventArgs e)
{
\\mycode

}

最佳答案

你设置它的方式是,当你调用 document.getElementById(buttonname).click() 时,它会不断调用 javascript 函数,并且永远不会真正到达你想要调用的服务器端方法。

首先分配服务器端点击事件处理程序:

<asp:Button ID="btnInfor" runat="server" Text="Add Information" Style="position: absolute;
top: 1659px; left: 433px;" OnClick="btnInfor_Click"
onclientclick="JavaScript: return myAdd()" />

然后在您的 javascript 函数中,在 if 分支中返回 true:

function add(){
if (tag == trye) {
alert("DataAdded");
return true;
}
else {
alert("Requiered Field Missing.")
return false;
}
}

关于c# - 单击 ASP.NET 按钮后调用 javascript 函数,然后调用 C# 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2430072/

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