Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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 :
ASP.NET
<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
C#
string tblName = txtlabel.InnerText.ToString();
Posted
Updated 18-Apr-16 22:57pm
Comments
Sergey Alexandrovich Kryukov 18-Apr-16 13:51pm    
I have no idea what is "JavaScript messagebox". There is no such thing, is there? :-)
It seems that you need to understand what client-side script does, and what the server side does.
—SA
Hameed Khan 18-Apr-16 13:58pm    
Just message box show i enter value and i want this value in aspx.cs code??Simple is that
ZurdoDev 18-Apr-16 14:37pm    
If you need to access an html control in C# then put a runat="server" tag on it.

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.

HTML
<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

C#
protected void insert(object sender, EventArgs e)
{
    string value = Request.Form["hiddenName"];
}
 
Share this answer
 
Comments
Hameed Khan 19-Apr-16 8:14am    
Awesome Thanks it's working :)
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
 
Share this answer
 
v2
Can you use "alert" in your Javascript to display the value?
JavaScript
alert(person);


And you need to set the value of the element, not the element itself.
JavaScript
document.getElementById('txtlabel').value = person;
 
Share this answer
 
Comments
Hameed Khan 18-Apr-16 14:25pm    
I need value of javascript messagebox...I insert value in message box and use this value in aspx.cs
Hameed Khan 18-Apr-16 14:25pm    
alert is used to simple message box.
Hameed Khan 18-Apr-16 14:37pm    
I'm receiving null value

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900