2016-04-29 62 views
1

.aspxページでは、質問フォームのデータベースを表示するためにLabel Controlを使用しました。 ラジオボタンは、特定の質問に関連する4つのオプションを表示するために使用されますが、最後に、私は特定の質問の回答を保存する隠しフィールドを使用しました。 そして、私はボタンコントロールを撮影しました。これはonclickイベントを作成しました。そこで、スコアを生成する操作を実行します。エラー: - データバインディング: 'System.Data.DataRowView'に 'Optiont4'という名前のプロパティが含まれていません

.aspx.csページのボタンのonclickイベントでは、下記のコードを使用してaspxページのコントロールを取得しました。さらに、if文を使用してラジオボタンがアクティブであり、この "selans"を使ってvarialbe "selans"に対応する値を格納している場合は、チェックされたラジオボタンが正解かどうかを調べるために非表示フィールドの値と比較します。答えは正しいです。つまり、 "selans"の値が隠しフィールド(実際の答え)の値と一致し、変数 "count"(最初は値0で初期化されます)がそれに応じてインクリメントされ、このコードはすべて "forループ"いいえまで実行されます。 GridViewのコントロールの数(GridViewが新しいコントロールを生成するすべてのレコードについて、質問の数に関連付けることができます)。

しかし、私はそれを実行したとき、私はこのエラーを取得しています: - ここに

DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Optiont4'. 
 

 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
 

 
Exception Details: System.Web.HttpException: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Optiont4'. 
 

 
Source Error: 
 

 

 
Line 115:   <asp:RadioButton ID="rad2" runat="server" Text='<%#Eval("Option2") %>' GroupName="A" /> 
 
Line 116:   <asp:RadioButton ID="rad3" runat="server" Text='<%#Eval("Option3") %>' GroupName="A" /> 
 
Line 117:   <asp:RadioButton ID="rad4" runat="server" Text='<%#Eval("Optiont4")%>' GroupName="A" /> 
 
Line 118:   <asp:HiddenField ID="hf" runat="server" Value='<%#Eval("CorrectAns")%>' /> 
 
Line 119: 
 

 
Source File: e:\Way2Success\Student\Examdemo.aspx Line: 117

エラー行番号は

が私のコードを見てください.aspxのページでは30であります。私にミスをしていて、解決策が何であるかを教えてください。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Examdemo.aspx.cs" Inherits="Student_Examdemo" %> 
 
<!DOCTYPE html> 
 
<html xmlns="http://www.w3.org/1999/xhtml"> 
 
<head runat="server"> 
 
    <title></title> 
 
    </head> 
 
<body> 
 
    <form id="form2" runat="server"> 
 
     <div> 
 
<div id="tabs"> 
 
    <ul> 
 
     <li><a href="#tabs-1">Tab 1</a></li> 
 
     <li><a href="#tabs-2">Tab 2</a></li> 
 
     <li><a href="#tabs-3">Tab 3</a></li> 
 
     <li><a href="#tabs-4">Tab 4</a></li> 
 
     <li><a href="#tabs-5">Tab 5</a></li> 
 
    </ul> 
 
    <div id="tabs-1"> 
 
     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" > 
 
    <Columns> 
 
     <asp:TemplateField> 
 
      <ItemTemplate> 
 
      <asp:Label ID="Label1" runat="server" Text='<%#Eval("Question") %>'></asp:Label> 
 
      <br /> 
 
      <br /> 
 
      <br /> 
 
      <asp:RadioButton ID="rad1" runat="server" Text='<%#Eval("Option1") %>' GroupName="A" /> 
 
      <asp:RadioButton ID="rad2" runat="server" Text='<%#Eval("Option2") %>' GroupName="A" /> 
 
      <asp:RadioButton ID="rad3" runat="server" Text='<%#Eval("Option3") %>' GroupName="A" /> 
 
      <asp:RadioButton ID="rad4" runat="server" Text='<%#Eval("Option4") %>' GroupName="A" /> 
 
      <asp:HiddenField ID="hf" runat="server" Value='<%#Eval("CorrectAns") %>' /> 
 

 
     <br /> 
 
     <br /> 
 

 
       </ItemTemplate> 
 
     </asp:TemplateField> 
 
    </Columns> 
 
</asp:GridView> 
 
    
 
    </div> 
 
    <div id="tabs-2"> 
 
     
 

 
     <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" > 
 
    <Columns> 
 
     <asp:TemplateField> 
 
      <ItemTemplate> 
 
      <asp:Label ID="Label1" runat="server" Text='<%#Eval("Question") %>'></asp:Label> 
 
      <br /> 
 
      <br /> 
 
      <br /> 
 
      <asp:RadioButton ID="rad1" runat="server" Text='<%#Eval("Option1") %>' GroupName="A" /> 
 
      <asp:RadioButton ID="rad2" runat="server" Text='<%#Eval("Option2") %>' GroupName="A" /> 
 
      <asp:RadioButton ID="rad3" runat="server" Text='<%#Eval("Option3") %>' GroupName="A" /> 
 
      <asp:RadioButton ID="rad4" runat="server" Text='<%#Eval("Optiont4")%>' GroupName="A" /> 
 
      <asp:HiddenField ID="hf" runat="server" Value='<%#Eval("CorrectAns")%>' /> 
 

 
     <br /> 
 
     <br /> 
 

 
       </ItemTemplate> 
 
     </asp:TemplateField> 
 
    </Columns> 
 
</asp:GridView> 
 

 
    </div> 
 
    <div id="tabs-3"> 
 
     Tab 3 Content 
 
    </div> 
 
    <div id="tabs-4"> 
 
     Tab 4 Content 
 
    </div> 
 
    <div id="tabs-5"> 
 
     Tab 5 Content 
 
    </div> 
 
</div> 
 
<input type="button" id="btnPrevious" value="Previous" style = "display:none"/> 
 
<input type="button" id="btnNext" value="Next" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
 
      
 
      <asp:Button class="panelButton" runat="server" Text="Finish the exam" ClientIDMode="Static" OnClick="btn_Click" /> 
 
      <br /> 
 
    </div> 
 
    </form> 
 
</body> 
 
</html>

.aspx.cs: - - :.aspxの

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data.SqlClient; 
using System.Data; 
using System.Configuration; 


public partial class Student_Examdemo : System.Web.UI.Page 
{ 
protected void Page_Load(object sender, EventArgs e) 
{ 
if (!IsPostBack) 
    { 
     GridView1.DataSource = GetData("SELECT top 2 Question, Option1, Option2, Option3, Option4, CorrectAns, Explanation FROM Questions"); 
     GridView1.DataBind(); 
     GridView2.DataSource = GetData("SELECT top 2 Question, Option1, Option2, Option3, Option4, CorrectAns, Explanation FROM Questions WHERE SectionId=2"); 
     GridView2.DataBind(); 

private DataSet GetData(string query) 
{ 
    string conString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 
    SqlCommand cmd = new SqlCommand(query); 
    using (SqlConnection con = new SqlConnection(conString)) 
    { 
     using (SqlDataAdapter sda = new SqlDataAdapter()) 
     { 
      cmd.Connection = con; 
      sda.SelectCommand = cmd; 
      using (DataSet ds = new DataSet()) 
      { 
       sda.Fill(ds); 
       return ds; 
      } 
     } 
    } 
} 

protected void btn_Click(object sender, EventArgs e) 
{ 

      RadioButton r1, r2, r3, r4; 
      HiddenField hdn; 
      int count = 0; 
      int neg = 0; 
      int total; 
      int totalf=0; 
      int totals=0; 
      int totalt; 
      int totalfo; 
      int totalfi; 
      string selans = "-1"; 
      for (int i = 0; i < GridView1.Rows.Count; i++) 
      { 
       r1 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad1"); 
       r2 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad2"); 
       r3 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad3"); 
       r4 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad4"); 
       hdn = (HiddenField)GridView1.Rows[i].Cells[0].FindControl("hf"); 
       if (r1.Checked) 
       { 
        selans = r1.Text; 
       } 
       else if (r2.Checked) 
       { 
        selans = r2.Text; 
       } 
       else if (r3.Checked) 
       { 
        selans = r3.Text; 
       } 
       else if (r4.Checked) 
       { 
        selans = r4.Text; 
       } 

       if (hdn.Value == selans) 
       { 
        count++; 
       } 
       else 
       { 
        neg--; 
       } 

       totalf = count + neg; 


      } 

    for (int i = 0; i < GridView2.Rows.Count; i++) 
    { 
     r1 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad1"); 
     r2 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad2"); 
     r3 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad3"); 
     r4 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad4"); 
     hdn = (HiddenField)GridView2.Rows[i].Cells[0].FindControl("hf"); 
     if (r1.Checked) 
     { 
      selans = r1.Text; 
     } 
     else if (r2.Checked) 
     { 
      selans = r2.Text; 
     } 
     else if (r3.Checked) 
     { 
      selans = r3.Text; 
     } 
     else if (r4.Checked) 
     { 
      selans = r4.Text; 
     } 

     if (hdn.Value == selans) 
     { 
      count++; 
     } 
     else 
     { 
      neg--; 
     } 

     totals = count + neg; 

    } 
    total = totalf + totals; 
    Session["score"] = total; 

} 


} 
+0

あるべきではなくoptiont4の他人のようにオプション4れることになっていますか? –

答えて

1

あなたのGridView2定義で入力ミスがあります。

<asp:RadioButton ID="rad4" runat="server" Text='<%#Eval("Optiont4")%>' GroupName="A" /> 

<asp:RadioButton ID="rad4" runat="server" Text='<%#Eval("Option4")%>' GroupName="A" /> 
関連する問題