0

I am trying to run some JavaScript from an event handler. Here is my code:

Protected Sub RecordBottomGridRow(ByVal sender As Object, ByVal e As EventArgs) Handles GridView_Body.PageIndexChanging
    Dim scriptText As New StringBuilder()
    scriptText.Append("<script type=""text/javascript"">")
    scriptText.Append(Environment.NewLine)
    scriptText.Append("function RecordBottomGridRow() {")
    scriptText.Append(Environment.NewLine)
    scriptText.Append("var elements = document.getElementsByClassName('exp');")
    scriptText.Append(Environment.NewLine)
    scriptText.Append("var expIndex;")
    scriptText.Append(Environment.NewLine)
    scriptText.Append("for (var i = 0, im = elements.length; im > i; i++) {")
    scriptText.Append(Environment.NewLine)
    scriptText.Append("var elementId = elements[i].id;")
    scriptText.Append(Environment.NewLine)
    scriptText.Append("expIndex = elementId.substring(3);")
    scriptText.Append(Environment.NewLine)
    scriptText.Append("}")
    scriptText.Append(Environment.NewLine)
    scriptText.Append("var fundCode = document.getElementById('htmlFund' + expIndex);")
    scriptText.Append(Environment.NewLine)
    scriptText.Append("var e = document.getElementById('exp' + expIndex);")
    scriptText.Append(Environment.NewLine)
    scriptText.Append("document.getElementById('lastFundChosen') = fundcode.value;")
    scriptText.Append(Environment.NewLine)
    scriptText.Append("document.getElementById('expOrColl').value = e.value;")
    scriptText.Append(Environment.NewLine)
    scriptText.Append("alert(e.value);")
    scriptText.Append(Environment.NewLine)
    scriptText.Append("} </script>")

    ScriptManager.RegisterStartupScript(Me, Me.GetType(), "RecordBottomGridRow", scriptText.ToString(), False)
End Sub

Notice the "alert" at the end there. I run the code and the event fires, but no alert window pops up suggesting the JavaScript code did not run. What am I doing wrong?

9
  • How does your javascript code look like in web browser? You can debug javascript code in browser. Commented Oct 27, 2014 at 17:55
  • Hit f12 after the page loads and debug the javascript, would be my first suggestion, fyi chrome is easier to debug in than IE Commented Oct 27, 2014 at 17:59
  • @user3715441 Did you call JavaScript function RecordBottomGridRow? Commented Oct 27, 2014 at 18:04
  • I tried in IE and Chrome. I see the script on the page but when I put a break point in and trigger the event the break point never gets reached. Commented Oct 27, 2014 at 18:09
  • 1
    @user3715441 - RegisterStartupScript() will execute the code given to it. Your code, though, only creates a function. It never actually calls anything. Therefore, your function is never actually called. Commented Oct 27, 2014 at 18:21

1 Answer 1

1

You code just declares function, in essence it just adds to your page output:

function RecordBottomGridRow() {
.....
}

You need to add client-side code that actually call this function by name:

RecordBottomGridRow();

So in your case you can just replace line

scriptText.Append("} </script>")

with (to follow your format):

scriptText.Append("}")
scriptText.Append(Environment.NewLine)
scriptText.Append("RecordBottomGridRow(); </script>")

This code block finishes function declaration with "}" and then adds the call to function to actually execute it on client.

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

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.