如何在aspx.cs文件中使用javascript messagebox值? Asp.net C# [英] How can I use javascript messagebox value in aspx.cs file? Asp.net C#

查看:59
本文介绍了如何在aspx.cs文件中使用javascript messagebox值? Asp.net C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

..

我有一个'按钮','标签'和javascript函数。当我点击按钮然后消息框显示,我在消息框中插入值,我想使用此值在aspx.cs代码中?我怎么能这样做?



我尝试过:



这是我的cdoe:

 <   script     type   =  text / javascript >  
function myFunction(){
var person = prompt( 输入表名);

if (person!= null ){
document .getElementById(' txtlabel')= person ;

}
}
< / < span class =code-leadattribute> script >
< asp:按钮 ID = Button2 ValidationGroup = p runat = server onclick = insert OnClientCli ck = javascript:myFunction() 文本 = 创建表 / >
< label runat = server 可见 = false id = txtlabel > < / label >



我在aspx.cs中尝试这个但是没有工作

  string  tblName = txtlabel.InnerText.ToString(); 

解决方案

使用隐藏字段而不是标签,标签不会随表单一起提交,因此您无法通过他们的数据到你的服务器端代码。



 <  < span class =code-leadattribute> script     type   =  text / javascript >  
function myFunction(){
var person = prompt( 输入表名);

if (person!= null ){
document .getElementById(' hiddenName')。value =人;
}
}
< / script >
< 输入 type = hidden name = hiddenName id = hiddenName / >
< asp:按钮 ID = Button2 ValidationGroup = p runat = server OnClick = insert OnClientClick = javascript:myFunction() 文字 = 创建表格 / >





代码落后



  protected   void  insert( object  sender,EventArgs e)
{
string value = Request.Form [ hiddenName];
}


你使用txtlabel作为服务器对象,即runat =server。

表示 - 你不会有从javascript(客户端)以相同名称直接访问服务器控件,因为最终的html可能与服务器名称的值不同。



你需要什么要做的是使用ClientID来获取客户端值。



document.getElementById(<%= txtlabel.ClientID%>)是对象名称



document.getElementById(<%= txtlabel.ClientID%>)。value是对象值



这是在javascript中使用服务器控件的正确方法。



谢谢

干杯


您可以在Javascript中使用alert来显示值吗?

 alert(person); 





你需要设置元素的值,而不是元素本身。

  document  .getElementById('  txtlabel')。value = person; 


Hi,..
I have a 'button' ,'label' and javascript function.When i click on button then messagebox show ,i insert value in messagebox and i want to use this value in aspx.cs code?How can i do this?

What I have tried:

Here is my cdoe :

<script type="text/javascript">
function myFunction() {
        var person = prompt("Enter Table Name");

        if (person != null) {
            document.getElementById('txtlabel') = person;
          
        }
    }
</script>
<asp:Button ID="Button2" ValidationGroup="p" runat="server" onclick="insert" OnClientClick="javascript:myFunction()" Text="Create Table" />
<label  runat="server" visible="false" id="txtlabel"></label>


I try this in aspx.cs but not working

string tblName = txtlabel.InnerText.ToString();

解决方案

Use a hidden field rather than a label, labels are not submitted with a form so you can't pass their data to your server-side code.

<script type="text/javascript">
    function myFunction() {
        var person = prompt("Enter Table Name");

        if (person != null) {
            document.getElementById('hiddenName').value = person;
        }
    }
</script>
<input type="hidden" name="hiddenName" id="hiddenName" />
<asp:Button ID="Button2" ValidationGroup="p" runat="server" OnClick="insert" OnClientClick="javascript:myFunction()" Text="Create Table" />



Code behind

protected void insert(object sender, EventArgs e)
{
    string value = Request.Form["hiddenName"];
}


You used txtlabel as server object i.e runat="server".
means - you will not have direct access to the server control by the same name from javascript (client end) as the final html might not have the same value as the server name.

What you need to do is use "ClientID" to get the client value.

document.getElementById("<%= txtlabel.ClientID %>") is the object name
and
document.getElementById("<%= txtlabel.ClientID %>").value is the object value

This is the right way to use server control in javascript.

Thanks
Cheers


Can you use "alert" in your Javascript to display the value?

alert(person);



And you need to set the value of the element, not the element itself.

document.getElementById('txtlabel').value = person;


这篇关于如何在aspx.cs文件中使用javascript messagebox值? Asp.net C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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