[英]Add Items to DropDownList at Runtime
我在C#中使用Asp.Net。 我想在運行時將數據庫名稱添加到DropDownList。 這是代碼。
背后的代碼:
[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 src="Scripts/jquery-1.4.1.min.js" type="text/javascript">
$(document).ready(function () {
$('#<%= ddlDbnm.ClientID %>').click(function () {
PageMethod.GetDdlList();
//alert('hi');
})
});
</script>
當我在單擊按鈕時編寫GetDdlList()代碼時,它將成功執行。 但是我不想使用按鈕單擊。 相反,我希望在單擊DropDownList時執行此代碼。 在上面的示例中,當我單擊“下拉列表”時沒有任何反應。
您可以在jquery中獲得dropdownlist的click事件,然后對您的方法進行ajax調用。 以下是下拉列表的get click事件的代碼:
$(document).ready(function () {
var isClickToLoad = true;
$('#<%= ddlDbnm.ClientID %>').click(function () {
if (isClickToLoad == false) {
//The following line is not allowing the selection changed value to persist
//But commenting it out will call the server side code just once
//i.e. when first time the dropdownlist is clicked
//You need to handle it
isClickToLoad = true;
return;
}
isClickToLoad = false;
$('#<%= ddlDbnm.ClientID %>').empty();
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/GetDdlList",
data: '{ }',
dataType: "json",
success: function (msg) {
var options = "";
$.each(msg.d, function (index, value) {
options = options + "<option>" + value + "</option>";
});
$('#<%= ddlDbnm.ClientID %>').html(options);
}
});
})
});
以下是webmethod代碼:
[WebMethod]
public static List<string> GetDdlList()
{
//This can be your call to database. Hard-coded here for simplicity
List<string> lst = new List<string>();
lst.Add("aaa");
lst.Add("bbb");
return lst;
}
此處的鏈接詳細說明了如何在代碼中使用jquery ajax。
希望這可以幫助
您可以使用此js動態下拉列表:
HTML:
<select>
<option>Select Something</option>
<option> </option>
<option> </option>
<option> </option>
</select>
JavaScript的:
$(document).ready(function () {
var isClickToLoad = true;
$("select").click(function(e) {
if (isClickToLoad == false){
isClickToLoad = true;
return;
}
isClickToLoad = false;
$("select").empty().html("<option>Loading options</option>");
setTimeout(function() {
$.ajax({
type: "POST",
url: 'your url',
traditional: true,
dataType: "json",
data: {},
success: function(resp) {
$('select').empty();
if (resp.length > 0) {
var listItems = [];
for (var i = 0; i < resp.length; i++) {
listItems.push('<option value="' +
resp[i].Value + '">' + resp[i].Text
+ '</option>');
}
$('select').append(listItems.join(''));
}
}
});
$("select :nth-child(1)").attr("selected", "selected");
}, 500);
});
});
您已經在Visual Studio中為ComboBox內置了click事件。 右鍵單擊Combox並選擇屬性。 在“屬性”中,單擊“事件”按鈕(加亮符號),然后在其中找到事件“ Click”,然后雙擊該事件,即使這會自動創建click事件。 您可以將以上代碼添加到此事件中,然后嘗試運行它。 這應該有助於您實現目標。 試試這個,讓我知道這是否對您不起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.