I'm using Asp.Net with C#. I want to add Database names to DropDownList at Runtime. Here is the code.
Code Behind:
[WebMethod]
public void GetDdlList()
{
if (!String.IsNullOrEmpty(txtServer.Text.ToString()))
ServerName = txtServer.Text.ToString();
if (!String.IsNullOrEmpty(txtUnm.Text.ToString()))
UserName = txtUnm.Text.ToString();
if (!String.IsNullOrEmpty(txtPwd.Text.ToString()))
Pwd = txtPwd.Text.ToString();
SqlConnection conn = new SqlConnection("Data Source=" + ServerName + ";User ID=" + UserName + ";Password=" + Pwd);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "SELECT name FROM sys.databases";
cmd.CommandType = CommandType.Text;
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
ddlDbnm.Items.Add(rdr.GetString(0).ToString());
}
conn.Close();
}
Script:
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript">
$(document).ready(function () {
$('#<%= ddlDbnm.ClientID %>').click(function () {
PageMethod.GetDdlList();
//alert('hi');
})
});
</script>
When I write GetDdlList() code on button click, it executes successfully. But I don't want to use button click. Instead I want this code to be executed when I click on DropDownList. In above example nothing is happening when I click on Dropdownlist.