如何将值从gridview传递到下一页

问题描述:





i在gridview中有员工详细信息,



当我点击超链接时行,我使用员工ID作为超链接,



相应的行数据将传递到下一页,



请帮助任何人......



i have a staff details in gridview ,

when i click the hyperlink in the row ,which im using staff id as hyperlink,

that corresponding row data will pass to next page ,

pls help anybody to me.....

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

        onrowcommand="GridView1_RowCommand">
        <Columns>
            <asp:BoundField DataField="id" HeaderText="id" />
            <asp:BoundField DataField="name" HeaderText="name" />
            <asp:BoundField DataField="address" HeaderText="address" />
            <asp:TemplateField>
            <ItemTemplate>
            <asp:LinkButton ID="XXXXX" runat="server" Text="NEW PAGE" CommandName="AA" CommandArgument='<%#Eval("id") %>' />
            </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>





code

_________________



code
_________________

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            BindGridview();
    }
    protected void BindGridview()
    {
        using (SqlConnection con = new SqlConnection("------------------------"))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("Select * FROM TEST", con);
            SqlDataReader dr = cmd.ExecuteReader();
            GridView1.DataSource = dr;
            GridView1.DataBind();
            con.Close();
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "AA")
        {
            string id = e.CommandArgument.ToString();
            Server.Transfer("Default2.aspx?id=" + id);
        }

    }
}


试试这个:



你可以使用网格视图的模板字段。



try this :

you may use Template field of grid view .

<Columns>

       <asp:TemplateField HeaderText="Path" Visible="false">
       <ItemTemplate>
           <asp:Label ID="Label1" runat="server" Text=''<%#Eval("ID")%>'' ></asp:Label>
       </ItemTemplate>
       </asp:TemplateField>









这里的ID是每个数据的唯一键。



现在,



使用ROW数据命令事件网格。







here ID is the Unique key for each data.

now,

use ROW data Command event of Grid.

   protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        c.setcon();
        if(e.CommandName.Equals("link"))
        {
            int index = System.Convert.ToInt32(e.CommandArgument); // find the index of row that you clicked
                Label l1 = (Label)GridView1.Rows[index].FindControl("Label1"); // here "Label1" is the label name of your ID.

     //     now use query string to redirect next page

         response.redirect("xyz.aspx?data_id=''"+l1.toString()+"''");
      

         }
   }
// and get Data_id from next page







这对你有帮助......

如果没有,请发布错误或你的问题。



Mitesh




this will help you...
if not , please post it what error come or your problem.

Mitesh


你好读它:



在GridView中使用超链接列 [ ^ ]





谢谢
Hello Read It :

Using Hyperlink columns in GridView[^]


Thanks