简体   繁体   中英

How to get Gridview Cell values into corresponding textbox values on another page?

This is a simple question but I have a gridview with Boundfields for ID, Name, Salary. I have a Item template field that will bring the user to another page with textboxes. I want it so that when the user clicks the hyperlink to update, they are led to a page with textboxes that are preset to the values from the specific row in the gridview so that the user can edit and update.

        protected void lnkEditUpdate_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in gvEmployee.Rows)
        {
            Employee newe = new Employee();
            newe.ID = int.Parse(((BoundField)row.FindControl("txtID")).Text);
            newe.Name = ((BoundField)row.FindControl("lblName")).Text;
            newe.Salary = ((BoundField)row.FindControl("Salary")).Text;

            Session["Update"] = newEmployee;
        }
        Response.Redirect("~/EditUpdateEmployee.aspx");
    }

.aspx

    <asp:GridView ID="gvEmployee" runat="server" AutoGenerateColumns="False" ShowFooter="True" DataKeyNames="ID" CellPadding="4" ForeColor="#333333" GridLines="None"
     DataSourceID="EmployeeSQL" >
    <AlternatingRowStyle BackColor="White" />
    <Columns>
                    <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="lnkEditUpdate" runat="server" Text="Edit/Update" OnClick="lnkEditUpdate_Click" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="ID" HeaderText="NetID" ReadOnly="True" SortExpression="ID" />
        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
        <asp:BoundField DataField="Salary" HeaderText="Salary" SortExpression="Salary" />
        <asp:TemplateField>

Use command argument of grid view property in button

CommandArgument='<%# Eval("Column Name") %>' //this is for aspx   

then in code behind call it

Response.write(e.commandargyment)//this is for aspx.cs   

for more details watch this tutorial Click here

after that use query string hope this help you.

For editing you could use Edit Functionality of Gridview. If your requirement is to redirect to page with all detail (as coded by you) then don't loop through gridview but fetch only the particular row from which linkbutton is clicked:

 protected void lnkEditUpdate_Click(object sender, EventArgs e)
        {
           LinkButton linkEdit =  (LinkButton)sender; // Point the particular row LinkButton clicked
           GridViewRow row = (GridViewRow)linkEdit.NameingContainer;// Fetch the Row
           Employee newe = new Employee();
           newe.ID = int.Parse((gvEmployee.Rows[row.RowIndex].Cells[1]).Text);
           newe.Name = (gvEmployee.Rows[row.RowIndex].Cells[2]).Text;
           newe.Salary = (gvEmployee.Rows[row.RowIndex].Cells[3]).Text;
           Session["Update"] = newe;
           Response.Redirect("~/EditUpdateEmployee.aspx");
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM