0

私はこれに何時間も立ち往生しており、初めての投稿です。値を取得して、選択した行を含む新しいページにポストバックする方法がわかりません。独自の新しいページに情報を表示します(必要に応じて詳細ビュー)

<asp:GridView ID="GameSearchGrid" runat="server"
AllowPaging = "True" AllowSorting = "True" 
    DataSourceID="EntityDataSource1" Width="502px" AutoGenerateColumns="False" >
    <Columns>
        <asp:CommandField ShowSelectButton="True" />
        <asp:BoundField DataField="SKU" HeaderText="SKU" ReadOnly="True" 
            SortExpression="SKU" Visible="false" />
        <asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" 
            SortExpression="Name" />
        <asp:BoundField DataField="GSystem" HeaderText="GSystem" ReadOnly="True" 
            SortExpression="GSystem" />
        <asp:BoundField DataField="Rating" HeaderText="Rating" ReadOnly="True" 
            SortExpression="Rating" />
        <asp:ButtonField ButtonType="Button" HeaderText = "select" Text="Select" 
        />
    </Columns>
</asp:GridView>

<asp:EntityDataSource ID="EntityDataSource1" runat="server" 
    ConnectionString="name=GameExpressEntities" 
    DefaultContainerName="GameExpressEntities" EnableFlattening="False" 
    EntitySetName="Games" 
    Select="it.[SKU], it.[Name], it.[GSystem], it.[Rating]" OrderBy="it.[Name]">
</asp:EntityDataSource>
//------------------------------------------------

void GameSearchGrid_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow row = GameSearchGrid.SelectedRow;
    Response.Redirect("~/GameDetailView.aspx");
}
4

2 に答える 2

1

Cells行のプロパティを使用してバインドされたフィールドにアクセスできます。Cells プロパティは、数値インデックスによってのみセルにアクセスできるため、アクセスする列のインデックスを知る必要があります。例えば:

string skuCellValue = row.Cells[0].Text;

インデックスをハード コーディングしたくない場合は、GridView の Columns プロパティから参照できます。たとえば、HeaderText を照合します。

GridViewRow row = GameSearchGrid.SelectedRow;
GridView gv = (GridView)row.Parent.Parent;

int colIndex = -1;
for (int i = 0; i < gv.Columns.Count; i++)
{
    if (gv.Columns[i].HeaderText == "SKU")
    {
        colIndex = i;
        break;
    }
}

// handle case when correct column was not found

string skuCellValue = row.Cells[colIndex].Text;

その後、先に進んで新しいページにリダイレクトできます。おそらく、クエリ文字列で見つかった SKU 値を指定する必要があります。

Response.Redirect("~/GameDetailView.aspx?SKU=" + skuCellValue);

リダイレクトすると、ブラウザーが新しいリクエスト (GET リクエスト) を発行するため、リダイレクト先のページは、以前のページの投稿データやその他のデータにアクセスできないことに注意してください (明示的に渡されるクエリ文字列)。

于 2012-06-15T10:21:58.580 に答える
0

ButtonField CommandNameを使用して、クリック (選択) で何を行っているかを識別します。次に、Gridview RowCommand イベントで、ボタンの名前を取得し、必要なことを行います。

私が提供したリンクにサンプルコードがあります。

于 2012-06-15T09:46:39.393 に答える