2015-12-01 30 views
1

我被困在這個錯誤......這個環節的字符串INT示值誤差:輸入字符串的格式不正確。在CS文件

<a id="25" class="abc" href="example.aspx" onclick="return myfunction(this);"> 

,我的功能碼取ID,並將其保存到會話變量..

function myfunction(a) 
{ 
    var myid = a.id; 
    '<%Session["x"] ="' + myid +'";%>'; 
    return; 
} 

所以,在那之後它會重新定向example.aspx和example.aspx.cs文件throwimg代碼:

static int j = 0; 
if(Session["x"]!=null) 
{ 
    j = int.Parse(Session["x"].ToString()); 
} 

日是拋繩錯誤: 出現FormatException:輸入字符串的格式不正確..

堆棧:

[FormatException: Format of input string is not correct..] 
System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +7476983 
    System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +119 
    System.Int32.Parse(String s) +23 
    example.Page_Load(Object sender, EventArgs e) in example.aspx.cs:103 
    System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14 
    System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35 
    System.Web.UI.Control.OnLoad(EventArgs e) +99 
    System.Web.UI.Control.LoadRecursive() +50 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627 
+0

那麼'Session [「x」]。ToString()'返回什麼值?不管它是什麼,它都不是可以解析的數字。 –

+0

它應該返回25作爲一個字符串..因爲我們已經存儲它從JavaScript函數 –

+0

好吧,但它是什麼_actually_返回? –

回答

0

在你的代碼的問題本質上是'<% ... %>'用於當服務器(即。 .cs)向客戶端發送數據(即.html,.js)。

要將數據從客戶端傳遞到服務器,您需要使用其他替代方法,如Query Strings或html <form>標記。