我一直在创建一个UI面板,并为界面输入提供动态数据。 为此我想要一个基于用户选择的几个SQL查询的valus的SQL查询过滤器。
原型:
我生成了面板并且数据显示没有问题,但是因为我不知道在查看第一个查询之前我将拥有多少个过滤器,因此我在过滤器上动态生成checkboxes
和dropdowns
。
我遇到的问题是我无法在下拉列表或复选框中访问用户选择的值。
我为每个元素分配了唯一的ID,因此最快的解决方案是getElementByID
的c#等价物?
我将在下面发布一些代码:
protected string SQConnWhere(string TableName = "Nonya", string FieldName = "Error!!!", int i=0)
{
string ConnectionString = "real string removed";
cmdText = @"SELECT DISTINCT " + FieldName + " FROM tablenamechangedfromrealonetoprotectthe innocent";
Label myLabel = new Label();
Label myLabelA = new Label();
CheckBox myCheckBox = new CheckBox();
DropDownList myList = new DropDownList();
myList.ID = "myList" + i;
myCheckBox.ID = "myCheckBox" + i;
myLabel.ID = "myLabel" + i;
myLabelA.ID = "myLabelA" + i;
myLabel.Text = FieldName;
PlaceHolder1.Controls.Add(myLabel);
PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
PlaceHolder1.Controls.Add(myList);
myCheckBox.Text = "Use This Field in Filter?";
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
try
{
SqlDataAdapter adapter = new SqlDataAdapter(cmdText, conn);
DataSet ds2 = new DataSet();
adapter.Fill(ds2);
myList.DataSource = ds2;
myList.DataTextField = FieldName;
myList.DataBind();
ViewState["Data"] = ds2;
}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
}
PlaceHolder1.Controls.Add(myCheckBox);
PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
//May not need this?
//filterList.Add(FieldName);
myLabelA.Text = cmdText;
PlaceHolder1.Controls.Add(myLabelA);
PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
//myCheckBox.CheckedChanged += new EventHandler(UpdatemyCheckBox);
//pnlCheckList.Controls.Add(myCheckBox);
// register the control to cause asynchronous postbacks
//ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(myCheckBox);
//Label4.Text = FieldName;
return cmdText;
}
P.S。这是我的第一篇文章,尽管长时间浏览网站,感谢迄今为止的所有帮助!!!
答案 0 :(得分:0)
我用
解决了它 ManualSQConnWhere(DropDownList1, myTable, "Run_ID", CheckBox1);
ManualSQConnWhere(DropDownList2, myTable, "Job_Status", CheckBox2);
ManualSQConnWhere(DropDownList3, myTable, "Job_Plan", CheckBox3);
protected string ManualSQConnWhere(DropDownList myList, string TableNameWeb2, string FieldName, CheckBox myCheckBox)
{
string ConnectionString = "Data Source=xxxxx;Initial Catalog=xxxxx;Integrated Security=True";
cmdText = @"SELECT DISTINCT " + FieldName + " FROM " + TableNameWeb2;
DataSet dbWeb2 = new DataSet();
myList.AutoPostBack = false;
//myList.ViewStateMode = ViewStateMode.Enabled;
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
try
{
SqlDataAdapter adapter = new SqlDataAdapter(cmdText, conn);
adapter.Fill(dbWeb2);
ViewState["Data"] = dbWeb2;
}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
}
myList.AppendDataBoundItems = true;
myList.DataSource = dbWeb2;
myList.DataTextField = FieldName;
myList.Items.Add(new ListItem("<None Selected>", string.Empty));
if (dbWeb2.Tables.Count > 0)
{
myList.DataBind();
}
else
{
Label1.Text = "Error on the SQL Query" + cmdText;
return cmdText;
}
myCheckBox.Text = FieldName;
return cmdText;
}
此代码传达了下拉列表,我只是为它们输入了标签