MrCarder
MrCarder

Reputation: 438

Issues calling JavaScript function from C# code behind (.NET)

I'm fairly new to the marriage of C# and javascript in the same application. I think I must be missing some important piece of making them work together. Calling my javascript function from my codebehind doesn't result in the outcome I expect, but doesn't result in an error either. Simply nothing happens. I am developing with Visual Studio 2010, and if there as a debugger for JS built in, I don't know where to find it - not being able to step through is making this much more aggravating.

In my .aspx (both "FieldName" values come from another part of the code):

<script language ="javascript">
    var idSelection;
    var nameSelection;
    function selectRow(idItem, nameItem) {
        idSelection = idItem;
        nameSelection = nameItem;
        alert(idSelection + " " + nameSelection);
        var targetIdValue;
        var targetForm = window.opener.document.forms(0);
        eval("targetForm." + targetIdFieldName + ".value = '" + idSelection + "';");
        eval("targetForm." + targetNameFieldName + ".value = '" + nameSelection + "';");
        window.close();
    }
</script>

And my call:

        protected void AppGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
        {
                txthidAppId = (HtmlInputHidden)Session["hidAppId_rvte"];
                txtAppName = (TextBox)Session["txtAppName_rvte"];

                txthidAppId.Value = selectedApp.Id;
                txtAppName.Text = selectedApp.Name;

                Page.ClientScript.RegisterStartupScript(GetType(), "SelectApp", "selectRow(" + txthidAppId.Value + ", " + txtAppName.Text + ")", true);
}

Upvotes: 0

Views: 609

Answers (3)

Mehmet
Mehmet

Reputation: 1874

You must add QuatationMarks to your string parameters. I wrote an extension method for this;

    public static string AddQuatationMark(this string value)
    {
        string retStr = "";
        retStr = "" + "'" + value + "'" + "";
        return retStr;
    }

And using this method on your code;

"selectRow(" + txthidAppId.Value.AddQuatationMark() + ", " + txtAppName.Text.AddQuatationMark() + ")"

Upvotes: 0

Smeegs
Smeegs

Reputation: 9224

I don't really see anything wrong. What I would do is try changing language=javascript to type="text/javascript" in the script tag.

Also, I would change

Page.ClientScript.RegisterStartupScript(GetType(), "SelectApp", "selectRow(" + txthidAppId.Value + ", " + txtAppName.Text + ")", true);

to

Page.ClientScript.RegisterStartupScript(Page.GetType(), "SelectApp", "selectRow(" + txthidAppId.Value + ", " + txtAppName.Text + ")", true);

Upvotes: 1

Darren Wainwright
Darren Wainwright

Reputation: 30727

Not fully sure what you're trying to accomplish, however, you have some syntax issues in your Javascript.

selectRow(" + txthidAppId.Value + ", " + txtAppName.Text + ")

should be

selectRow(" + txthidAppId.Value + ", '" + txtAppName.Text + "')

Notice the single-quote ' around your txtAppName.Text value. You're sending strings, so Javascript needs them to be passed through as such, otherwise it treats them as objects.

I advise you to install Firefox and FireBug for firefox (http://getfirebug.com/) - this will give you a nice developer debugger for Javascript and would show you that error right away.

Upvotes: 2

Related Questions