Go Premium for a chance to win a PS4. Enter to Win

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

simple asp prompt box question

Can someone show me, using asp or javascript, how to display a popup prompt box after a button click and return the value to an asp codebehind sub?

Basically, I have a button. When I push it I want a popup box to display with a textbox and OK/Cancel button. User will enter a new name and press OK. I need to grab that data and call a asp function (assuming it was  called from javascript) otherwise, just return to the asp function that created the popup box.
0
rutledgj
Asked:
rutledgj
  • 8
  • 8
1 Solution
 
rutledgjAuthor Commented:
Update. I got the prompt box to show using this javascript:

    function showNewProviderPrompt() {
        var name=prompt('Please Enter New Provider Name','');
    }

In code behind on page load I added this:
btnAdd.Attributes.Add("onclick", " return showNewProviderPrompt();")

Now how do I access the data entered when they click OK?
0
 
Alfredo Luis Torres SerranoCommented:
You could use a hiden field and set the selected value to that field ant then get the value in code behind.
0
 
Alfredo Luis Torres SerranoCommented:
In your asp page

<asp:HiddenField ID="MySelection" runat="server" />

In the java funciton

function showNewProviderPrompt() {
        var name=prompt('Please Enter New Provider Name','');
        document.getElementById('MySelection").value = name
    }

in the code behind you have myselection.value
0
Industry Leaders: 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!

 
rutledgjAuthor Commented:
How exactly do you get the value in the codebehind after the prompt closes? I assume you would do this in the button event or is it some how done in the pageload code?
0
 
Alfredo Luis Torres SerranoCommented:
After the promp close you have to send a request , Yes could be with a button.

Remember the codebehind is on serverr side and the hiden field is on client side.

So you have to do a postback.

Hope this helps
0
 
rutledgjAuthor Commented:
Ok. SO I have this now

function showNewProviderPrompt() {
        var name = prompt('Please Enter New Provider Name', '');
        document.getElementById("hiddenNewProvider").value = name;
       
    }

and in page:
<input type="text" id="hiddenNewProvider" runat="server" />

on button click I have this:
 Dim NewProvider As String = Me.hiddenNewProvider.Value.Trim

when I run this, enter data in the prompt and click ok the hidden field doesn't have anything in it when I try to read the data.
0
 
Alfredo Luis Torres SerranoCommented:
Please make this changes

function showNewProviderPrompt() {
        var name = prompt('Please Enter New Provider Name', '');
        var elem = document.getElementById("hiddenNewProvider");
        elem.value = name;
       
    }

And try again
0
 
rutledgjAuthor Commented:
Still not luck. the hidden field is still empty.

Just to recap and be sure I didn't forget something:

javascript:

    function showNewProviderPrompt() {
        var name = prompt('Please Enter New Provider Name', '');
        var elem = document.getElementById("hiddenNewProvider");
        elem.value = name;    
    }

Hidden field:

<asp:HiddenField id="hiddenNewProvider" runat="server" />

Page_Load:

 btnAdd.Attributes.Add("onclick", " return showNewProviderPrompt();")

 btnAdd_Click:

 Dim NewProvider As String = Me.hiddenNewProvider.Value.Trim
0
 
Alfredo Luis Torres SerranoCommented:
Could you please put a breakpoint in elem.value = name; and inspect what is in the name variable????
0
 
rutledgjAuthor Commented:
I tried that earlier but it won't allow a break point on the .ascx page.
0
 
Alfredo Luis Torres SerranoCommented:
.ascx , you are not using ASp???
0
 
rutledgjAuthor Commented:
yes but this is an asp user control page. same principle.
0
 
Alfredo Luis Torres SerranoCommented:
An update soryy
ok.

Change

    function showNewProviderPrompt() {
        var name = prompt('Please Enter New Provider Name', '');
        var elem = document.getElementById("hiddenNewProvider");
        elem.value = name;    
    }

With

    function showNewProviderPrompt() {
        var name = prompt('Please Enter New Provider Name', '');
        var elem = document.getElementById("<%= hiddenNewProvider.ClientID %>")
        elem.value = name;    
    }

and give it a try
0
 
rutledgjAuthor Commented:
I assume you really mean this:

   var name = prompt('Please Enter New Provider Name', '');
        var elem = document.getElementById("<%= hiddenNewProvider.ClientID %>")
        elem.value = name;

Still empty.

I also put a try catch around the javascript but it didn't throw an error.

      function showNewProviderPrompt() {
      try{
        var name = prompt('Please Enter New Provider Name', '');
        var elem = document.getElementById("<%= hiddenNewProvider.ClientID %>")
        elem.value = name;    
        }
        catch (Error){
        alert(Error.ToString);
        }
    }
0
 
Alfredo Luis Torres SerranoCommented:
Try

      function showNewProviderPrompt() {
      try{
        var name = prompt('Please Enter New Provider Name', 'Something');
        var elem = document.getElementById("<%= hiddenNewProvider.ClientID %>")
        elem.value = name;    
        }
        catch (Error){
        alert(Error.ToString);
        }
    }

and lets see the hiddenNewProvider.value
0
 
rutledgjAuthor Commented:
Well damn. It worked that time. Thanks.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 8
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now