Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing prompt box value from javascript function- PostBack to c#

I'll try to do the best I can to articulate what I'm trying to do.

Let me preface by saying that I am very new to C# and ASP.NET and have minimal experience with javascript.

I have a javascript function that invokes a prompt box. The overall picture is - if input is entered - it will be saved to a column in the database.

I'm drawing a blank on passing the value from the prompt box to the PostBack in c#.

function newName()
{
    var nName = prompt("New Name", " ");
    if (nName != null)
    {
        if (nName == " ")
        {
            alert("You have to specify the new name.");
            return false;
        }
        else
        {
            // i think i need to getElementByID here???
            //document.forms[0].submit();
        }
    }
}

This is what I have in C#:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //I have other code that works here
    }
    else
    {
        //I'm totally lost here
    }
}

I'm trying to figure out how to make that call for the input from the javascript function.

I've spent the last few hours looking online and in books. Been overwhelmed.

EDIT

i did a little tweeking to fit what I'm trying to do....

<asp:HiddenField ID="txtAction" runat="server" Value="" /> 

document.forms(0).txtAction.Value = "saveevent"; 
document.forms(0).submit();

trying to figure out how to insert the string into the table now.....

 string nEvent = Request.Form["event"]; 
    if (txtAction.Value == "saveevent") { 
                   nName.Insert(); //am i on the right track? 
                 }
like image 221
rlegs Avatar asked Nov 03 '22 22:11

rlegs


1 Answers

Well, here's one possible way (untested but should give you the basic idea). You could place a hidden field on your form to hold the value of the prompt:

<input type="hidden" id="hiddenNameField" runat="server" value="">

Then prompt the user for the value, set it to the hidden field, and then submit your form:

document.getElementById('hiddenNameField').value = nName;
document.forms(0).submit();

Then in your code-behind you can just access hiddenNameField.Value.

like image 195
Grant Winney Avatar answered Nov 10 '22 00:11

Grant Winney