必须在ListView的“ListView1”上定义ItemTemplate。任何人帮我解决这个错误

时间:2013-10-28 07:12:21

标签: c# .net

 protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(str);
        SqlCommand cmd = new SqlCommand("insert into empoffice(empname,department,designation,empstatus,reportingto,grade,emplevel,doj) values('" + TextBox1.Text + "','" + DropDownList1.SelectedItem.Text + "','" + DropDownList2.SelectedItem.Text + "','" + DropDownList3.SelectedItem.Text + "','" + DropDownList4.SelectedItem.Text + "','" + DropDownList5.SelectedItem.Text + "','" + DropDownList6.SelectedItem.Text + "','" + TextBox2.Text + "')", con);

        con.Open();
        cmd.ExecuteReader();

        con.Close();
        SqlDataAdapter da = new SqlDataAdapter("select *from empoffice",con);

        DataSet ds = new DataSet();

        da.Fill(ds);

        ListView1.DataSource = ds;

        ListView1.DataBind();

     }

2 个答案:

答案 0 :(得分:0)

您需要为ListView创建一个ItemTemplate,因此它知道在加载数据时如何显示数据。

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.itemtemplate.aspx

http://highoncoding.com/Articles/396_Getting_Started_with_the_ListView_Control.aspx

How to bind a ListView with Directories and Files in C#

http://msdn.microsoft.com/en-us/library/bb398790%28v=vs.100%29.aspx#CreatingTemplatesForTheListViewControl

在这些链接的某处你应该找到答案。通过简单的谷歌搜索您获得的错误将对您有所帮助。在发布问题之前,请先尝试自己做一些研究。

答案 1 :(得分:0)

您需要将<ItemTemplate></ItemTemplate>对标签放入ListView中,并在下面加上一些内容。我还包括一个LayoutTemplate。

<asp:ListView ID="ListView1" runat="server">
  <LayoutTemplate>
    <table runat="server" id="table1" >
      <tr runat="server" id="itemPlaceholder" ></tr>
    </table>
  </LayoutTemplate>
  <ItemTemplate>
    <tr runat="server">
      <td runat="server">
         <%-- Data-bound content. --%>
         <asp:Label ID="NameLabel" runat="server" Text='<%#Eval("Name") %>' />
      </td>
    </tr>
  </ItemTemplate>
</asp:ListView>