Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用上一页获取GridView所选行值_C#_Asp.net_.net_Vb.net_Gridview - Fatal编程技术网

C# 使用上一页获取GridView所选行值

C# 使用上一页获取GridView所选行值,c#,asp.net,.net,vb.net,gridview,C#,Asp.net,.net,Vb.net,Gridview,我在ConsumerList页面中有一个gridView。单击“编辑”按钮(在每行中)时,它将导航到EditConsumer页面。在EditConsumer页面中,我需要使用page.PreviousPage获取所选行的值。如何获得这些值 public void Consumer_RowCommand(Object sender, GridViewCommandEventArgs e) { if (e.CommandName == "CustomEdit")

我在ConsumerList页面中有一个gridView。单击“编辑”按钮(在每行中)时,它将导航到EditConsumer页面。在EditConsumer页面中,我需要使用page.PreviousPage获取所选行的值。如何获得这些值

    public void Consumer_RowCommand(Object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName == "CustomEdit")
        {
            int rowIndex = Convert.ToInt32(e.CommandArgument);
            string consumerID = grdConsumers.Rows[rowIndex].Cells[1].Text;
            string consumerName = grdConsumers.Rows[rowIndex].Cells[2].Text;
            string consumerUrl = grdConsumers.Rows[rowIndex].Cells[3].Text;
            string consumerStatus = grdConsumers.Rows[rowIndex].Cells[4].Text;

            Response.Redirect("EditConsumer.aspx?RowIndex=" + rowIndex);
        }
    }
加价

  <asp:GridView ID="grdConsumers" runat="server" AutoGenerateColumns="False" CssClass="resultGridTable"
                GridLines="None" EnableViewState="True" AllowSorting="True" OnSorting="Consumers_Sorting" OnRowCommand="Consumer_RowCommand">
                <AlternatingRowStyle BackColor="#E5E5E5" />
                <Columns>

                   <asp:TemplateField HeaderText="Action">
                        <ItemTemplate>

                             <asp:Button ID="btnView" runat="server" CssClass="actionButtonView"
                                  Text="VIEW" Style="width: 40px" BackColor="Orange"
                                  Font-Bold="True" ForeColor="White" /> 

                             <asp:Button ID="btnEdit" runat="server" CommandName="CustomEdit" CommandArgument="<%# ((GridViewRow)Container).RowIndex %>"
                              CssClass="actionButtonEdit"
                                  Text="EDIT" Style="width: 35px" BackColor="Orange"
                                  Font-Bold="True" ForeColor="White" /> 

                             <asp:Button ID="btnDelete" runat="server" CssClass="actionButtonDelete"
                                  Text="DELETE" Style="width: 55px" BackColor="Orange"
                                  Font-Bold="True" ForeColor="White" />

                             <asp:Button ID="btnPing" runat="server" CssClass="actionButtonPing"
                                  Text="PING" Style="width: 35px" BackColor="Orange"
                                  Font-Bold="True" ForeColor="White" />

                         </ItemTemplate>
                    </asp:TemplateField>

                    <asp:BoundField HeaderText="Consumer ID" DataField="ConsumerID" SortExpression="ConsumerID"></asp:BoundField>
                    <asp:BoundField HeaderText="Consumer Name" DataField="ConsumerName" SortExpression="ConsumerName"></asp:BoundField>
                    <asp:BoundField HeaderText="Consumer URL" DataField="ConsumerURL" SortExpression="ConsumerURL"></asp:BoundField>
                    <asp:BoundField HeaderText="Status" DataField="Status" SortExpression="Status"></asp:BoundField>



                </Columns>
            </asp:GridView>

检查

注意
上一页
只是
!=空值
与,将不起作用。例如,您可以使用

您可以提供公共属性
SelectedConsumer
,该属性返回
GridView
中当前选定行的自定义对象

public Consumer SelectedConsumer
{
    get {
        if (grdConsumers.SelectedRow == null)
            return null;

        var c = new Consumer();
        c.consumerID = grdConsumers.SelectedRow.Cells[1].Text;
        c.consumerName = grdConsumers.SelectedRow.Cells[2].Text;
        c.consumerUrl = grdConsumers.SelectedRow.Cells[3].Text;
        c.consumerStatus = grdConsumers.SelectedRow.Cells[4].Text;

        return c;
    }
}
现在,您只需将
PreviousPage
属性强制转换为正确的页面类型:

var consumerList = (ConsumerList)Page.PreviousPage;
var selectedConsumer = consumerList.SelectedConsumer;