繁体   English   中英

如何从服务器端ASP.NET调用Javascript函数

[英]How to call Javascript function from server side asp.net

我在GridView有一个Textbox ,如果文本框中的文本被更改,我想更改Label.Text。 但是我无法调用用.cs页编写的javascript函数。 TbUnitCost_TextChanged方法是否有任何错误。

这是我的aspx页面

<ItemTemplate>
  <asp:TextBox Style="text-align: right" ID="TbUnitCost" runat="server" Width="80px" Text='<%#Bind("Unit_Cost")%>' AutoPostBack="true" OnTextChanged="TbUnitCost_TextChanged"  TextMode="Number"></asp:TextBox>
 </ItemTemplate>

Page后面的代码如下

 protected void TbUnitCost_TextChanged (object sender, EventArgs e)
    {
        GridViewRow currentRow = (GridViewRow)((TextBox)sender).Parent.Parent;
        int rowNo = currentRow.RowIndex;
        string gridName = "GridWorkExpenses";
        TextBox tbunitCost = (TextBox)currentRow.FindControl("TbUnitCost");
        int row = Convert.ToInt32(tbunitCost.Text);

        tbunitCost.Attributes.Add("onchange","javascript:calculateTotalCost('"+rowNo+"','"+gridName+"');");

    }

而javascript函数是:

<script type ="text/javascript">
    function calculateTotalCost(rowNo, GridName) {                        

        if (GridName== 'GridWorkExpenses') {
            var rowscount = document.getElementById('<%=GridWorkExpensesSheet.ClientID%>').rows.length;
            return calculateTotalUnitCost("GridWorkExpensesSheet", rowscount,rowNo);
        }
   }
 </script>     

尝试使用此:

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Script", "CalculateTotalCost", true);

描述任何进一步的问题(如果有)。

ScriptManager.RegisterStartupScript(this, this.GetType(), "Script", "<script type='text/javascript'>CalculateTotalCost</script>", false); 

是另一种方法

这是一个非常古老的讨论[1]: RegisterStartupScript和RegisterClientScriptBlock之间的区别?

请也参考这个。

嗨,您必须将所有命令都写在一个字符串中

 ScriptManager.RegisterClientScriptBlock((sender as Control), GetType(), "jQueryCommand", "$('.MenuLoginLink').hide();$('.MenuUsername').show();$('.MenuUsername').text('" + Currentmember.Mobile  + "');$('.MenuExit').show();" +
                " $('.btnLoginInFriends').hide();$('#HiddenSession').val('" + int.Parse(Session["CurrentUserID"].ToString()) + "');", true);

暂无
暂无

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

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