Get GridView Selected Row Values using Page Previous Page

社会主义新天地 提交于 2019-12-22 18:33:28

问题


I have a gridView in ConsumerList page. When Edit button is clicked (in each row), it is navigated to a EditConsumer page. In the EditConsumer page, I need to get the values of the selected row using Page.PreviousPage. How to get these values?

    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);
        }
    }

MARKUP

  <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>

回答1:


Note: PreviousPage is only != null with cross page posting, Response.Redirect will not work. So you could for example use Server.Transfer.

You could provide a public property SelectedConsumer that returns a custom object of the curerently selected row in the 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;
    }
}

Now you only have to cast the PreviousPage property to the correct page type:

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



回答2:


Check how to use Page.PreviousPage.



来源:https://stackoverflow.com/questions/10831647/get-gridview-selected-row-values-using-page-previous-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!