[英]Use a variable of the column names to select the column returned from a query. (asp.net c#)
I have searched for most of the day and I believe I may just be not able to explain myself to a search engine. 我一天中大部分时间都在搜索,我相信我可能无法向搜索引擎解释自己。 Please forgive me on this. 请原谅我。 I have one drop down list I use to get the column names with the code below. 我有一个下拉列表,我用下面的代码来获取列名。
MySQLDataSource.ConnectionString = matt.GetConnectionString();
MySQLDataSource.SelectCommand = "SELECT name FROM sys.syscolumns WHERE (id =
(SELECT id FROM sys.sysobjects WHERE (name = 'tbl_EquipmentStock')))";
DropDownList1.DataValueField = "name";
DropDownList1.DataTextField = "name";
Now, the intention of my work is to use the selected item from this to create the sqldatasource of the next drop down. 现在,我的工作目的是使用从中选择的项创建下一个下拉列表的sqldatasource。
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
funtions matt = new funtions(); // funtions is the class that contains my connection string.
string frank = DropDownList1.SelectedValue;
a_c_instruction.Text = " Data filtered on: ";
selectedItem.Text = frank;
string sql = "SELECT " + frank + " FROM tbl_EquipmentStock";
AnotherSqlDataSource.ConnectionString = matt.GetConnectionString();
AnotherSqlDataSource.SelectCommand = sql;
DropDownList2.DataValueField = frank;
DropDownList2.DataTextField = frank;
DropDownList2.Visible = true;
}
However, when I attempt to get this to work, the DropDownList2 doesn't populate. 但是,当我尝试使其工作时,不会填充DropDownList2。 What could i be missing? 我可能会缺少什么? Thank you for looking at my problem. 谢谢你看我的问题。
I tested your code and it works on my machine. 我测试了您的代码,它可以在我的机器上运行。 I'm guessing that, in order for this to happen right after the column is selected, you need to make sure that the AutoPostBack
option on DropDownList1
is set to true
. 我猜想,为了在选择列之后立即发生这种情况,您需要确保DropDownList1
的AutoPostBack
选项设置为true
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.