[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 382
  • Last Modified:

Assigning a value returned from a javascript function to a variable in vb.net

Hi!

I have the following javascript function:

function GetPhone()
{
 With document.all
 {
   if (!IsMatch(txtTSYSPhone.value,'mq_phone'))//forphone    
   {    
       var Phone;
       alert("clean format");                      
       Phone=FormatClean(txtTSYSPhone.value);
       return Phone;
    }  
 }


I want to assign the value returned from the above function to a avraiable in vb.net

function GetFormatPhone()
{
 With document.all
 {
   if (!IsMatch(txtTSYSPhone.value,'mq_phone'))//forphone    
   {    
       var Phone;
       alert("clean format");                      
       Phone=FormatClean(txtTSYSPhone.value);
       return Phone;
    }  
 }

Private Sub GetPhone()
      Dim Phone as String
        Dim sScript As StringBuilder = Nothing

        sScript = New StringBuilder
        sScript.Append("<Script Language='javascript'>")
        sScript.Append("document.location.href='SearchTSYS.aspx'")
        sScript.Append("</Script>")
        ' Dim sTSYSPhone As String = Request.Params("txtTSYSPhone") & ""
        'Dim strScript As String = "<script language='javascript'>"test"</script>"
        ClientScript.RegisterStartupScript(Me.GetType(), "GetFormatPhone", sScript.ToString)
    End Sub

How do I assign the value returned from the javascript function to the string phone in Javascript function. If it ha sto be done using hidden variables, how should it be done? If there is any other way, how? I saw a comment poste din this site to a question similar to this, but I don't understand that at all an ddon't think it's helpful to me.

If I can get the code/ logic, that'll be great.

Thanks a  million in advance!

IT'S EXTREMELY URGENT! I NEED IT ASAP.

0
amukta
Asked:
amukta
  • 10
  • 3
  • 2
1 Solution
 
b0lsc0ttCommented:
amukta,

The 2 languages and scripts are run at different times.  You can't have Javascript pass a value directly to VB.NET (or any other language).  However you can use the URL's query string, form fields, cookies, etc to set a value that VB.NET code can then read.  In other words Javascript would use one of those to set a name/value pair and then VB.NET would read it.  There is no way to do it directly.

Let me know if you have any questions or need more information.

b0lsc0tt
0
 
amuktaAuthor Commented:
Hi!

Bo1,

Could you please me know how that can be done?
I'm not aware of that.
Any tips or logic you can offer? That'll be greatly appreciated.I need to finish up with this by tomorrow evening and I've been trying this for a long time now.I'm ne wto javacsript and hence I posted this question.

Thank you in advance.
0
 
b0lsc0ttCommented:
I provided general info above on how it can be done.  Also tips and why it works that way.  You need to decide which method you want to use and a key will be how the passing of the value will be initiated.  If you have a question about the methods I mentioned let me know but they are pretty basic so I would think you'd know what I mean (i.e. URL, form fields, cookie).  If you need code samples or details on a method please let me know which you choose and provide any details on the pages I would need to give you code.
bol
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
amuktaAuthor Commented:
Hi! sm394,

Thanks  alot for ur response!
As per my requirement, I should not call  function test() method from on client click event of the button.  "getresults()" that is being called in the server side button click event.

In the getresults function, I have a string variable to which I need to assign the value of the hidden variable.
public string Name;

        protected void btnTest_Click(object sender, EventArgs e)
        {
                 If(txtPhone.text.length>0)
              {
                  lnkPhone.Innerhtml=txtPhone.text;
                 txtPhone.text="";
              }
                 getresults();
         }
getresults()
{
   String Phone=hdnField.value; // This is giving me an error.

}
My test function looks like this

function test()
{
  with (document.all)
 {
   if(!Is match(txtPhone,'txt'))
{
hdphone.value=FormatClean(txtPhone.value) //The FormatClean function removes extra characters
}
}
}

please let me know ASAP. i'm still struggling with it. it's extremely urgent.

0
 
amuktaAuthor Commented:
Hi! sm394,

Thanks  alot for ur response!
As per my requirement, I should not call  function test() method from on client click event of the button.  "getresults()" that is being called in the server side button click event.

In the getresults function, I have a string variable to which I need to assign the value of the hidden variable.
public string Name;

        protected void btnTest_Click(object sender, EventArgs e)
        {
                 If(txtPhone.text.length>0)
              {
                  lnkPhone.Innerhtml=txtPhone.text;
                 txtPhone.text="";
              }
                 getresults();
         }
getresults()
{
   String Phone=hdnField.value; // This is giving me an error.

}
My test function looks like this

function test()
{
  with (document.all)
 {
   if(!Is match(txtPhone,'txt'))
{
hdphone.value=FormatClean(txtPhone.value) //The FormatClean function removes extra characters
}
}
}

please let me know ASAP. i'm still struggling with it. it's extremely urgent.

0
 
amuktaAuthor Commented:
I'm sorry!

I hit "submit" twice by mistake and it got posted twice...I'm very sorry!
0
 
amuktaAuthor Commented:
Hi! sm394,

The actual thing is:

I have 4 textboxes, a button and a grid.
In the aspx page, the form tag looks like this.
<form id="frm" method="post" runat="server" onsubmit="fncpagevalidate()"

fncPagevalidate()
{window.event.returnvalue=false;
with(doument.all)
{
if(txtphone.value==null||txtphone.value='')
{
alert("please enter a valid phone no.");
return;
}
}
}
When i click on the button, the value that teh user enters in the textbox should be shown as a link below it and the resulst ahve to be displaye din a grid.I'm doing this on the button_click event.

   protected void btnTest_Click(object sender, EventArgs e)
        {
                 If(txtPhone.text.length>0)
              {
                  lnkPhone.Innerhtml=txtPhone.text;
                 txtPhone.text="";
              }
                 getresults();
         }
  After assigning the textbox value to the link, the phoneno that was in the textbox should be converted to the format 1234567890 which I'm doing like this in the .aspx page.

function test()
{
  with (document.all)
 {
   if(!Is match(txtPhone,'txt'))
{
hdphone.value=FormatClean(txtPhone.value) //The FormatClean function removes extra characters
}
}
}
and this value of hdphone.value should eb asigned to a string value in getresults function to dispaly the results.
private void GetResults()
{
 string str;
 str=hdPhone.value; this is sent as teh key to get the results.
and some code to get teh results and display in teh grid.

}
After the results are displayed, the link should still appear below the textbox and the user should eb able to click on the link and value in the link should be populatedin the textbox and allowed to be edited
and the new value of the text box should appear as a link and again go button can be hit and teh ersulst can be displayed and so on.

The link is defined as follows:
<a id="lnkphone" href="javascript:Populatetextboxes();" runat="server"></a>

function populate testboxes()
{
with(document.all)
{
  txtPhone.value=lnkPhone.innerHTML;
}
}

and also I have a question a sto how to remove extra characters from a zipcode entered in the following formats using javascript.
12345 or 12345-6789 or 12345 6789 or 12345/6789 or 12345.6789 , but 123-45-6789 should not be accepted an dany alphanumeric characters should not be accepted.


Please let me know.

Thank you!
It's extremely urgent and i just have a few more hours to finish it.
0
 
sm394Commented:
>>  String Phone=hdnField.value; // This is giving me an error.
 

what error you getting ?
0
 
sm394Commented:
in your above post it appears there are many questions  not one
first you need to break down your problems and list each question as a separate thread

by mixing lot of questions in one thread i doubt you would be able to get precise help
0
 
amuktaAuthor Commented:
The actual thing is:

I have 4 textboxes, a button and a grid.
In the aspx page, the form tag looks like this.
<form id="frm" method="post" runat="server" onsubmit="fncpagevalidate()"

fncPagevalidate()
{window.event.returnvalue=false;
with(doument.all)
{
if(txtphone.value==null||txtphone.value='')
{
alert("please enter a valid phone no.");
return;
}
}
}
When i click on the button, the value that teh user enters in the textbox should be shown as a link below it and the resulst ahve to be displaye din a grid.I'm doing this on the button_click event.

   protected void btnTest_Click(object sender, EventArgs e)
        {
                 If(txtPhone.text.length>0)
              {
                  lnkPhone.Innerhtml=txtPhone.text;
                 txtPhone.text="";
              }
                 getresults();
         }
  After assigning the textbox value to the link, the phoneno that was in the textbox should be converted to the format 1234567890 which I'm doing like this in the .aspx page.

function test()
{
  with (document.all)
 {
   if(!Is match(txtPhone,'txt'))
{
hdphone.value=FormatClean(txtPhone.value) //The FormatClean function removes extra characters
}
}
}
and this value of hdphone.value should eb asigned to a string value in getresults function to dispaly the results.
private void GetResults()
{
 string str;
 str=hdPhone.value; this is sent as teh key to get the results.
and some code to get teh results and display in teh grid.

}
After the results are displayed, the link should still appear below the textbox and the user should eb able to click on the link and value in the link should be populatedin the textbox and allowed to be edited
and the new value of the text box should appear as a link and again go button can be hit and teh ersulst can be displayed and so on.

The link is defined as follows:
<a id="lnkphone" href="javascript:Populatetextboxes();" runat="server"></a>

function populatetextboxes()
{
with(document.all)
{
  txtPhone.value=lnkPhone.innerHTML;
}
}

I have only one question, the links are getting populated......all I need to know is at what place in the code getFormatPhone() function needs to be called.
0
 
amuktaAuthor Commented:
The reason i wanto to know the above mentioned one is because, i need to remove extra spaces from the phone no. and assign it to the hidden field on the clientside.
I have to send teh value of this hidden fields to a str variable in the button_click event and pass teh string variable as a key to get the results.

Please let me know ASAP.
0
 
amuktaAuthor Commented:
I got the answer.
0
 
amuktaAuthor Commented:
I got the answer
0
 
amuktaAuthor Commented:
Thank you!
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 10
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now