2012-09-06 62 views
2

我有,我想通過單選按鈕list.and選擇一個選項後,顯示從數據庫的一個記錄,並單擊下一步按鈕旁邊記錄loads.this一個項目是我的html代碼:如何通過單選按鈕列表顯示圖片?

<table style ="width :800px"> 
     <tr> 
      <td style="width: 100px"> 
       <asp:Image ID="Image1" runat="server" ImageUrl="~/images/stdents12.jpeg" /></td> 
     </tr> 
     <tr> 
      <td style="width: 100px"> 


       <table style="width: 950px" id="TABLE1" onclick="return TABLE1_onclick()"> 
     <tr> 
      <td colspan="8" style="color: white; height: 21px; background-color: #3366ff"> 
       <asp:Label ID="Label1" runat="server" Font-Bold="True" Text="Test No :" Width="82px"></asp:Label> 
       <asp:Label ID="TestNo" runat="server" Text="Label" Width="100px"></asp:Label><asp:Label ID="Label2" runat="server" Font-Bold="True" Text="Test Name :" Width="84px"></asp:Label> 
       <asp:Label ID="TestName" runat="server" Text="Name Of the Test" Width="501px"></asp:Label><asp:Label ID="Label3" runat="server" Font-Bold="True" Text="Question :"></asp:Label> 
       <asp:Label ID="Question" runat="server" Text="N of T" Width="52px"></asp:Label></td> 
     </tr> 
     <tr> 
      <td style="width: 23px" rowspan="5"> 
      </td> 
      <td style="width: 100px; height: 1px;"> 
       &nbsp;</td> 
      <td style="width: 100px; height: 1px;"> 



      </td> 
      <td style="width: 100px; height: 1px;"> 
      </td> 
      <td style="width: 100px; height: 1px;"> 
      </td> 
      <td style="width: 100px; height: 1px;"> 
      </td> 
      <td style="width: 100px; height: 1px;"> 
       </td> 
      <td style="width: 100px; height: 1px;"> 
      <div class="timerCss"> <asp:Label ID="lblTimerCount" runat="server" Height="5px" Width="232px"></asp:Label>&nbsp;</div> 
       </td> 
     </tr> 
     <tr> 
      <td colspan="7" align="right"> 
       <asp:Image ID="Image2" runat="server" /> 
       <asp:Label ID="Questionlbl" runat="server" Height="66px" Text="Label" 
        Width="317px"></asp:Label></td> 
     </tr> 
     <tr> 
      <td colspan="7"> 
       &nbsp;</td> 
     </tr> 
     <tr> 
      <td colspan="7"> 


       <asp:RadioButtonList ID="RadioButtonList1" runat="server" 
        RepeatDirection="Horizontal"> 
       </asp:RadioButtonList> 


       </td> 
     </tr> 
     <tr> 
      <td style="width: 100px; height: 12px;"> 
       &nbsp;</td> 
      <td style="width: 100px; height: 12px;"> 
      </td> 
      <td style="width: 100px; height: 12px;"> 
      </td> 
      <td style="width: 100px; height: 12px;"> 
       </td> 
      <td style="width: 100px; height: 12px;"> 
       </td> 
      <td style="width: 100px; height: 12px;"> 
       <asp:Button ID="Button2" runat="server" Text="Skip" Width="55px" /></td> 
      <td style="width: 100px; height: 12px;"> 
       <asp:Button ID="BtnNext" runat="server" onclick="BtnNext_Click" Text="Next" 
        Width="70px" /> 
      </td> 
     </tr> 
    </table> 


      </td> 
     </tr> 
     <tr> 
      <td style="background-color: silver;" class="style1"> 
      </td> 
     </tr> 
    </table> 

,這是我的代碼隱藏頁:

void Page_PreRender(object sender, EventArgs e) 
    { 

     OnlineExamEntities context = new OnlineExamEntities(); 
     StringBuilder bldr = new StringBuilder(); 
     bldr.AppendFormat("var Timer = new myTimer({0},{1},'{2}','timerData');", this.timerStartValue, this.TimerInterval, this.lblTimerCount.ClientID); 
     bldr.Append("Timer.go()"); 
     ClientScript.RegisterStartupScript(this.GetType(), "TimerScript", bldr.ToString(), true); 
     ClientScript.RegisterHiddenField("timerData", timerStartValue.ToString()); 
     ///////////////////////////// 
     List<int> a = (List<int>)Session["QnumList"]; 
     List<string> resulttemp = new List<string>(); 

     int j = a[Convert.ToInt32(Session["Click"].ToString())]; 

     var q3 = ((from c in context.questions 
        orderby c.QID 
        where c.QID == j 
        select c)).SingleOrDefault(); 
     resulttemp.Add(q3.trueAns.ToString()); 
     Session["result"] = resulttemp; 

     Questionlbl.Text = q3.Question1.ToString(); 
     Image2.ImageUrl = q3.ans4.ToString(); 
     RadioButtonList1.Items.Clear(); 
     string ans1, ans2, ans3; 
     ans1 = q3.ans1.ToString(); 
     ans2 = q3.ans2.ToString(); 
     ans3 = q3.ans3.ToString(); 
     RadioButtonList1.Items.Add(String.Format("<img src='{0}'>", ans1)); 
     RadioButtonList1.Items.Add(String.Format("<img src='{0}'>", ans2)); 
     RadioButtonList1.Items.Add(String.Format("<img src='{0}'>", ans3)); 

    } 

,當我選擇一個選項,並單擊下一步按鈕,加載另一個記錄我得到這個錯誤:

* 從檢測到有潛在危險的Request.Form值客戶端(RadioButtonList1 =「*

我調整了Validate Request =」false「,但它不起作用。

回答

4

服務器警告你有人可能插入Html代碼例如,如果你想獲得FirstName但有人在裏面寫道:「alert('哈哈')」,那麼當你顯示輸入的數據時,你可能會看到警報框。取消驗證不是一件好事。您可以使用Server.HtmlEncode爲:

RadioButtonList1.Items.Add(Server.HtmlEncode(String.Format("<img src='{0}'>", ans1))); 

且用戶選擇一個值後,那麼你就可以解碼與Server.HtmlDecode

然而,在這種情況下,你只需要選擇的IMG,而不是整個img標籤,這樣你就可以設置不同的值(第二個參數是值):

RadioButtonList1.Items.Add(new ListItem(String.Format("<img src='{0}'>", ans1), ans1)); 

這樣的HTML表格將僅保存不被視爲潛在危險值的圖像路徑。