Monday, March 26, 2012

Pass Value from HTML to Code Behind

Is it possible to pass a value coming from a javascript function in HTML to a

receiving variable in Code Behind?

My program passes a value to a function in HTML '**** This works fine ****'

'****** CODE BEHIND ******'

Private Sub MyDataGrid(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles MyDataGrid.ItemDataBound

Dim x As Integer

For x = 0 To 1

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then

e.Item.Cells(x).Attributes.Add("onclick", "sample('" & e.Item.Cells(x).Text & "')")

End If
Next

End Sub

'MY PROBLEM IS IN HERE. I want to pass a variable back to the Code Behind

'******* HTML *******'

<script language="javascript"
function sample(j)

'PROCESS j AND PASS IT BACK TO A VARIABLE IN CODE BEHIND
{
</script
i tried to make a public variable in code behind but it wont get the value passed

i think it is because of ITEMDATABOUND.

PLEASE HELP ME . . . Please show me how to make this work. . .Not sure exactly what you are trying to do but one easy way to pass values to the server is using Hidden Fields, so the idea is before posting back to the server you set the value using classic javascript and then in the server you will be able to read it either using the Request.Form collection or if you add this as a runat=server control using the Control.Value property.
So in your .aspx page, add this:

<INPUT Type="Hidden" ID="Hidden1" Name="Hidden1"
and in your javascript sample function, you can manipulate this field throught the "document" object. And then in your codebehind, you can simply retrieve the value:

string theValue=Request.Form["Hidden1"];

0 comments:

Post a Comment