2017-02-09 44 views
0

這裏是C#文件代碼,我希望每個輸出都能得到單選按鈕,這樣我就可以像在「mcqs」中那樣選擇選項。單選按鈕通過C#文件中的內部html返回答案選擇

public HtmlString questions_sec1() 
{ 
    int userid = Convert.ToInt32(Session["USERID"]); 
    List<DAO_getQuestiones> get_question_sec1 = new List<DAO_getQuestiones>(DAO_getQuestiones.get_question_sec1(userid)); // data to  fech first section 
    string content = ""; 
    HtmlString theEnvelopePlease = null; 
    try 
    { 
     foreach (var obj in get_question_sec1) 
     { 
      content = content + "<tr><td> " + obj.get_question_id + "</td> <td>" + obj.get_answer_question + "</td><td>"+ <input type="radio" name="d" value="data"> DATA +"</td></tr>"; 
     } 

     theEnvelopePlease = new HtmlString(content); 
     return theEnvelopePlease;  
    } 
    catch (Exception ex) 
    { 
     content = content + ex; 
     theEnvelopePlease = new HtmlString(content); 
     return theEnvelopePlease; 
    } 
} 

這裏是HTML呼叫時,返回CS文件的功能渲染代碼:

<table> 
    <thead> 
    <tr> 
     <th>Sr.</th> 
     <th>Questions</th> 
     <th>Answers</th> 
    </tr> 
    </thead> 
    <tbody> 
    < %:questions_sec1() % > 
    </tbody> 
</table> 

問:我想單選按鈕,每出答案看跌,這樣我可以選擇一個選項和一件事。

+0

我建議你看看[RadioButtonList Control](https://msdn.microsoft.com/en-us/library/cc295394.aspx)。 – VDWWD

回答

0

很難將帶有HTML字符串的表單元素添加到ASP.NET表單中。

相反,我建議你使用 '中繼':

<asp:Repeater id="Repeater1" runat="server"> 

    <HeaderTemplate> 
    <table> 
    <thead> 
     <tr> 
      <th>Sr.</th> 
      <th>Questions</th> 
      <th>Answers</th> 
     </tr> 
    </thead> 
    </HeaderTemplate> 

    <ItemTemplate> 
    <tr> 
     <td> <%#Eval("get_question_id ") %> </td> 
     <td> <%#Eval("get_answer_question ") %> </td> 
     <td> <asp:RadioButton ID="RadioButton" runat="Server" Text="DATA" /> </td> 
    </tr> 
    </ItemTemplate> 

    <FooterTemplate> 
    </table> 
    </FooterTemplate> 

</asp:Repeater> 

而在你的cs文件,將數據綁定到Repeater這樣的:

Repeater1.DataSource = get_question_sec1; 
Repeater1.DataBind(); 

這裏是MSDN直放站的鏈接:https://msdn.microsoft.com/en-US/library/system.web.ui.webcontrols.repeater.itemtemplate(v=vs.110).aspx

+0

感謝您的回覆,但我不想在asp.net的工具箱控件中使用biukt。 – ghasif

0

感謝您的回覆,但我不想讓您從asp.net的工具箱中建立控制。