I am new in asp.net and so want to ask as to how do i pass the datas from the gridview to textbox through query string with only the ID of the selected row being passed as query...Means when i select a particular row all the attributes of that row should be displayed on the textboxes of another page...


Below is the code...

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
            <Columns>
                <asp:TemplateField HeaderText="First Name">
                    <ItemTemplate>
                        <asp:Label ID="LinkButton1" runat="server" Text='<% #Eval("FirstName") %>' PostBackUrl="~/Default3.aspx"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Last Name">
                    <ItemTemplate>
                        <asp:Label ID="LinkButton1" runat="server" Text='<% #Eval("LastName") %>' PostBackUrl="~/Default3.aspx"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Emp ID">
                    <ItemTemplate>
                        <asp:Label ID="LinkButton1" runat="server" Text='<% #Eval("EmpID") %>' PostBackUrl="~/Default3.aspx"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Phone no">
                    <ItemTemplate>
                        <asp:Label ID="LinkButton1" runat="server" Text='<% #Eval("PhoneNumber") %>' PostBackUrl="~/Default3.aspx"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Salary">
                    <ItemTemplate>
                        <asp:Label ID="LinkButton1" runat="server" Text='<% #Eval("Salary") %>' PostBackUrl="~/Default3.aspx"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Country">
                    <ItemTemplate>
                        <asp:Label ID="LinkButton1" runat="server" Text='<% #Eval("CountryName") %>' PostBackUrl="~/Default3.aspx"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="State">
                    <ItemTemplate>
                        <asp:Label ID="LinkButton1" runat="server" Text='<% #Eval("StateName") %>' PostBackUrl="~/Default3.aspx"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="City">
                    <ItemTemplate>
                        <asp:Label ID="LinkButton1" runat="server" Text='<% #Eval("CityName") %>' PostBackUrl="~/Default3.aspx"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                    
                <asp:TemplateField HeaderText="">
                    <asp:LinkButton ID="LinkButton1" runat="server" Text="view"  PostBackUrl='<%#"~/Default3.aspx?ID="+Eval("ID")%>'></asp:LinkButton> 
                   
                </asp:TemplateField>
               
            
     </Columns>
</asp:GridView>

Recommended Answers

All 7 Replies

The easiest way is, as you are using PostBackUrl property you don't need to pass vlaue in querystring instead you can access your entire GridView control of current page on Default3.aspx page. Below is the code to access entire GridView.

GridView grid = (GridView)PreviousPage.FindControl("GridView1");
txt1.Text = grid.SelectedRow.Cells[2].Text;
txt2.Text = grid.SelectedRow.Cells[3].Text;

Here txt1, txt2 etc is the textbox of your Default3.aspx page and Cells[2], Cells[3] etc contains your Gridview's particular cell value.

Another way is, By Clicking on View link button, you are already passing Employee ID in querystring of Default3.aspx page. So on the Default3.aspx page you just need to fetch Employee ID from querystring then Write a SQL SELECT Query to pull required data from table by passing querystring employee id in Where clause.

Try it and let us know :-)

I am new in asp.net and so want to ask as to how do i pass the datas from the gridview to textbox through query string with only the ID of the selected row being passed as query...Means when i select a particular row all the attributes of that row should be displayed on the textboxes of another page...


Below is the code...

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
            <Columns>
                <asp:TemplateField HeaderText="First Name">
                    <ItemTemplate>
                        <asp:Label ID="LinkButton1" runat="server" Text='<% #Eval("FirstName") %>' PostBackUrl="~/Default3.aspx"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Last Name">
                    <ItemTemplate>
                        <asp:Label ID="LinkButton1" runat="server" Text='<% #Eval("LastName") %>' PostBackUrl="~/Default3.aspx"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Emp ID">
                    <ItemTemplate>
                        <asp:Label ID="LinkButton1" runat="server" Text='<% #Eval("EmpID") %>' PostBackUrl="~/Default3.aspx"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Phone no">
                    <ItemTemplate>
                        <asp:Label ID="LinkButton1" runat="server" Text='<% #Eval("PhoneNumber") %>' PostBackUrl="~/Default3.aspx"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Salary">
                    <ItemTemplate>
                        <asp:Label ID="LinkButton1" runat="server" Text='<% #Eval("Salary") %>' PostBackUrl="~/Default3.aspx"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Country">
                    <ItemTemplate>
                        <asp:Label ID="LinkButton1" runat="server" Text='<% #Eval("CountryName") %>' PostBackUrl="~/Default3.aspx"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="State">
                    <ItemTemplate>
                        <asp:Label ID="LinkButton1" runat="server" Text='<% #Eval("StateName") %>' PostBackUrl="~/Default3.aspx"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="City">
                    <ItemTemplate>
                        <asp:Label ID="LinkButton1" runat="server" Text='<% #Eval("CityName") %>' PostBackUrl="~/Default3.aspx"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                    
                <asp:TemplateField HeaderText="">
                    <asp:LinkButton ID="LinkButton1" runat="server" Text="view"  PostBackUrl='<%#"~/Default3.aspx?ID="+Eval("ID")%>'></asp:LinkButton> 
                   
                </asp:TemplateField>
               
            
     </Columns>
</asp:GridView>

thanks ....
but 1 error its showing when i click on view
"Error executing child request for handler 'ASP.default2_aspx'."

and about the second option how do i display it in textboxes...

Thanks in advance

Not sure for error why it's causing ? Can you please message me the code you have written in Default3.aspx ?

Regarding second way, You just need to fetch employee id from query string. below is the code you need to write in page_load event of Default3.aspx :

int EmpId = Convert.ToInt32(Request.Form("your empid querystring").ToString());
string StrSQL = "Select col1, col2 upto coln From yourtable Where EmployeeID = " + EmpId;
SqlCommand cmd = new SqlCommand(strSQL,yourconnection);
SqlDataReader dr = cmd.ExecuteReader();
if(dr.Read())
{
   your specific textbox(i.e. firstname).Text = dr["column name"].ToString();
   your specific textbox(i.e. lastname).Text = dr["column name"].ToString();
}
dr.Close();

that's all :-)

thanks ....
but 1 error its showing when i click on view
"Error executing child request for handler 'ASP.default2_aspx'."

and about the second option how do i display it in textboxes...

Thanks in advance

Not sure for error why it's causing ? Can you please message me the code you have written in Default3.aspx ?

Regarding second way, You just need to fetch employee id from query string. below is the code you need to write in page_load event of Default3.aspx :

int EmpId = Convert.ToInt32(Request.Form("your empid querystring").ToString());
string StrSQL = "Select col1, col2 upto coln From yourtable Where EmployeeID = " + EmpId;
SqlCommand cmd = new SqlCommand(strSQL,yourconnection);
SqlDataReader dr = cmd.ExecuteReader();
if(dr.Read())
{
   your specific textbox(i.e. firstname).Text = dr["column name"].ToString();
   your specific textbox(i.e. lastname).Text = dr["column name"].ToString();
}
dr.Close();

that's all :-)

Here is the code in default3.aspx

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Default3 : System.Web.UI.Page
{
public void Page_Load(object sender, EventArgs e)
{

GridView gridview = (GridView)PreviousPage.FindControl("GridView1");
TextBox1.Text = gridview.SelectedRow.Cells[0].Text;
TextBox2.Text = gridview.SelectedRow.Cells[1].Text;
TextBox3.Text = gridview.SelectedRow.Cells[2].Text;
TextBox4.Text = gridview.SelectedRow.Cells[3].Text;
TextBox5.Text = gridview.SelectedRow.Cells[4].Text;
TextBox6.Text = gridview.SelectedRow.Cells[5].Text;
TextBox7.Text = gridview.SelectedRow.Cells[6].Text;
TextBox8.Text = gridview.SelectedRow.Cells[7].Text;
}
}

Below is the code for the default3.aspx code using SQL

Its saying "Invalid Column name _page"....i checked it never write page newer....
And i have ommited the request.form line coz it is not able to fetch the ID

Plz correct and help me...
Thanks for ur effort Rohand

public void Page_Load(object sender, EventArgs e)
   {
       SqlConnection con = new SqlConnection("Data Source=SNSS1\\SQLEXPRESS;Initial Catalog=Employee;User ID=sa;Password=eLog!234");
     
       //int empid = Convert.ToInt32(Request.Form["ID"].ToString());
       //Response.Write(empid);
       string strSQL = " SELECT Tbl_Employee.ID,Tbl_Employee.FirstName,Tbl_Employee.LastName,Tbl_Employee.EmpID,Tbl_Employee.PhoneNumber,Tbl_Employee.Salary,Tab1.[Name] as CountryName,Tab2.[Name] as StateName,Tab3.[Name] as CityName FROM Tbl_Employee left JOIN Tbl_CountryStateCity Tab1 ON Tbl_Employee.CountryID=Tab1.ID left JOIN Tbl_CountryStateCity Tab2 ON Tbl_Employee.StateID=Tab2.ID left JOIN Tbl_CountryStateCity Tab3 ON Tbl_Employee.CityID=Tab3.ID WHERE Tbl_Employee.ID = " + ID;
       SqlCommand cmd = new SqlCommand(strSQL, con);
       con.Open();
       SqlDataReader dr = cmd.ExecuteReader();
       if (dr.Read())
       {
           TextBox1.Text = dr["FirstName"].ToString();
           TextBox2.Text = dr["LastName"].ToString();
           TextBox3.Text = dr["EmpID"].ToString();
           TextBox4.Text = dr["PhoneNumber"].ToString();
           TextBox5.Text = dr["Salary"].ToString();
           TextBox6.Text = dr["CountryName"].ToString();
           TextBox7.Text = dr["StateName"].ToString();
           TextBox8.Text = dr["CityName"].ToString();
       }
       dr.Close();
}

Sorry dude :-( it was my mistake.. See the below corrected line..


int empid = Convert.ToInt32(Request.QueryString["ID"].ToString());


Below is the code for the default3.aspx code using SQL

Its saying "Invalid Column name _page"....i checked it never write page newer....
And i have ommited the request.form line coz it is not able to fetch the ID

Plz correct and help me...
Thanks for ur effort Rohand

public void Page_Load(object sender, EventArgs e)
   {
       SqlConnection con = new SqlConnection("Data Source=SNSS1\\SQLEXPRESS;Initial Catalog=Employee;User ID=sa;Password=eLog!234");
     
       //int empid = Convert.ToInt32(Request.Form["ID"].ToString());
       //Response.Write(empid);
       string strSQL = " SELECT Tbl_Employee.ID,Tbl_Employee.FirstName,Tbl_Employee.LastName,Tbl_Employee.EmpID,Tbl_Employee.PhoneNumber,Tbl_Employee.Salary,Tab1.[Name] as CountryName,Tab2.[Name] as StateName,Tab3.[Name] as CityName FROM Tbl_Employee left JOIN Tbl_CountryStateCity Tab1 ON Tbl_Employee.CountryID=Tab1.ID left JOIN Tbl_CountryStateCity Tab2 ON Tbl_Employee.StateID=Tab2.ID left JOIN Tbl_CountryStateCity Tab3 ON Tbl_Employee.CityID=Tab3.ID WHERE Tbl_Employee.ID = " + ID;
       SqlCommand cmd = new SqlCommand(strSQL, con);
       con.Open();
       SqlDataReader dr = cmd.ExecuteReader();
       if (dr.Read())
       {
           TextBox1.Text = dr["FirstName"].ToString();
           TextBox2.Text = dr["LastName"].ToString();
           TextBox3.Text = dr["EmpID"].ToString();
           TextBox4.Text = dr["PhoneNumber"].ToString();
           TextBox5.Text = dr["Salary"].ToString();
           TextBox6.Text = dr["CountryName"].ToString();
           TextBox7.Text = dr["StateName"].ToString();
           TextBox8.Text = dr["CityName"].ToString();
       }
       dr.Close();
}

Hi

I had same problem.You can refer the following code to solve the issue of Passing data from grid view to textbox through query string.I think it will help you.

you can pass multiple parameters like:

postbackurl='<%# string.Format("~Default3.aspx?ID={0}&City={1}", Eval("ID"), Eval("CityName")) %>'

note that there would be a limitation in the querystring, in terms of length, finally.

on the other page you could receive your ID and other values as :

if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
{
    string ID = Request.QueryString["ID"];

   //now you can set TextBox1.Text = ID;

  //or better still fetch data based on ID value and set the TextBoxes
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.