我希望能夠有一個可點擊的按鈕,動態添加從數據綁定中提取其值的下拉列表。我的問題是,它看起來像ASP.Net試圖解析JavaScript之前的下拉列表,並抱怨該ID。如何動態添加和刪除ASP:DropDownLists?
這是我收到的錯誤:
DotNetNuke.Services.Exceptions.ModuleLoadException: 'ddlToolPusher' + i + '' is not a valid identifier. ---> System.Web.HttpParseException: 'ddlToolPusher' + i + '' is not a valid identifier. --->
以下是我有:
ASCX
<div id="dropdowns">
Toolpushers:
<asp:DropDownList runat="server" id="ddlToolPusher" />
<asp:Button runat="server" ID="btnSubmit" Text="Submit" />
</div>
<p><a href="#" id="addDD">add something</a></p>
<script type="text/javascript">
$(document).ready(function() {
var ddlDiv = $('#dropdowns');
var i = 1;
$('#addDD').click(function() {
$('<asp:DropDownList runat="server" id="ddlToolPusher' + i + '" />').appendTo(ddlDiv);
i++;
return;
});
});
</script>
ascx.cs
protected void Page_Load(object sender, EventArgs e)
{
MyDatabase db = new MyDatabase();
rmModuleNavigationWrapper.InnerHtml = Helper.BuildModuleNavigation();
btnSubmit.Click += new EventHandler(ButtonHandler);
if (!Page.IsPostBack)
{
BindData();
}
}
protected void BindData()
{
string connStr = ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connStr))
{
SqlCommand cmd = new SqlCommand("robinson_GetEmployeesForRigDDL", conn);
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
ddlToolPusher.DataSource = reader;
ddlToolPusher.DataTextField = "Name";
ddlToolPusher.DataValueField = "ID";
ddlToolPusher.DataBind();
ddlToolPusher.Items.Insert(0, new ListItem("Choose One", "0"));
ddlToolPusher.SelectedIndex = 0;
}
}
好主意....並做:) – fnostro