簡體   English   中英

ASP.NET:從選定的gridview項中獲取數據-linq-to-entities數據源

[英]ASP.NET : Get data from selected gridview item - linq-to-entities data source

我有一個帶有c#代碼隱藏的asp.net頁面。 當網格視圖上的選定索引發生更改時,我有一個在C#中觸發的事件...該網格視圖綁定到一個實體數據源,並且我需要找到一種方法來在對象ID后面告訴我的代碼它在調用selected_index_changed()方法時選擇的。 關於如何最好地做到這一點的任何想法?

當前事件處理程序代碼:

protected void VehiclesGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ChangeAttemptedId && !IsSavedId)
            {
                Alert.Show("Dispatch assignment saved... (But you forgot to click Confirm or Cancel!)");
            }
            IsSavedId = false;
            ChangeAttemptedId = true;
            int selectedIndex = VehiclesGridView.SelectedIndex + 1;
            getNextRide(selectedIndex); //TODO: FIX 
        }

ASP.NET代碼:

<asp:EntityDataSource ID="VehiclesEDS" runat="server" EnableDelete="True" 
        EnableFlattening="False" EnableInsert="True" EnableUpdate="True" 
        EntitySetName="Vehicles" ContextTypeName="RamRideOps.RamRideOpsEntities" >
    </asp:EntityDataSource>

<asp:UpdatePanel ID="SelectCarUP" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:GridView ID="VehiclesGridView" runat="server" AllowPaging="True" 
                AllowSorting="True" DataSourceID="VehiclesEDS" AutoGenerateColumns="False" 
                onselectedindexchanged="VehiclesGridView_SelectedIndexChanged" 
                BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" 
                CellPadding="3" GridLines="Vertical" ShowHeaderWhenEmpty="True" AutoPostBack="True">
                <AlternatingRowStyle BackColor="#DCDCDC" />
                <Columns>
                    <asp:TemplateField ShowHeader="False">
                        <ItemTemplate>
                            <asp:LinkButton ID="GVSelectButton" runat="server" CausesValidation="False" 
                                CommandName="Select" Text="Select"></asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="CarNum" HeaderText="Car" ReadOnly="True" 
                        SortExpression="CarNum" />
                    <asp:BoundField DataField="CurrPassengers" HeaderText="Passengers" 
                        ReadOnly="True" SortExpression="CurrPassengers" />
                    <asp:BoundField DataField="MaxPassengers" HeaderText="Capacity" ReadOnly="True" 
                        SortExpression="MaxPassengers" />
                    <asp:BoundField DataField="Status" HeaderText="Status" ReadOnly="True" 
                        SortExpression="Status" />
                    <asp:BoundField DataField="StartAdd" HeaderText="Pick-Up Address" 
                        ReadOnly="True" SortExpression="StartAdd" />
                    <asp:BoundField DataField="EndAdd" HeaderText="Drop-Off Address" 
                        ReadOnly="True" SortExpression="EndAdd" />
                    <asp:BoundField DataField="AvgRideTime" HeaderText="Avg. Ride Time" 
                        ReadOnly="True" SortExpression="AvgRideTime" />
                </Columns>
                <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
                <HeaderStyle BackColor="#004812" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
                <SelectedRowStyle BackColor="#C6940D" Font-Bold="True" ForeColor="White" />
                <SortedAscendingCellStyle BackColor="#F1F1F1" />
                <SortedAscendingHeaderStyle BackColor="#C6940D" />
                <SortedDescendingCellStyle BackColor="#CAC9C9" />
                <SortedDescendingHeaderStyle BackColor="#9F770B" />
            </asp:GridView>
        </ContentTemplate>
    </asp:UpdatePanel>

將EventArgs e更改為GridViewSelectEventArgs時出錯:

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS0123: No overload for 'VehiclesGridView_SelectedIndexChanged' matches delegate 'System.EventHandler'

Source Error:


Line 90:     <asp:UpdatePanel ID="SelectCarUP" runat="server" UpdateMode="Conditional">
Line 91:         <ContentTemplate>
Line 92:             <asp:GridView ID="VehiclesGridView" runat="server" AllowPaging="True" 
Line 93:                 AllowSorting="True" DataSourceID="VehiclesEDS" AutoGenerateColumns="False" 
Line 94:                 onselectedindexchanged="VehiclesGridView_SelectedIndexChanged" 

如果您要傳遞給getNextRide的參數確實與所選索引相同,那么我將制作一個這樣的事件處理程序

    protected void VehiclesGridView_SelectedIndexChanged(object sender, GridViewSelectEventArgs e)
    {
        if (ChangeAttemptedId && !IsSavedId)
        {
            Alert.Show("Dispatch assignment saved... (But you forgot to click Confirm or Cancel!)");
        }
        IsSavedId = false;
        ChangeAttemptedId = true;

        int selectedIndex = e.NewSelectedIndex;
        getNextRide(selectedIndex); //TODO: FIX             
    }

同樣,在事件處理程序內部,您可以像這樣訪問網格視圖的各個成員: VehiclesGridView.Rows[e.NewSelectedIndex].Cells[i]其中i是單元格的索引。

另外,您可以在設置VehiclesGridView數據源的行中發布該行,以便我可以提出更清晰的答案

暫無
暫無

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

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