Generally we use AJAX AutoCompleteExtender for a TextBox to show data from database through Webservice. Here I faced a problem that to set contextKey value dyanamically from '.CS' file without fix it in AutoCompleteExtender's property that is ContextKey="value" in design view.
Nb : before using this we should have appropriate AjaxControlToolkit.dll for our .Net version.
Design Code:-
<body>
<form id="form2" runat="server">
<asp:ToolkitScriptManager
runat="server">
<Services>
<asp:ServiceReference Path="~/WebService/WebService.asmx"
/>
</Services>
</asp:ToolkitScriptManager>
<div>
<fieldset style="height: 200px; width: 365px;">
<table class="style1">
<tr>
<td
style="text-align: right">
<asp:Label ID="Label18" runat="server" Text="Designation"></asp:Label>
</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" Height="22px" Width="142px" AutoPostBack="True"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>Select</asp:ListItem>
<asp:ListItem>Software Engineer</asp:ListItem>
<asp:ListItem>Designer</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td
style="text-align: right">
<asp:Label ID="Label19" runat="server" Text="Employee"></asp:Label>
</td>
<td
style="text-align: left">
<asp:TextBox ID="TxtEmployee" runat="server" autocomplete="off"></asp:TextBox>
<asp:AutoCompleteExtender ID="TxtEmployee_AutoCompleteExtender" runat="server"
DelimiterCharacters=""
Enabled="True"
ServicePath="~/WebService/WebService.asmx"
ServiceMethod="GetEmployee"
CompletionInterval="100"
MinimumPrefixLength="0"
TargetControlID="TxtEmployee"
UseContextKey="true">
</asp:AutoCompleteExtender>
</td>
</tr>
</table>
</fieldset>
</div>
</form>
</body>
C# Code:-
private void
SetContextKey()
{
AjaxControlToolkit.AutoCompleteExtender
modal = (AjaxControlToolkit.AutoCompleteExtender)TxtEmployee.FindControl("TxtEmployee_AutoCompleteExtender");
modal.ContextKey = DropDownList1.SelectedItem.ToString();// Any constant value
}
protected void
DropDownList1_SelectedIndexChanged(object
sender, EventArgs e)
{
SetContextKey();
}
WebService Code :-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
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] // This line should
Uncomment
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]
public string[]
GetEmployee(string prefixText,int count,string
contextKey)
{
string sql = "Select
* from emp_demo Where EmpName like '"+ prefixText +"%' and Designation='"+ contextKey +"' ";
SqlDataAdapter da = new
SqlDataAdapter(sql, ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);
DataTable dt = new
DataTable();
da.Fill(dt);
string[] country= new
string[dt.Rows.Count];
int i = 0;
foreach (DataRow
dr in dt.Rows)
{
country.SetValue(dr["EmpName"].ToString(),
i);
i++;
}
return country;
}
}
thanks a lot dude u help me
ReplyDeletei to have a blog friend
Hi friend me to have some doubt the same concept apply in gridview Dropdown instead of use text box how to used same concept. Please can you explain In mail ID
ReplyDeletearunjun6@gmail.com
its very helpful...Thank U.....:)
ReplyDeletewelcome
DeleteNice post...I look forward to reading more, and getting a more active part in the talks here, whilst picking up some knowledge as well..
ReplyDeletePass Box manufacturers
Thank you sir
ReplyDeleteThank you this worked!! But, I used webservice method in the same aspx.cs file.
ReplyDelete