将ASP.NET文本框值传递给javascript函数 [英] Passing ASP.NET textbox value to javascript function

查看:107
本文介绍了将ASP.NET文本框值传递给javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI全部,



我创建了一个ASP.Net表单,其中包含2个标签,2个文本框和1个文本框。 2个按钮。

我想将ASP.Net文本框中的值传递给JavaScript函数,触发器来自后面的代码。



我已经成功触发了使用以下语句从代码后面的JavaScript函数。



  protected  < span class =code-keyword> void  btnOK_Click( object  sender,EventArgs e)
{

ScriptManager .RegisterClientScriptBlock( this ,GetType(), MyKey myFunction(););

}



但是当我试图传递值时,它没有给我输出。

< pre lang =HTML> < script type = text / javascript >
function myFunction(){

var vUserName = document .getElementById( txtUserName)值。
var vPassword = document .getElementById( txtPassword)。value;

var vFullInfo = vUserName + vPassword;

alert(vFullInfo);

}
< / script >





我尝试过:



以下是整个解决方案的完整源代码。



Form01

 <%@     Page    语言  =  C#    AutoEventWireup   =  true    CodeBehind   =  frmStudInfo.aspx.cs   继承  =  WebApplication01.frmStudInfo   %>  

< !DOCTYPE html >

< html xmlns = http://www.w3.org/1999/xhtml >
< head runat = server >
< title > 第一个ASP.Net应用程序< / title >

< script 类型 = text / javascript >
function myFunction(){

var vUserName = document .getElementById( txtUserName)。value;
var vPassword = document .getElementById( txtPassword)。value;

var vFullInfo = vUserName + vPassword;

alert(vFullInfo);
}
< / 脚本 >

< / head >
< 正文 >
< form id = form1 runat = server < span class =code-attribute> method = post >

< asp:Label ID = Label1 runat = 服务器 正文 = 用户名 > < / asp:Label >
< asp:TextBox ID = txtUserName runat = server > < / asp:TextBox >
< ; br / < span class =code-keyword>>
< asp:Label ID = Label2 runat = server 文本 = 密码 > < / asp:Label >
< asp:TextBox ID = txtPassword runat = server > < / asp:TextBox >
< br / >
< asp:按钮 ID = btnCancel runat = server < span class =code-attribute>文本 = 取消 / >
< asp:按钮 ID = btnOK runat = server OnClick = btnOK_Click 文字 = 确定 / >

< / form >
< / body >
< / html >





Cod e表格01

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;

命名空间 WebApplication01
{
public partial class frmStudInfo:System.Web.UI.Page
{
private string strUserName;
private string strPassword;

受保护 void Page_Load(对象发​​件人,EventArgs e)
{

}

受保护 void btnOK_Click( object sender,EventArgs e)
{

ScriptManager.RegisterClientScriptBlock ( this ,GetType(), MyKey myFunction(); true < /跨度>);

}
}
}





我检查了代码并找出了JavaScript函数即MyFunction()不捕获表单中的值。因此问题在于从表单到JS函数的传递值。



有人可以解释一下出了什么问题吗?



谢谢,

Chiranthaka

解决方案

试试这个:



代码落后:





 protected void btnOK_Click(object sender,EventArgs e)
{
string fisrtname =FirstName;
string lastname =LastName;
Page.ClientScript.RegisterStartupScript(this.GetType(),key,showAlert('+ fisrtname +','+ lastname +');,true);

}





脚本:



< script type =text / javascript> 
函数showAlert(mydata1,mydata2){
var vUserName = document.getElementById(txtUserName)。value = mydata1;
var vPassword = document.getElementById(txtPassword)。value = mydata2;

var vFullInfo = vUserName + vPassword;

alert(vFullInfo);
}
< / script>





请投票,如果解决方案适合您。


试试

 ScriptManager。 RegisterStartupScript (this,GetType(),MyKey,myFunction();,true); 





 ClientScript.RegisterStartupScript(this.GetType(),MyKey,myFunction ();,true); 



都有效。


HI All,

I have created an ASP.Net form that has 2 labels, 2 textboxes & 2 buttons.
I want to pass the values in ASP.Net textboxes to JavaScript function and trigger is from code behind.

I have successfully triggered the JavaScript function from code behind using the below statement.

protected void btnOK_Click(object sender, EventArgs e)
        {

         ScriptManager.RegisterClientScriptBlock(this, GetType(), "MyKey", "myFunction();", true);

        }


But when I tried to pass the values it didn't give me an output.

<script  type ="text/javascript" >
        function myFunction() {
      
            var vUserName = document.getElementById("txtUserName").value;
            var vPassword = document.getElementById("txtPassword").value;
           
            var vFullInfo = vUserName + vPassword;
            
            alert(vFullInfo);

        }
</script>



What I have tried:

Below is the full source code for the total solution.

Form01

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="frmStudInfo.aspx.cs" Inherits="WebApplication01.frmStudInfo" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>First ASP.Net Application</title>

    <script  type ="text/javascript" >
        function myFunction() {
      
            var vUserName = document.getElementById("txtUserName").value;
            var vPassword = document.getElementById("txtPassword").value;
           
            var vFullInfo = vUserName + vPassword;
            
            alert(vFullInfo);
        }
</script>

</head>
<body>
    <form id="form1" runat="server" method="post">

        <asp:Label ID="Label1" runat="server" Text="User Name"></asp:Label>
        <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
        <br />
        <asp:Label ID="Label2" runat="server" Text="Password"></asp:Label>
        <asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="btnCancel" runat="server" Text="Cancel" />
        <asp:Button ID="btnOK" runat="server" OnClick="btnOK_Click" Text="OK" />
    
    </form>
</body>
</html>



Code Behind Form 01

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication01
{
    public partial class frmStudInfo : System.Web.UI.Page
    {
        private string strUserName;
        private string strPassword;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnOK_Click(object sender, EventArgs e)
        {

         ScriptManager.RegisterClientScriptBlock(this, GetType(), "MyKey", "myFunction();", true);

        }
    }
}



I checked the code and find out the JavaScript function i.e. MyFunction() does not capture the values from the form. So that the issue is at the passing values from the form to the JS function.

Could someone please explain what whould have gone wrong?

Thanks,
Chiranthaka

解决方案

Try This:

Code behind:


protected void btnOK_Click(object sender, EventArgs e)
     {
         string fisrtname = "FirstName";
         string lastname = "LastName";
         Page.ClientScript.RegisterStartupScript(this.GetType(), "key", "showAlert('" + fisrtname + "','" + lastname + "');", true);

     }



Script:

<script type="text/javascript">
       function showAlert(mydata1, mydata2) {
           var vUserName = document.getElementById("txtUserName").value = mydata1;
           var vPassword = document.getElementById("txtPassword").value = mydata2;

           var vFullInfo = vUserName + vPassword;

           alert(vFullInfo);
       }
   </script>



Please vote, if solution works for you.


try

ScriptManager.RegisterStartupScript(this, GetType(), "MyKey", "myFunction();", true);


or

ClientScript.RegisterStartupScript(this.GetType(), "MyKey", "myFunction();", true);


both works.


这篇关于将ASP.NET文本框值传递给javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆