2016-09-21 86 views
0

我想顯示一個簡單的消息使用Json我要去哪裏錯了,我不明白,請幫助我。我的JSON代碼是顯示消息在asp.net中使用Json

<script type="text/javascript"> 
    var text = '{ "R000092201": "EIN and Name should be same" }'; 

    var obj = JSON.parse(text); 

    document.getElementById("demo").innerHTML = obj.R000092201; 

我的aspx代碼

<div id="divErrorList" runat="server" style="line-height: 25px; font-size: 16px; font-weight: bold; color: #E41B17; padding-left: 20px; margin-top: 1%; border: 1px dashed red; background-color: #FBEFEF;"> 
      <div> 
       Rejection Code: 
       <asp:Label ID="lblRejectionReason" runat="server" Text=""></asp:Label> 
      </div> 
      <div> 
       Rejection Reason: 
        <asp:Label ID="demo" runat="server" Text=""></asp:Label> 
      </div> 
     </div> 

,我的aspx.cs代碼

if (Convert.ToString(ldr["rejection"]) == "R0000-922-01") 
        { 
         divErrorList.Visible = true; 
         lblRejectionReason.Text = Convert.ToString(ldr["rejection"]); 
         //ScriptManager.RegisterStartupScript(this, GetType(), "displayalertmessage", "Showalert();", true); 
         // lblrejectionmsg.Text = Convert.ToString(ldr["rejection_msg"]); 
         contSup.Visible = false; 
         editbtn.Visible = true; 
        } 
        else 
        { 
         divErrorList.Visible = true; 
         editbtn.Visible = false; 
         contSup.Visible = true; 
         lblRejectionReason.Text = Convert.ToString(ldr["rejection"]); 
         // lblrejectionmsg.Text = Convert.ToString(ldr["rejection_msg"]); 
        } 
+0

只需打開瀏覽器開發者控制檯查看是否有任何錯誤。你的代碼是好的,但如果它上面的任何JS行給出錯誤,那麼它也不會工作 –

+0

我認爲如果演示Label在contentplaceholder內。那麼它的id在客戶端可能會有所不同。如果是這種情況,您可以使用服務器控件的這個屬性來確保它在客戶端也具有相同的ID。 ClientIDMode =「Static」

+0

顯示......無法設置屬性'innerHTML'爲空 –

回答

0

標籤的ID可能是不一樣的客戶端。所以,很可能js無法得到它。你可以在標籤ClientIDMode =「Static」上使用這個屬性,或者使用不同的客戶端getter,比如jquery。你可以在哪裏得到一個名字以「demo」結尾的節點。

$("span[id$='demo']") 

我希望它有幫助。

0

只是我已經改變了腳本和主要的事情是幫助哈立德汗我把ClietIDMode =「靜態」在標籤。 JSON代碼如下

<script type="text/javascript"> 
    var text = '{ "R000092201": "EIN and Name should be same" }'; 

    var obj = JSON.parse(text); 

    document.getElementById('lblrejectionmsg').innerHTML = obj.R000092201; 
</script> 

我的aspx代碼

<div> 
    Rejection Reason: 
    <asp:Label ID="lblrejectionmsg" runat="server" Text="" ClientIDMode="Static"></asp:Label> 
</div> 

謝謝你們對我的幫助