Pergunta

I have been making a UI panel and have dynamic data for input to the interface. For this I want a filter an SQL query based on user selected valus of several SQL querys.

For a prototype: I generated the panel and got the data to appear no problems, but since I do not know how many filters I will have until I look at the first query I made the checkboxes and dropdowns on the filter dynamically.

The problem I have is I cannot access the user selected values in the dropdowns or the checkboxes.

I assigned unique ID's to each element so the fastest solution would be the c# equivalent of getElementByID?

I will post some of the code below:

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. This is my first post despite browsing the website for a long time, thanks for all the help thus far!!!

Foi útil?

Solução

I solved it using

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

This code propogated the dropdowns and I simply entered the tags for them

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top