Passing hint window value from javascript function - PostBack to C #

I will try my best to formulate what I am 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 calls a query window. General image - if input is entered - it will be saved in the database column.

I draw a space when passing a value from the prompt window to 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 am trying to figure out how to make this call for input from a javascript function.

I spent the last few hours on the Internet and in books. It was overloaded.

EDIT

I made friends a little to match 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 a row into a table now .....

 string nEvent = Request.Form["event"]; 
    if (txtAction.Value == "saveevent") { 
                   nName.Insert(); //am i on the right track? 
                 }
+5
5

, (, ). , :

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

, :

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

hiddenNameField.Value.

+2

java script, -.

, , SendForm

     function SendForm() {
         var name = $("#label").text();
         PageMethods.SendForm(name,
          OnSucceeded, OnFailed);
     }
     function OnSucceeded() {   
     }
     function OnFailed(error) {
     }

, javascript.

  [WebMethod(enableSession: true)]
    public static void SendForm(string name)
    {

    }

0
<script language='Javascript'> 
__doPostBack('__Page', ''); 
</script> 

"" javascript

0

, AJAX. jQuery, ... AJAX.

- :

function PromptSomewhere(/* some args if needed*/)
{
    var nName = prompt("New Name", " ");
    // Do process your prompt here... as your code in JS above. Not placed here to be more readable.
    // nName is used below in the AJAX request as a data field to be passed.

    $.ajax({
        type: "post", // may be get, put, delete also
        url: 'place-the-url-to-the-page',
        data {
            name: nName
            // You may put also other data
        },
        dataType: "json",
        error: PromptFailed,
        success: OnPromptComplete
    });
}

function  PromptFailed(xhr, txtStatus, thrownErr) // The arguments may be skipped, if you don't need them
{
    // Request error handling and reporting here (404, 500, etc.), for example:
    alert('Some error text...'); // or
    alery(txtStatus); // etc.
}

function OnPromptComplete(res)
{
    if(!res)
        return;

    if(res.code < 0)
    {
        // display some validation errors
        return false;
    }

    // display success dialog, message, or whatever you want

    $("div.status").html(result.message);
}

. #:

using System.Web.Script.Serialization;

protected void Page_Load(object sender, EventArgs e)
{
    if(IsPostBack && ScriptManager.GetCurrent(this).IsInAsyncPostBack)
    {
        string nName = Request.Form["name"];

        // do validation and storage of accepted value
        // prepare your result object with values 



        result.code = some code for status on the other side
        result.message = 'Some descriptive message to be shown on the page';

        // return json result
        JavaScriptSerializer serializer = new JavaScriptSerializer();

        Response.Write(serializer.Serialize(result));
    }
}

. ASP.NET MVC 2 , , JsonResult Request.IsAjaxRequest ( , ) ASP.NET - ASP.NET MVC - - MVC () ASP.NET.

0

:

__doPostBack()

Basically, call a function PostbackWithParameter()from your other JS function:

<script type="text/javascript">
function PostbackWithParameter(parameter)
{
    __doPostBack(null, parameter)
}
</script>

And at your code end, take the value for this parameter as follows:

public void Page_Load(object sender, EventArgs e)
{
    string parameter = Request["__EVENTARGUMENT"];
}
0
source

All Articles