如何使用asp.net将gridview值发送到另一个页面?

时间:2018-02-08 15:12:29

标签: c# asp.net gridview

我有两个表,其中包含registration.aspx和reportview.aspx。在reportview.aspx中我从数据库中检索值并绑定它。 我的reportview.aspx代码是:

 <asp:GridView ID="grduser" AutoGenerateColumns="false" runat="server" DataKeyNames="Id" CssClass="table table-striped table-bordered table-hover" BorderWidth="2px" OnRowCommand="grduser_RowCommand">
                        <%--<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" DataKeyNames="Id" CssClass="table table-striped table-bordered table-hover" OnRowCancelingEdit="grduser_RowCancelingEdit" OnRowEditing="grduser_RowEditing" OnRowDeleting="grduser_RowDeleting" OnRowUpdating="grduser_RowUpdating">--%>
                        <Columns>
                            <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" HeaderStyle-CssClass="visible-lg" ItemStyle-CssClass="visible-lg" ItemStyle-Wrap="true"/>
                            <asp:BoundField DataField="Firstname" HeaderText="Firstname" SortExpression="Firstname" HeaderStyle-CssClass="visible-lg" ItemStyle-CssClass="visible-lg" ItemStyle-Wrap="true" />
                            <asp:BoundField DataField="Lastname" HeaderText="Lastname" SortExpression="Lastname" HeaderStyle-CssClass="visible-lg" ItemStyle-CssClass="visible-lg" ItemStyle-Wrap="true" />
                            <asp:BoundField DataField="Middlename" HeaderText="Middlename" SortExpression="Middlename" HeaderStyle-CssClass="visible-lg" ItemStyle-CssClass="visible-lg" ItemStyle-Wrap="true" />
                            <asp:BoundField DataField="Password" HeaderText="Password" SortExpression="Password" HeaderStyle-CssClass="visible-lg" ItemStyle-CssClass="visible-lg" ItemStyle-Wrap="true" />
                            <asp:BoundField DataField="ConfirmPassword" HeaderText="Confirm Password" SortExpression="ConfirmPassword" HeaderStyle-CssClass="visible-lg" ItemStyle-CssClass="visible-lg" ItemStyle-Wrap="true"/>

                            <asp:BoundField DataField="DOB" HeaderText="DOB" SortExpression="DOB" HeaderStyle-CssClass="visible-lg" ItemStyle-CssClass="visible-lg" ItemStyle-Wrap="true" ApplyFormatInEditMode="true" DataFormatString="{0:d}"/>
                            <asp:BoundField DataField="Address1" HeaderText="Address1" SortExpression="Address1" HeaderStyle-CssClass="visible-lg" ItemStyle-CssClass="visible-lg" ItemStyle-Wrap="true" />
                            <asp:BoundField DataField="Address2" HeaderText="Address2" SortExpression="Firstname" HeaderStyle-CssClass="visible-lg" ItemStyle-CssClass="visible-lg" ItemStyle-Wrap="true"/>
                            <asp:BoundField DataField="Dateofjoin" HeaderText="Dateofjoin" SortExpression="Dateofjoin" HeaderStyle-CssClass="visible-lg" ItemStyle-CssClass="visible-lg" ItemStyle-Wrap="true" ApplyFormatInEditMode="true" DataFormatString="{0:d}" />
                            <%-- <asp:BoundField DataField="Active" HeaderText="Active" SortExpression="Active" HeaderStyle-CssClass="visible-lg" ItemStyle-CssClass="visible-lg" />--%>
                            <asp:TemplateField ItemStyle-Wrap="true">
                                <%-- <EditItemTemplate>--%>
                                <%--    <asp:Button ID="btnUpdate" CssClass="btn btn-primary" CommandName="Update" Text="Update"  runat="server" />
                                    <asp:Button ID="btnCancel" CssClass="btn btn-primary" CommandName="Cancel" Text="Cancel"  runat="server" />
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Button ID="btnEdit" CssClass="btn btn-primary" CommandName="Edit" Text="Edit" runat="server"/>
                                    <asp:Button ID="btnDelete" CssClass="btn btn-primary" CommandName="Delete" Text="Delete" runat="server" />
                                </ItemTemplate>--%>

                                <ItemTemplate>
                                    <asp:Button ID="btnEdit" Text="Edit" CssClass="btn btn-primary" runat="server" CommandArgument='<%#Eval("Id")%>' CommandName="EditUser" />
                                    <asp:Button ID="btnDelete" Text="Delete" CssClass="btn btn-primary" CommandName="DeleteUser" runat="server" ></asp:Button>
                                    <%--<asp:LinkButton ID="btnCancel" Text="Cancel" CommandName="CancelUser" runat="server"></asp:LinkButton>--%>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>


protected void grduser_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "EditUser")
            {

                //int index =Convert.ToInt32(grduser.DataKeys[e.CommandArgument]).ToString();
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = grduser.Rows[index];
                Response.Redirect("~/Registration.aspx?Id=" + row.Cells[0].Text);
            }
        }

在rowcommand事件中,我能够检索rowindex但是当我运行应用程序时,我得到像Index这样的错误超出范围。必须是非负数且小于集合的大小。 参数名称:index&#39; 我通过registration.aspx页面中的查询字符串获取id,并使用数据集绑定gridview值。

0 个答案:

没有答案