RSS

How to access server side variable at client side

18 Jul

So the markup looks something like this

<form id="form1" runat="server">
   <div>
<input type="button" value="Alert" onclick="Print();" />
</div>
<script type="text/javascript">

function Print()

{

alert(<%=QuestionID %>);

}

</script>

</form>

If Not IsPostBack Then

QuestionID = 5

End If

Private _QuestionID As Integer

Public Property QuestionID() As Integer

Get

Return _QuestionID

End Get

Set(ByVal value As Integer)

_QuestionID = value

End Set

End Property

C#

<form id="form1" runat="server">
   <div>
<input type="button" value="Alert" onclick="Print();" />
</div>
<script type="text/javascript">

function Print()

{

alert(<%=QuestionID %>);

}

</script>

</form>
private int _QuestionID;
   public int QuestionID
{
get { return _QuestionID; }
set{_QuestionID = value;}
}
protected void Page_Load(object sender, EventArgs e)
{

QuestionID=9
}

Also you can write in html (body) tags

   <a href="faqs-detail.aspx?QuestionID =<%= QuestionID %>">[Click Here]</a>

Advertisements
 
Leave a comment

Posted by on July 18, 2011 in ASP Dot Net C#

 

Tags: , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: