0

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);
}
4
  • Are you using update panels in this page? Commented May 28, 2013 at 18:35
  • I am not using any update panels. Commented May 28, 2013 at 18:36
  • have you tried executing a simple alert statement from c# to make sure it's not a problem with the function itself? Just to see if the generated javascript is being handled? Commented May 28, 2013 at 18:37
  • I created a separate function simply called "clickedAlert()" and it worked. My main concern is the lack of error, I have no idea where to go because there is seemingly nothing wrong... Commented May 28, 2013 at 18:39

3 Answers 3

2

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.

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

2 Comments

You also need to be careful about single quotes in your textbox.
Playing around with the quotes, this isn't really the solution I was looking for. A few different variations (including the one posted here) produced the same [lack of] results. FireBug looks promising though, thanks for the recommendation!
1

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);

Comments

0

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() + ")"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.