Hi
I have gridview and AutocompleteExtender inside.
In footer if we select country name,State name list will be displayed.
If i select country,I get state based on country.
but i am not getting statecode in "txtftrStateCode" using " txtftrStateName_TextChanged(object sender, EventArgs e)"
I have to get statecode because it is passed as parameter to filter District
Please help me.I have given the code below.
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
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.Collections.Generic;
using AjaxControlToolkit;
public partial class _Default : System.Web.UI.Page
{
private SqlConnection con = new SqlConnection("Data
Source=DELL-PC\\SQLEXPRESS;Initial Catalog=MySampleDB;Integrated
Security=True");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindEmployeeDetails();
}
}
protected void BindEmployeeDetails()
{
con.Open();
string SqlQry = "Select *,CM.CountryName,SM.StateName "+ " from Employee_Details ED,CountryMaster CM,StateMaster SM where
ED.Statecode=SM.statecode and ED.CountryCode=CM.CountryCode ";
SqlCommand cmd = new SqlCommand(SqlQry, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
gvDetails.DataSource = ds;
gvDetails.DataBind();
}
else
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
gvDetails.DataSource = ds;
gvDetails.DataBind();
int columncount = gvDetails.Rows[0].Cells.Count;
gvDetails.Rows[0].Cells.Clear();
gvDetails.Rows[0].Cells.Add(new TableCell());
gvDetails.Rows[0].Cells[0].ColumnSpan = columncount;
gvDetails.Rows[0].Cells[0].Text = "No Records Found";
}
}
protected void gvDetails_RowEditing(object sender, GridViewEditEventArgs e)
{
gvDetails.EditIndex = e.NewEditIndex;
BindEmployeeDetails();
}
protected void gvDetails_RowUpdating(object sender,
GridViewUpdateEventArgs e)
{
int userid =
Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Value.ToString());
string username =
gvDetails.DataKeys[e.RowIndex].Values["UserName"].ToString();
TextBox txtStateName =
(TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtStateName");
TextBox txtCountryName =
(TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtstate");
con.Open();
SqlCommand cmd = new SqlCommand("update Employee_Details set
State='" + txtStateName.Text + "',CountryName='" + txtCountryName.Text+ "' where UserId=" + userid, con);
cmd.ExecuteNonQuery();
con.Close();
lblresult.ForeColor = Color.Green;
lblresult.Text = username + " Details Updated successfully";
gvDetails.EditIndex = -1;
BindEmployeeDetails();
}
protected void gvDetails_RowCancelingEdit(object sender,
GridViewCancelEditEventArgs e)
{
gvDetails.EditIndex = -1;
BindEmployeeDetails();
}
protected void gvDetails_RowDeleting(object sender,
GridViewDeleteEventArgs e)
{
int userid =
Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["UserId"].ToString());
string username =
gvDetails.DataKeys[e.RowIndex].Values["UserName"].ToString();
con.Open();
SqlCommand cmd = new SqlCommand("delete from Employee_Details
where UserId=" + userid, con);
int result = cmd.ExecuteNonQuery();
con.Close();
if (result == 1)
{
BindEmployeeDetails();
lblresult.ForeColor = Color.Red;
lblresult.Text = username + " details deleted successfully";
}
}
protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
protected void gvDetails_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AddNew"))
{
TextBox txtUsrname =
(TextBox)gvDetails.FooterRow.FindControl("txtftrUserName");
TextBox txtftrStateName =
(TextBox)gvDetails.FooterRow.FindControl("txtftrStateName");
TextBox txtftrCountryName =
(TextBox)gvDetails.FooterRow.FindControl("txtftrCountryName");
con.Open();
SqlCommand cmd =
new SqlCommand("insert into
Employee_Details(UserName,StateCode,CountryCode) values('" +
txtUsrname.Text + "'," + txtftrStateName.Text + "," +
txtftrCountryName.Text + ")", con);
int result = cmd.ExecuteNonQuery();
con.Close();
if (result == 1)
{
BindEmployeeDetails();
lblresult.ForeColor = Color.Green;
lblresult.Text = txtUsrname.Text + " Details inserted
successfully";
}
else
{
lblresult.ForeColor = Color.Red;
lblresult.Text = txtUsrname.Text + " Details not inserted";
}
}
}
protected void txtftrStateName_TextChanged(object sender, EventArgs e)
{
using (SqlCommand cmd = new SqlCommand())
{
SqlConnection conn = new SqlConnection(GetConnectionString());
conn.Open();
TextBox thisTextBox = (TextBox)sender;
GridViewRow currentRow = (GridViewRow)thisTextBox.Parent.Parent;
int rowindex = 0;
rowindex = currentRow.RowIndex;
TextBox txtftrStateName = (TextBox)currentRow.FindControl("txtftrStateName");
string cmdText = "select StateName,StateCode from StateMaster where StateName = '" + txtftrStateName.Text + "'";
cmd.CommandText = cmdText;
cmd.Connection = conn;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
TextBox TextBox1 = sender as TextBox;
GridViewRow gvr = TextBox1.NamingContainer as GridViewRow;
TextBox txtftrStateCode = gvr.FindControl("txtftrStateCode") as TextBox;
if ((dt.Rows.Count > 0) && (dt != null))
{
txtftrStateCode.Text =dt.Rows[0]["StateCode"].ToString();
TextBox txtftrDistrictName = (TextBox)currentRow.FindControl("txtftrDistrictName") as TextBox;
AutoCompleteExtender autoCompleteExtender3 = (AutoCompleteExtender)currentRow.FindControl("AutoCompleteExtender3") as AutoCompleteExtender;
AutoCompleteExtender3.TargetControlID = txtftrDistrictName.ID;
autoCompleteExtender3.ServiceMethod = "SearchStateBranch";
autoCompleteExtender3.UseContextKey = true;
}
}
}
Thanks and regards
Ramachandran
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> SearchCustomers(string prefixText, int count)
{
using (SqlCommand cmd = new SqlCommand())
{
SqlConnection conn=new SqlConnection("Data
Source=DELL-PC\\SQLEXPRESS;Initial Catalog=MySampleDB;Integrated
Security=True");
conn.Open();
string cmdText = "select CountryName,CountryCode from
CountryMaster where " +"CountryName like @SearchText + '%'";
cmd.Parameters.AddWithValue("@SearchText", prefixText);
cmd.CommandText = cmdText;
cmd.Connection = conn;
List<string> Countries = new List<string>();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
Countries.Add(sdr["CountryName"].ToString());
}
}
conn.Close();
return Countries;
}
}
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> SearchState(string prefixText, int
count, string contextKey)
{
using (SqlCommand cmd = new SqlCommand())
{
SqlConnection conn = new SqlConnection("Data
Source=DELL-PC\\SQLEXPRESS;Initial Catalog=MySampleDB;Integrated
Security=True");
conn.Open();
string cmdText = "select StateName,StateCode from
StateMaster where " +"StateName like @SearchText + '%'";
cmd.Parameters.AddWithValue("@SearchText", prefixText);
if (contextKey != "0")
{
cmdText += " and CountryCode = @Country";
cmd.Parameters.AddWithValue("@Country", contextKey);
}
cmd.CommandText = cmdText;
cmd.Connection = conn;
List<string> States = new List<string>();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
States.Add(sdr["StateName"].ToString());
}
}
conn.Close();
return States;
}
}
protected void txtftrCountryName_TextChanged(object sender, EventArgs e)
{
using (SqlCommand cmd = new SqlCommand())
{
SqlConnection conn = new SqlConnection("Data
Source=DELL-PC\\SQLEXPRESS;Initial Catalog=MySampleDB;Integrated
Security=True");
conn.Open();
TextBox thisTextBox = (TextBox)sender;
GridViewRow currentRow = (GridViewRow)thisTextBox.Parent.Parent;
int rowindex = 0;
rowindex = currentRow.RowIndex;
TextBox txt = (TextBox)currentRow.FindControl("txtftrCountryName");
string cmdText = "select CountryName,CountryCode from
CountryMaster where " +"CountryName = '" + txt.Text + "'";
cmd.CommandText = cmdText;
cmd.Connection = conn;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
TextBox txt1 =
(TextBox)currentRow.FindControl("txtftrHCountryCode");
txt1.Text = dt.Rows[0]["CountryCode"].ToString();
TextBox txtftrHCountryCode =
(TextBox)currentRow.FindControl("txtftrHCountryCode");
TextBox txtftrStateName =
(TextBox)currentRow.FindControl("txtftrStateName") as TextBox;
AutoCompleteExtender autoCompleteExtender2
=(AutoCompleteExtender)currentRow.FindControl("AutoCompleteExtender2")
as AutoCompleteExtender;
autoCompleteExtender2.ContextKey = txtftrHCountryCode.Text;
autoCompleteExtender2.TargetControlID = txtftrStateName.ID;
autoCompleteExtender2.ServiceMethod = "SearchState";
autoCompleteExtender2.UseContextKey = true;
}
}
protected void gvDetails_RowDataBound1(object sender,
GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.Footer)
{
TextBox txtftrHCountryCode =
e.Row.FindControl("txtftrHCountryCode") as TextBox;
TextBox txtftrStateName =
e.Row.FindControl("txtftrStateName") as TextBox;
AutoCompleteExtender autoCompleteExtender2 =
e.Row.FindControl("AutoCompleteExtender2") as AutoCompleteExtender;
autoCompleteExtender2.ContextKey = txtftrHCountryCode.Text;
autoCompleteExtender2.TargetControlID = txtftrStateName.ID;
autoCompleteExtender2.ServiceMethod = "SearchState";
autoCompleteExtender2.UseContextKey = true;
//getting username from particular row
string username =
Convert.ToString(DataBinder.Eval(e.Row.DataItem, "UserName"));
//identifying the control in gridview
ImageButton lnkbtnresult =
(ImageButton)e.Row.FindControl("imgbtnDelete");
//raising javascript confirmationbox whenver user clicks
on link button
if (lnkbtnresult != null)
{
lnkbtnresult.Attributes.Add("onclick","javascript:return ConfirmationBox('" + username + "')");
}
}
}
}
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %><%@ 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 id="Head1" runat="server"><title>Untitled Page</title><style type="text/css">
.Gridview {
font-family: Verdana;
font-size: 10pt;
font-weight: normal;
color: black;
}</style><script type="text/javascript">
function ConfirmationBox(username) {
var result = confirm('Are you sure you want to delete ' +
username + ' Details?');
if (result) {
return true;
}
else {
return false;
}
}
</script></head><body><form id="form1" runat="server"><asp:ScriptManager ID="ScriptManager1"
runat="server"></asp:ScriptManager><div><asp:GridView ID="gvDetails"
DataKeyNames="UserId,UserName" runat="server"
AutoGenerateColumns="false" CssClass="Gridview"
HeaderStyle-BackColor="#61A6F8"
ShowFooter="true" HeaderStyle-Font-Bold="true"
HeaderStyle-ForeColor="White"
OnRowCancelingEdit="gvDetails_RowCancelingEdit"
OnRowDeleting="gvDetails_RowDeleting"
OnRowEditing="gvDetails_RowEditing"
OnRowUpdating="gvDetails_RowUpdating"
OnRowCommand="gvDetails_RowCommand"
OnRowDataBound="gvDetails_RowDataBound1"><Columns><asp:TemplateField><EditItemTemplate><asp:ImageButton ID="imgbtnUpdate"
CommandName="Update" runat="server" ImageUrl="~/Images/update.jpg"
ToolTip="Update" Height="20px" Width="20px" /><asp:ImageButton ID="imgbtnCancel"
runat="server" CommandName="Cancel" ImageUrl="~/Images/Cancel.jpg"
ToolTip="Cancel" Height="20px" Width="20px" /></EditItemTemplate><ItemTemplate><asp:ImageButton ID="imgbtnEdit"
CommandName="Edit" runat="server" ImageUrl="~/Images/Edit.jpg"
ToolTip="Edit" Height="20px" Width="20px" /><asp:ImageButton ID="imgbtnDelete"
CommandName="Delete" Text="Edit" runat="server"
ImageUrl="~/Images/delete.jpg" ToolTip="Delete" Height="20px"
Width="20px" /></ItemTemplate><FooterTemplate><asp:ImageButton ID="imgbtnAdd"
runat="server" ImageUrl="~/Images/AddNewitem.jpg" CommandName="AddNew"
Width="30px" Height="30px" ToolTip="Add new User"
ValidationGroup="validaiton" /></FooterTemplate></asp:TemplateField><asp:TemplateField HeaderText="State"><EditItemTemplate><asp:TextBox ID="txtStateName"
runat="server" Text='<%#Eval("StateCode") %>' /></EditItemTemplate><ItemTemplate><asp:Label ID="lblStateName"
runat="server" Text='<%#Eval("StateName") %>' /></ItemTemplate><FooterTemplate><asp:TextBox ID="txtftrStateName" runat="server" /><asp:RequiredFieldValidator ID="rfvState"
runat="server"
ControlToValidate="txtftrStateName"
Text="*" ValidationGroup="validaiton" /><cc1:AutoCompleteExtender
ID="AutoCompleteExtender2" TargetControlID="txtftrStateName"
UseContextKey="true"
MinimumPrefixLength="1" EnableCaching="true"
CompletionInterval="500"
ServiceMethod="SearchState" runat="server"></cc1:AutoCompleteExtender><asp:TextBox ID="txtftrStateCode" EnableViewState="true" AutoPostBack="true" runat="server" /></FooterTemplate></asp:TemplateField><asp:TemplateField HeaderText="Country"><EditItemTemplate><asp:TextBox ID="txtCountryName"
runat="server" Text='<%#Eval("CountryCode") %>' /></EditItemTemplate><ItemTemplate><asp:Label ID="lblCountryName"
runat="server" Text='<%#Eval("CountryName") %>' /></ItemTemplate><FooterTemplate><asp:TextBox ID="txtftrCountryName"
runat="server" AutoPostBack="True"
OnTextChanged="txtftrCountryName_TextChanged" /><asp:RequiredFieldValidator
ID="rfvtxtftrCountryName" runat="server"
ControlToValidate="txtftrCountryName" Text="*"
ValidationGroup="validaiton" /><cc1:AutoCompleteExtender ID="AutoCompleteExtender1"
runat="server"
TargetControlID="txtftrCountryName"
MinimumPrefixLength="1"
EnableCaching="true" CompletionSetCount="1"
CompletionInterval="500"
ServiceMethod="SearchCustomers"></cc1:AutoCompleteExtender><asp:TextBox ID="txtftrHCountryCode"
AutoPostBack="true" runat="server"></asp:TextBox></FooterTemplate></asp:TemplateField></Columns><HeaderStyle BackColor="#61A6F8" Font-Bold="True"
ForeColor="White"></HeaderStyle></asp:GridView><asp:SqlDataSource ID="SqlDataSource1"
runat="server"></asp:SqlDataSource></div><div><asp:Label ID="lblresult" runat="server"></asp:Label></div>