在asp:ListView控件中选择项会抛出错误

时间:2012-10-29 15:58:23

标签: asp.net listview data-binding select asplinkbutton

这是我的ASP代码:

<asp:ListView ID="DocsListView" OnSelectedIndexChanging="DocsListView_SelectedIndexChanging" DataKeyNames="DocID" OnSorting="DocsListView_Sorting" runat="server">
  <LayoutTemplate>
    <table>
      <thead style="background-color: #336699; color: White;">
        <th align="left">Selection</th>
        <th align="left"><asp:LinkButton runat="server" ID="DocIDSortButton" Text="DocID" CommandName="Sort" CommandArgument="DocID" style="color: White;" /></th>
        <th align="left"><asp:LinkButton runat="server" ID="DocTitleSortButton" Text="DocTitle" CommandName="Sort" CommandArgument="DocTitle" style="color: White;" /></th>
        <th align="left">DocVer</th>
        <th align="left">DocType</th>
      </thead>
      <tbody>
        <asp:PlaceHolder runat="server" ID="itemPlaceholder" />
      </tbody>
    </table>
  </LayoutTemplate>
  <ItemTemplate>
    <tr>
      <td><asp:LinkButton ID="SelectButton" runat="server" CommandName="Select" Text="Select" /></td>
      <td><asp:Label ID="DocID" runat="server" Text='<%#Eval("DocID") %>' /></td>
      <td><asp:Label ID="DocTitle" runat="server" Text='<%#Eval("DocTitle") %>' /></td>
      <td><asp:Label ID="DocVer" runat="server" Text='<%#Eval("DocVer") %>' /></td>
      <td><asp:Label ID="DocType" runat="server" Text='<%#Eval("DocType") %>' /></td>
    </tr>
  </ItemTemplate>
  <SelectedItemTemplate>
    <tr style="background-color:#B0C4DE">
      <td><asp:LinkButton ID="SelectButton" runat="server" CommandName="Select" Text="Select" /></td>
      <td><asp:Label ID="DocID" runat="server" Text='<%#Eval("DocID") %>' /></td>
      <td><asp:Label ID="DocTitle" runat="server" Text='<%#Eval("DocTitle") %>' /></td>
      <td><asp:Label ID="DocVer" runat="server" Text='<%#Eval("DocVer") %>' /></td>
      <td><asp:Label ID="DocType" runat="server" Text='<%#Eval("DocType") %>' /></td>
    </tr>
  </SelectedItemTemplate>
</asp:ListView>

我不会将ListView数据绑定到SQL表,而是我自己创建的DataTable。这是代码:

private DataTable InstantiateDataTable()
{
    DataTable dataTable;

    dataTable = new DataTable();
    dataTable.Columns.Add(DocID, Type.GetType(typeof(string).ToString()));
    dataTable.Columns.Add(DocTitle, Type.GetType(typeof(string).ToString()));
    dataTable.Columns.Add(DocVer, Type.GetType(typeof(string).ToString()));
    dataTable.Columns.Add(DocType, Type.GetType(typeof(string).ToString()));

    return dataTable;
}

private void DataBindToListView()
{
    SPList list;
    DataTable docs;
    DataRow row;

    // The data for ListView comes from a SharePoint list
    using (SPWeb web = this.Web)
    {
        docs = this.InstantiateDataTable();
        list = web.Lists[ListTitle];
        foreach (SPListItem item in list.Items)
        {
            row = docs.NewRow();
            row[DocID] = item[DocID] != null ? item[DocID].ToString() : string.Empty;
            row[DocTitle] = item[DocTitle] != null ? item[DocTitle].ToString() : string.Empty;
            row[DocVer] = item[DocVer] != null ? item[DocVer].ToString() : string.Empty;
            row[DocType] = item[DocType] != null ? item[DocType].ToString() : string.Empty;

            docs.Rows.Add(row);
        }
    }

    docs.AcceptChanges();
    DocsListView.DataSource = docs;
    DocsListView.DataBind();
}

最后,这是OnSelectedIndexChanging事件的处理程序:

protected void DocsListView_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
{
    string docID;

    // Sets the selected index for the ListView
    DocsListView.SelectedIndex = e.NewSelectedIndex;

    // I am able to get the accurate docID value
    docID = DocsListView.SelectedDataKey.Value.ToString();

    // I need it to get the visual representation of the selection
    this.DataBindToListView();
}

当我运行代码时,单击项目的Select链接按钮时出现错误。错误发生在this.DataBindToListView();上。这是DocsListView_SelectedIndexChanging处理程序的最后一行。它不会回发。代码有什么问题?我是否需要为选择编写一些处理程序,因为我没有使用SQL表作为数据绑定源?感谢。

0 个答案:

没有答案