簡體   English   中英

獲取C#中動態生成的下拉列表/復選框的選定值

[英]Get selected value of a dynamically generated drop down/checkbox in c#

我一直在制作一個UI面板,並具有用於輸入到界面的動態數據。 為此,我想要一個基於用戶選擇的幾個SQL查詢值的SQL查詢過濾器。

對於一個原型:我生成了面板,並且數據看起來沒有問題,但是由於我不知道要擁有多少個過濾器,因此在查看第一個查詢之前,我動態地對過濾器進行了checkboxesdropdowns

我的問題是我無法訪問下拉菜單或復選框中用戶選擇的值。

我為每個元素分配了唯一的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;
}

附言:盡管瀏覽了很長時間,這還是我的第一篇文章,感謝您迄今為止提供的所有幫助!!!

我用解決了

    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;
    }

這段代碼宣傳了下拉菜單,我只是為其輸入了標簽

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM