0

I wrote a javscript which create a popup with certain information.

function Showduplicate() {
            isDuplicate = true;
            var modal = document.getElementById('duplicate');
            modal.style.display = '';
            modal.style.position = 'fixed';
            modal.style.zIndex = '100';
            modal.style.left = '30%';
            modal.style.top = '40%';

            var screen = document.getElementById('modalScreen');
            screen.style.display = '';
            return false;


    }

modal is a div element. duplicate is also a div element which contains certain checkboxes etc. . Now I check a query in codebehind and call this javascript function accordingly. I tried to use Page.RegisterClientScriptBlock Method but was unsuccessful. So can you guys help me out in calling the javascript function in codebehind.

Sayamima
  • 205
  • 1
  • 8
  • 24
  • In general you can call C# functions from JS, but not vice-versa - at least not without significant effort. What exactly are you trying to do with this code? It looks like an input validation helper. – sukru Apr 11 '11 at 00:20
  • I am actually dealing with duplication in grid where i need to take the decision of the user whether or not to create the record – Sayamima Apr 11 '11 at 00:23
  • Is this after a PostBack, or is it a response a a JS action? – sukru Apr 11 '11 at 00:41

2 Answers2

0

I think for your scenario, you should be using Page.RegisterStartupScript(). See this post for more info on the distinction between RegisterStartupScript() and RegisterClientScriptBlock().

Community
  • 1
  • 1
Bala R
  • 107,317
  • 23
  • 199
  • 210
0

I'm assuming you're using Asp.Net WebForms and Asp.Net Ajax. (The same technique can be applied to other scenarios as well).

Let's say you have a button like:

<tr>
    <td><input id="grid_1">Value</input></td>
    <td><a href="javascript:addItem(1)">Add</a></td>
</tr>

Then you can control the popup with the following JS code:

function OnAddSuccess(result) {
    // Update display
}

function OnAddError(error) {
    Showduplicate();
}

function addItem(id) {
    $get("LoadingLabel").innerHTML = "Loading...";
    PageMethods.TryAddItem(id, $get("grid_"+id).innerHTML, OnAddSuccess, OnAddError);
}

You should not forget:

<asp:ScriptManager ID="ScriptManager" runat="server" EnablePageMethods="true" />
sukru
  • 2,229
  • 14
  • 15