【问题标题】:How to get Gridview Cell values into corresponding textbox values on another page?如何将 Gridview 单元格值转换为另一个页面上的相应文本框值?
【发布时间】:2016-06-27 15:30:56
【问题描述】:

这是一个简单的问题,但我有一个网格视图,其中包含 ID、姓名、工资的 Boundfields。我有一个项目模板字段,它将把用户带到另一个带有文本框的页面。我希望这样当用户单击要更新的超链接时,他们会被引导到一个带有文本框的页面,这些文本框预设为 gridview 中特定行的值,以便用户可以编辑和更新。

        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>

【问题讨论】:

  • 如果您使用链接按钮,您可以通过查询字符串或会话变量将网格视图中的值传递到下一个表单。
  • 如何获得 gridview 值?上面的代码不起作用

标签: c# asp.net session gridview


【解决方案1】:

对于编辑,您可以使用 Gridview 的编辑功能。如果您的要求是重定向到包含所有详细信息的页面(由您编写),则不要循环遍历 gridview,而仅获取单击链接按钮的特定行:

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

【讨论】:

  • 是链接。编辑 NamingConainer?因为 NameingContainer 不起作用。我也收到 int.Parse(((BoundField)row.FindControl("txtID")).Text); 的错误。它说不能将类型 System.Web.UI.Control 转换为 System.Web.UI.WebControls.BoundField
  • 我可以看看你的 Gridview 编码(aspx)你是如何创建所有字段的吗?
【解决方案2】:

在按钮中使用网格视图属性的命令参数

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

然后在后面的代码中调用它

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

更多详情请观看本教程Click here

之后使用查询字符串希望这对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多