Ashwini

Ashwini

  • NA
  • 147
  • 140k

Autocomplete extender in grid

Oct 30 2011 1:12 AM
Hi,
  i am developing an application in that iam using a gridview. in all the columns i am keeping textbox.
iam using ajax autocomplete extender for first row textboxes. after selecting one value from firstrow and first column textbox corresponding related values fetch from database and display in first row of 2nd,3ed and 4th column of gridview. it i select one value in 2nd row of first column corresponding values fetch from database and display in 2nd row of 2nd,3ed,4th column of the gridview this process will continue.

I have tried to do this one but what is happening is if i select value from 2nd row of first column textbox corresponding data will fetch from database and displaying in 2nd row of 2nd,3ed,4th column of gridvew. if i select one value from first row of 1st column textbox the same values of nd row is repeating in 1st row also i am not getting what mistake i have did.please can anyone help me?


my .aspx code is

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="dbauto.aspx.cs" Inherits="Default4" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" tagprefix="cc1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title></title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
  <asp:ScriptManager runat="server" ID="sc">
  <Services>
  <asp:ServiceReference Path="~/WebService.asmx" />
  </Services>
  </asp:ScriptManager>
  <%--  <cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" EnablePageMethods="true">
  </cc1:ToolkitScriptManager>--%>
  <asp:GridView runat="server" ID="grd">
  <Columns>
  <asp:TemplateField HeaderText="ProductID">
  <ItemTemplate>
  <asp:UpdatePanel runat="server" ID="Up1" UpdateMode="Conditional">
  <ContentTemplate>
  <asp:TextBox runat="server" ID="txt" AutoPostBack="true" ontextchanged="txt_TextChanged"></asp:TextBox>
  <div>
  <cc1:AutoCompleteExtender runat="server" ID="acecontrol" TargetControlID="txt" MinimumPrefixLength="1"
  ServiceMethod="GetCompletionList" ServicePath="WebService.asmx" EnableCaching="true">
  </cc1:AutoCompleteExtender>
  </div>
  </ContentTemplate>
  </asp:UpdatePanel>
  </ItemTemplate>
  </asp:TemplateField>
  <asp:TemplateField HeaderText="FY12List">
<ItemTemplate>
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TS">
<ItemTemplate>
<asp:TextBox ID="txt2" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DS">
<ItemTemplate>
<asp:TextBox ID="txt3" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

  </Columns>
  </asp:GridView>
  </div>
  </form>
</body>
</html>

my .aspx.cs code is

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


public partial class Default4 : System.Web.UI.Page
{
 
  SqlDataAdapter sqlda = new SqlDataAdapter();
  SqlCommand com = new SqlCommand();
  DataTable dt;
  string str;

  protected void Page_Load(object sender, EventArgs e)
  {
  if (!IsPostBack)
  {
  List<string> l = new List<string>();
  l.Add("Row 1");
  l.Add("Row 2");
  grd.DataSource = l;
  grd.DataBind();
  }
  }

  protected void txt_TextChanged(object sender, System.EventArgs e)
  {
  TextBox ddl = sender as TextBox;


  foreach (GridViewRow row in grd.Rows)
  {
 
  Control ctrl = row.FindControl("txt") as TextBox;
  if (ctrl != null)
  {
  TextBox ddl1 = (TextBox)ctrl;

  if (ddl1.ClientID == ddl.ClientID)
  {

  TextBox txt1 = row.FindControl("txt1") as TextBox;
  TextBox txt2 = row.FindControl("txt2") as TextBox;
  TextBox txt3 = row.FindControl("txt3") as TextBox;

  SqlConnection conn = new SqlConnection("data source=hobvision07; initial catalog=MBOMexcel; user id=sa; password=hobvision");
  conn.Open();
  str = "select * from sampledata where productid='" +txt.Text+ "'";
  com = new SqlCommand(str, conn);
  SqlDataReader reader = com.ExecuteReader();
  while (reader.Read())
  {
  txt1.Text = reader["FY12List"].ToString();
  txt2.Text = reader["TS"].ToString();
  txt3.Text = reader["DS"].ToString();

  }
  reader.Close();
  conn.Close();
  }
  }
  }
  }
}

and my webservice.cs code for binding to textbox is


using System;

using System.Web;

using System.Collections;

using System.Web.Services;

using System.Web.Services.Protocols;

using System.Data;

using System.Data.SqlClient;

using System.Configuration;
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

  public WebService()
  {

  //Uncomment the following line if using designed components
  //InitializeComponent();
  }

  [WebMethod]
  public string HelloWorld()
  {
  return "Hello World";
  }
  [WebMethod]
  [System.Web.Script.Services.ScriptMethod]
  public string[] GetCompletionList(string prefixText)
  {

  SqlConnection con = new SqlConnection("data source=hobvision07; initial catalog=MBOMexcel; user id=sa; password=hobvision");
  SqlDataAdapter dad = new SqlDataAdapter("select * from sampledata where productid like '" + prefixText + "%'", con);
  DataSet ds = new DataSet();
  dad.Fill(ds);
  string[] list = new string[ds.Tables[0].Rows.Count];
  int i = 0;
  foreach (DataRow dr in ds.Tables[0].Rows)
  {
  list.SetValue(dr[0].ToString(), i);
  i++;
  }
  return list;


  }
}


please refer this if any mistakes let me know.

Answers (1)