Hi alemadi,
I have created sample please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<%@ 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 ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="txtCustomer" runat="server" Width="150px" />
<asp:HiddenField ID="hfCustomerId" runat="server" />
<cc1:DropDownExtender runat="server" DropDownControlID="pnlComboDropDownList" TargetControlID="txtCustomer" />
<asp:Panel runat="server" ID="pnlComboDropDownList" Style="display: none; visibility: hidden;">
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound"
OnSelectedIndexChanged="OnSelectedIndexChanged" ShowHeader="false">
<RowStyle BackColor="Azure" />
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="Id" />
<asp:BoundField DataField="ContactName" HeaderText="Name" />
</Columns>
</asp:GridView>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button Text="Save" runat="server" OnClick="OnSave" />
</div>
</form>
</body>
</html>
Note: Set EnableEventValidation to false in Page directive.
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT TOP 10 CustomerID,ContactName FROM Customers";
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
}
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.originalBackgroundColor=this.style.backgroundColor;this.style.backgroundColor='#bbbbbb';";
e.Row.Attributes["onmouseout"] = "this.style.backgroundColor=this.originalBackgroundColor;";
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.gvCustomers, "Select$" + e.Row.RowIndex);
}
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
if (gvCustomers.SelectedRow != null)
{
hfCustomerId.Value = Server.HtmlDecode(gvCustomers.SelectedRow.Cells[0].Text);
txtCustomer.Text = Server.HtmlDecode(gvCustomers.SelectedRow.Cells[1].Text);
}
else
{
txtCustomer.Text = "";
}
}
protected void OnSave(object sender, EventArgs e)
{
string id = hfCustomerId.Value;
string name = txtCustomer.Text;
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('Name : " + name + "\\nId : " + id + "')", true);
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT TOP 10 CustomerID,ContactName FROM Customers"
Dim cmd As SqlCommand = New SqlCommand(query)
Using con As SqlConnection = New SqlConnection(conString)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Using
End Using
End Using
End If
End Sub
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes("onmouseover") = "this.style.cursor='hand';this.originalBackgroundColor=this.style.backgroundColor;this.style.backgroundColor='#bbbbbb';"
e.Row.Attributes("onmouseout") = "this.style.backgroundColor=this.originalBackgroundColor;"
e.Row.Attributes("onclick") = ClientScript.GetPostBackClientHyperlink(Me.gvCustomers, "Select$" & e.Row.RowIndex)
End If
End Sub
Protected Sub OnSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
If gvCustomers.SelectedRow IsNot Nothing Then
hfCustomerId.Value = Server.HtmlDecode(gvCustomers.SelectedRow.Cells(0).Text)
txtCustomer.Text = Server.HtmlDecode(gvCustomers.SelectedRow.Cells(1).Text)
Else
txtCustomer.Text = ""
End If
End Sub
Protected Sub OnSave(ByVal sender As Object, ByVal e As EventArgs)
Dim id As String = hfCustomerId.Value
Dim name As String = txtCustomer.Text
ClientScript.RegisterClientScriptBlock(Me.GetType(), "", "alert('Name : " & name & "\nId : " & id & "')", True)
End Sub
Screenshot
