3

On the click of a button, I call a JavaScript function. After getting the value, I need to perform some stuff from the value obtained in the code-behind. How should I call code-behind?

My aspx:

function openWindow(page) {
  var getval = window.showModalDialog(page);
  document.getElementById("<%= TxtInput.ClientID %>").value = getval; 
  //After this I need to perform stuff 'Upload(TxtInput.value)' into database from the code-behind
}

The button calling the function is set up in the following manner:

<button class="doActionButton" id="btnSelectImage" runat="server" onclick="openWindow('../rcwksheet/popups/uploader.htm')">Select Image</button>

My desired code behind (VB):

Public Sub btnSaveImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectImage.ServerClick
  Dim inputFile As String = Me.TxtInput.Value
  //do more stuff here
End Sub

So:

  1. Is there a way to call code-behind from the JavaScript?
  2. Can I somehow use the "onclick" property of a button to first go to a JavaScript and then to the code-behind?
  3. Trigger a code-behind call "onchange" of the TxtInput.Value?
1

3 Answers 3

1

yes there is a way.

first, you can use javascript to submit the form after your return value is set in TxtInput.

function openWindow(page) {
  var getval = window.showModalDialog(page);
  document.getElementById("<%= TxtInput.ClientID %>").value = getval; 
  document.forms[0].submit();
}

then in your code behind, you can handle TxtInput's value in page load event.

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack)
    {
        if (this.Input.Value != string.Empty)
        {
            this.Input.Value += "blah";
        }
    }
}

note: you may need Identifying control that caused postback

Sign up to request clarification or add additional context in comments.

5 Comments

If the page reloads again, and is a post back, can I still access the view state parameters?
if you mean access to view state on server side, the answer is yes.
Thanks! I ended up using your solution. I marked all as answers, because theoretically they sound right, but I tried your thing.
you can only mark one as answer and looks like you marked Jesper's answer.
Changed. Please see if you can help me on this: stackoverflow.com/questions/10679769/…
0

You can put the server side code into a web service, make a service reference in an asp:ScriptManager on your aspx page and then you can call/execute the web service from javascript by calling:

WebServiceClassName.MethodName(javascriptvariable, doSomethingOnSuccess)

Here is a link on doing that:

http://msdn.microsoft.com/en-us/magazine/cc163499.aspx

Comments

0

You can call the __doPostBack Event.

function openWindow(page) {
  var getval = window.showModalDialog(page);
  document.getElementById("<%= TxtInput.ClientID %>").value = getval; 
__doPostBack('btnSelectImage', getval);
}

And on the server side in your code behind, you can get the value:

In the PageLoad method:

if (Request.Form["__EVENTTARGET"] == "btnSelectImage")
{
    //get the argument passed
    string parameter = Request["__EVENTARGUMENT"];
    //fire event
    btnSaveImage_Click(this, new EventArgs());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.