2011-07-31 22 views
0

リストビューをバインドしたいのですが、できません。私はここで間違っていることを知りません。 Ctrl + F5を押すと、ページは空になります。リストビューをバインドできません

私のコードは次のとおりです。

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> 
     <asp:ListView ID="ListView1" runat="server" DataKeyNames="Student_ID"> 
      <LayoutTemplate> 
       <table id="itemplaceholderContainer" runat="server"> 
        <tr id="itemplaceholder"> 
         <td> 
          Student ID : 
         </td> 
         <td> 
          Registration Number : 
         </td> 
         <td> 
          Student Name : 
         </td> 
        </tr> 
       </table> 
      </LayoutTemplate> 
      <ItemTemplate> 
       StudentID: 
       <asp:Label ID="LabelID" runat="server" Text='<%Eval("Student_ID") %>'></asp:Label> 
       StudentRegistrationNumber:<asp:Label ID="LabelNumber" runat="server" Text='<%Eval("StudentRegistrationNumber") %>'></asp:Label> 
       StudentName:<asp:Label ID="LabelName" runat="server" Text='<%Eval("Name") %>'></asp:Label> 
      </ItemTemplate> 

     </asp:ListView> 
    </asp:Content> 

分離コードは次のとおりです。

protected void Page_Load(object sender, EventArgs e) 
      { 
       SqlConnection cn = new SqlConnection(@"Data Source=LOCALHOST;Initial Catalog=ITCF;Integrated Security=True"); 
       SqlCommand cmd = new SqlCommand("Select * from Student", cn); 
       SqlDataAdapter da = new SqlDataAdapter(cmd); 
       DataTable dt = new DataTable(); 
da.fill(dt); 
       ListView1.DataSource = dt; 
       ListView1.DataBind(); 
      } 

答えて

2

これを試してみてください: は、最初にあなたの項目テンプレートがworngであり、また、あなたがデータadabterを埋めるために必要なデータテーブルを形成

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> 
     <asp:ListView ID="ListView1" runat="server" DataKeyNames="Student_ID"> 
      <LayoutTemplate> 
       <table id="itemplaceholderContainer" runat="server"> 
        <tr id="itemplaceholder"> 
         <td> 
          Student ID : 
         </td> 
         <td> 
          Registration Number : 
         </td> 
         <td> 
          Student Name : 
         </td> 
        </tr> 
       </table> 
      </LayoutTemplate> 
      <ItemTemplate> 
       StudentID: 
       <asp:Label ID="LabelID" runat="server" Text='<%# Eval("Student_ID") %>'></asp:Label> 
       StudentRegistrationNumber:<asp:Label ID="LabelNumber" runat="server" Text='<%# Eval("StudentRegistrationNumber") %>'></asp:Label> 
       StudentName:<asp:Label ID="LabelName" runat="server" Text='<%# Eval("Name") %>'></asp:Label> 
      </ItemTemplate> 

     </asp:ListView> 
    </asp:Content> 

分離コードは次のとおりです。あなたのコードの

protected void Page_Load(object sender, EventArgs e) 
     { 
      SqlConnection cn = new SqlConnection(@"Data Source=LOCALHOST;Initial Catalog=ITCF;Integrated Security=True"); 
      SqlCommand cmd = new SqlCommand("Select * from Student", cn); 
      SqlDataAdapter da = new SqlDataAdapter(cmd); 
      DataTable dt = new DataTable(); 

      da.Fill(dt); 

      ListView1.DataSource = dt; 
      ListView1.DataBind(); 
     } 
1

最後の3行のコードでは、背後にある全体的な話を伝える:

DataTable dt = new DataTable(); 
ListView1.DataSource = dt; 
ListView1.DataBind(); 

DataTableを空にして、ListView1というバインディングをインスタンス化しています。したがって、正しく設計されたとおりに正しく動作すると、ListViewは空のDataTableからゼロレコードを表示しています。

何かをバインドする前に、DataAdapterからデータを取得する必要があります。このような何か:

var students = new DataSet(); 
da.Fill(students, "Student"); 
if (students.Tables.Count == 1) 
{ 
    ListView1.DataSource = students.Tables[0]; 
    ListView1.DataBind(); 
} 

注:DataTableをフェッチするために、より直接的な方法があるかもしれません。私は長い時間でDataSet sおよびDataTable秒で働いていませんが、.Fill()は直接DataTableを使用し、上記DataSetに有効なテーブルの私のチェックをスキップすることができるかもしれない。)

+0

ええ..私のコードを編集する。私はda.fill(dt)を編集しました。 <%Eval( "Student_ID '%> –

+0

@Abid Ali:あなたのページバインディングタグも正しくないように見えます。<%Eval(" Student_ID ")% > 'はおそらく' <%#Eval( "Student_ID")%> 'でなければなりません。'# 'はタグの一部です。 – David

関連する問題