簡體   English   中英

如何在存儲數據源的過程中訪問值

[英]How to access values where datasource is stored procedure

這是我的列表視圖:

<asp:ListView ID="lstList" runat="server" OnItemDataBound="lstList_ItemDataBound" 
              OnPagePropertiesChanging="lstList_PagePropertiesChanging">
<LayoutTemplate> 
    <table border="1" cellpadding="2" cellspacing="0" id="Table1" runat="server" 
           class="TableCSS" style="width:100%;">
        <tr id="Tr1" runat="server" class="TableHeader">                        
            <td>Description</td>                       
        </tr>

        <tr id="ItemPlaceholder" runat="server"></tr>
        <tr id="Tr2" runat="server">
            <td id="Td7" runat="server" align="right" colspan="5" class="TableData"> 
                <asp:DataPager ID="lvDataPager1" runat="server" 
                               PagedControlID="lstList" PageSize="6">
                <Fields>
                    <asp:NumericPagerField ButtonType="Link" />
                </Fields>
            </asp:DataPager>
            </td>
        </tr>
    </table>
</LayoutTemplate>
<EmptyDataTemplate>No match</EmptyDataTemplate>
<ItemTemplate>
    <tr class="TableData">                                
        <td><asp:Label ID="lblDescription" runat="server"/></td>                   
    </tr>
</ItemTemplate>
</asp:ListView>

listview的數據源由dbml存儲過程設置。 綁定列表視圖的函數是

public List<sp_ListResult> GetList(byte id)
{ 
    List<sp_ListResult> results = new List<sp_ListResult>();
    var en = dataContext.dc.sp_List(id).GetEnumerator();
    while (en.MoveNext())
    {
        results.Add(en.Current);
    }
    return results;
}

lstList.DataSource = GetList(id);

我想獲取描述值並將其設置為listview標簽,但無法執行。

protected void lstList_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        //Get the item object.
        ListViewDataItem dataItem = (ListViewDataItem)e.Item;            

        // throws error System.InvalidCastException: 'Unable to cast object
        DataRowView rowView = e.Item.DataItem as DataRowView;     

        String description = rowView["Description"].ToString();
    }
}

您不是在綁定DataTable,而是在List<class> ,所以必須轉換為正確的類型。

sp_ListResult rowView = e.Item.DataItem as sp_ListResult; 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM