Hi . I want to know how pass the data from the datagrid to textbox through query string with only the ID of the selected row being passed as query i.e when I select a particular row all the attributes of that row should be displayed on the textboxes of another page :
Below is d code for the first page that has d datagrid :
<asp:DataGrid ID="DataGrid1" AutoGenerateColumns="False" runat="server" DataKeyField="Empid"
OnDeleteCommand="DataGrid1_DeleteCommand" OnCancelCommand ="DataGrid1_CancelCommand" OnUpdateCommand ="DataGrid1_UpdateCommand" OnEditCommand ="DataGrid1_EditCommand" BackColor="#DEBA84" BorderColor="#DEBA84"
BorderWidth="1px" CellPadding="3" BorderStyle="None" CellSpacing="2" Height="296px"
Width="488px">
<Columns>
<asp:BoundColumn DataField="Empid" HeaderText="Empid" Visible="false"></asp:BoundColumn>
<asp:BoundColumn DataField="EmpName" HeaderText="EmpName"></asp:BoundColumn>
<asp:BoundColumn DataField="Address" HeaderText="Address"></asp:BoundColumn>
<asp:BoundColumn DataField="Salary" HeaderText="Salary"></asp:BoundColumn>
<asp:BoundColumn DataField="DeptName" HeaderText="DeptName"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Edit" >
<ItemTemplate>
<asp:Image runat="server" Width="50" Height="40" ImageUrl="~/Images/images.jpg" />
<asp:HyperLink NavigateUrl='<%# DataBinder.Eval(Container.DataItem,"Empid","~/Default.aspx?Empid={0}" ) %>' Text="Edit" id = "HyperLink2" runat="server"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn CommandName="Edit" Text="Edit" ></asp:ButtonColumn>
<asp:TemplateColumn HeaderText="Delete">
<ItemTemplate>
<asp:Image runat="server" Width="50" Height="40" ImageUrl="~/Images/cross.jpg" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn CommandName="Delete" Text="Delete"></asp:ButtonColumn>
<asp:ButtonColumn CommandName="AddNew" Text="Insert"></asp:ButtonColumn>
</Columns>
</asp:DataGrid>
And the code for the second page wherein the textboxes r there is as follows :
<table border="1">
<tr>
<td>
Name:
</td>
<td class="style1">
<asp:TextBox ID="txtname_e" runat="server" Width="167px" Text='<%# DataBinder.Eval(Container, "DataItem.EmpName") %>' ></asp:TextBox>
</td>
</tr>
<tr>
<td>
Address:
</td>
<td class="style1">
<asp:TextBox ID="txtadd_e" runat="server" TextMode="MultiLine" Width="176px" Text='<%# DataBinder.Eval(Container, "DataItem.Address") %>'></asp:TextBox>
<br />
</td>
</tr>
<tr>
<td class="style2" >
Salary :
</td>
<td class="style3">
<asp:TextBox ID="textbox3" runat="server" Width="176px" Text='<%# DataBinder.Eval(Container, "DataItem.Salary") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td colspan= "1" >
Department name :
</td>
<td>
<asp:DropDownList runat="server" ID="DropDownList1" Height="31px"
Width="179px" style="margin-bottom: 5px" >
<asp:ListItem> </asp:ListItem>
</asp:DropDownList>
<br />
<br />
<br />
</td>
</tr>
<tr>
<td colspan= 2 align= center>
<asp:Button ID="btnupdate" runat="server" CommandName="Update" Text="Update" onclick="btnupdate_Click" />
<asp:Button ID="Button2" runat="server" CommandName="Cancel" Text="Cancel" onclick="Button2_Click" />
<asp:Button ID="btnadd" runat="server" OnClick="btnadd_Click" Text="Add New Record" />
</td>
</tr>
</table>
and the code behind file :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strid = Request.QueryString["Empid"];
if (!Page.IsPostBack)
{
FillDeptDropDownList();
}
}
protected void btnadd_Click(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
}
protected void FillDeptDropDownList()
{
String strConnString = ConfigurationManager.ConnectionStrings["Company"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand("Select * from Department", con);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adp.Fill(dt);
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "DeptName";
DropDownList1.DataValueField = "DeptID";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, "Select department name:");
}
protected void btnupdate_Click(object sender, EventArgs e)
{
if (btnadd.Text == "Update Record")
{
btnadd.Text = "Cancel";
}
else
{
btnadd.Text = "Add New Record";
}
}
}