sudharsan s

sudharsan s

  • NA
  • 204
  • 87.2k

how can i pass grid values to textbox using javascript?

Apr 25 2012 1:57 AM
This is the code i am using bind the database values and display the values in html table.

public void load_builder()
{
    SqlConnection con = new SqlConnection(constring);
    con.Open();
    string sql = "select category,category_code,created_date from category";
    SqlCommand cmd = new SqlCommand(sql, con);
    SqlDataReader dr;
    dr = cmd.ExecuteReader();
    //sb.Append("<tbody>");
    while (dr.Read())
    {
      
        sb.Append("<tr >");
        sb.Append("<td><label onclick='loaddata(" + dr[0].ToString() + ")'>select</label></td>");
        sb.Append("<td id='c1" + dr[0].ToString() +"'>" + dr[0].ToString() + "</td>");
        sb.Append("<td id='c2" + dr[0].ToString() + "'>" + dr[1].ToString() + "</td>");
        sb.Append("</tr>");
    }

this is the html table grid structure.

<table cellpadding="0" align="left" cellspacing="0" border="0" id="table" class="tinytable">
            <thead>
                <tr>
                    <th><h3>select</h3></th>
                    <th><h3>Category Name</h3></th>
                    <th><h3>Category Code</h3></th>
                   
                                        
                </tr>
            </thead>
            <tbody>

after clicking this select option i want display the particular row values into particular text fields for update purpose..that means (cateory name and categorycode )data want to display in text fields.

for this purpose i am using the javascript function called loaddata

loaddata code:


<script type="text/javascript">
 
 
     function loaddata(val) {

         document.getElementById("<%=txtxtCategoryCode.ClientID%>").value = document.getElementById("c2" + val + "").innerHTML;
         document.getElementById("<%=txtcategory.ClientID%>").value = document.getElementById("c1" + val + "").innerHTML;
        
              }

</script>
but i am not able to get the particular row values to text box after clicking the select option..

Help me for this......

Advanec thx..

 




Answers (7)