简体   繁体   English

从C#中的背后代码调用javascript函数时,document.getElementById()无法正常工作

[英]document.getElementById() not working when call javascript function from code behind in c#

I have a javascript function that is working fine in default.aspx when click on a link button. 我有一个javascript函数,当单击链接按钮时,在default.aspx中工作正常。 However, when I call this function from code behind, it cant work. 但是,当我从后面的代码中调用此函数时,它将无法工作。

Here is part of my code in default.aspx : 这是default.aspx中我的代码的一部分:

function loadAdditionalInfoDialog(qtyId)
{
    alert(qtyId);
    var qty = document.getElementById(qtyId).value;
    alert(qty);
}

Here is part of my code in code behind (default.aspx.cs) when click on a button: 单击按钮时,这是我代码后面(default.aspx.cs)的代码的一部分:

protected void btnRedeemAll_Click(object sender, EventArgs e){
    TextBox txtQty = (TextBox)itm.FindControl("txtQty");

    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "temp", "<script>loadAdditionalInfoDialog(" + txtQty.ClientID + ")</script>", false);
}

The alert(qtyId) is working for both side and print out the same word. alert(qtyId)对双方都有效,并打印出相同的单词。 (default page and code behind). (默认页面和后面的代码)。 But code behind fail to alert(qty). 但是后面的代码无法警告(数量)。 Anyone know what is my problem? 有人知道我有什么问题吗?

Noted that the qtyId is a text box id inside a repeater in default.aspx. 请注意,qtyId是default.aspx中转发器内的文本框ID。

probably you are passing the wrong ClientID . 可能您传递了错误的ClientID

Use a java-script debugger (such as FireBug) and check if var qty = document.getElementById(qtyId); 使用Java脚本调试器(例如FireBug)并检查var qty = document.getElementById(qtyId); is not null (it will probably be). 不为null (可能为null )。

Have also a look here 在这里也看看

Could you try below? 您可以在下面尝试吗?

protected void btnRedeemAll_Click(object sender, EventArgs e){
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "temp", "  <script>loadAdditionalInfoDialog('" + txtQty.ClientID + "')</script>", false);
}

It seems you're missing single quotes around clientid being passed. 似乎您缺少有关正在传递的clientid的单引号。

Are you missing the quotes in the argument '" + txtQty.ClientID + "' 您是否遗漏了引号'" + txtQty.ClientID + "'的引号

protected void btnRedeemAll_Click(object sender, EventArgs e){
    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "temp"," <script>loadAdditionalInfoDialog('" + txtQty.ClientID + "')</script>", false);

} }

Try that: 试试看:

Page.ClientScript.RegisterStartupScript(this.GetType(), "temp", "<script>loadAdditionalInfoDialog('" + txtQty.ClientID + "')</script>", false);

This is method for scripts that should be bound during initial page rendering. 这是用于在初始页面呈现期间绑定的脚本的方法。

My guess is the problem is caused by that you're creating the control txtQty on the button click event handler. 我的猜测是问题是由您在按钮单击事件处理程序上创建控件txtQty引起的。

This is a dynamically-created control and might not exist in HTML generated by the ASP.NET engine. 这是一个动态创建的控件,在ASP.NET引擎生成的HTML中可能不存在。

Try using a control that is on the page and that you added in design view and check if you get the same issue. 尝试使用页面上和您在设计视图中添加的控件,并检查是否遇到相同的问题。

Update: Apologies. 更新:道歉。 In the button click event handler you're trying to find the control. 在按钮单击事件处理程序中,您试图找到控件。 Why are you doing that? 你为什么要这么做? Is the control a dynamically-created control created somewhere on another part of the application? 该控件是在应用程序另一部分的某个位置创建的动态创建的控件吗?

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

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