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?
RecordBottomGridRow?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.