Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
829 views
in Technique[技术] by (71.8m points)

asp.net - How to access viewstate using javascript?

I am trying to access view-state in client side but following exception coming : enter image description here

JAVASCRIPT:

<script language="javascript" type="text/javascript">
    var vCode = '<%=ViewState("code")%>';
    alert(dateView);
</script>

CODE BEHIND:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    ViewState("code") = "EE"
End Sub

Anybody suggest me how to do it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I would suggests to use RegisterHiddenField than mixing server/js codes:

You may try this sample:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    ViewState("code") = "EE"
    Page.ClientScript.RegisterHiddenField("vCode", ViewState("code"))
End Sub

On your javascript:

var vCode = document.getElementById("vCode");
alert(vCode);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...