Hi team
I have a griddata table in and a textbox , if i select a row from grid table then that row value or index shld be in textbox..
how can i pass gridview values to textbox using javascript?
I have a griddata table in and a textbox , if i select a row from grid table then that row value or index shld be in textbox..
how can i pass gridview values to textbox using javascript?
|
|
|
|
|
|
SenthilkumarPosted Apr 25, 2012, 12:55 AM
Its bit simple!
Write the onclick event on the row in the RowDataBound and pass the actual value into the function. when click on the row it will call the function with the value. Need to assign it the control.
In javascript, you need to write the following.
function AssignValue(CtrlVal)
{
if(document.getElementById('TextBox1'))
{
document.getElementById('TextBox1').value = CtrlVal;
}
return false;
}
Hope this will help you.
Satyapriya NayakPosted Apr 24, 2012, 2:58 PM
Try this...
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Select_a_row_grid_value_textbox._Default" %>
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;
using System.Data.SqlClient;
namespace Select_a_row_grid_value_textbox
{
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
DataSet ds = new DataSet();
SqlCommand com;
SqlConnection con;
SqlDataAdapter sqlda;
string str;
string str1;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
binddata();
}
}
void binddata()
{
con = new SqlConnection(connStr);
con.Open();
str = "select * from employee";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "employee");
GridView1.DataSource = ds;
GridView1.DataMember = "employee";
GridView1.DataBind();
con.Close();
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridView1.SelectedIndex = Convert.ToInt32(e.NewSelectedIndex);
str1 = GridView1.SelectedDataKey.Value.ToString();
TextBox1.Text = str1;
binddata();
}
}
}
Thanks