如何将多个选择项文本从DropDownListCheckBoxes传递到参数化Sql

本文关键字:DropDownListCheckBoxes 参数 Sql 文本 选择 | 更新日期: 2023-09-27 18:11:06

我能够通过单击按钮保留标签内的DropDownListCheckbox多选择项文本。我需要从数据库中搜索基于DropDownListCheckBox多选择项目及其相关数据从SQL-Server数据库。

如何通过将DDL_CB列表项或标签文本的输入传递到参数化的SQL查询来实现使用按钮单击的搜索选项?

我的要求:搜索功能必须根据Label或DDL_CheckBox多选项中包含的文本在JQgrid中显示数据。

My c# code:

static string value1;
static string value2;
static string value3;
 protected void createmaincontrols()
    {
        //Create a Dynamic Panel
        DynamicPanel = new Panel();
        DynamicPanel.ID = "DynamicPanel";
        DynamicPanel.Width = 1600;
        //Create Main Table
        var dynamic_filter_table = new WebForms.Table();
        dynamic_filter_table.ID = "dynamic_filter_table_id";
        TableRow campaign_table_row = new TableRow();
        campaign_table_row.ID = "country_table_row";
        TableRow campaign_label_row = new TableRow();
        campaign_label_row.ID = "country_label_row";           
        TableCell campaignnamecell = new TableCell();
        campaignnamecell.ID = "countrynamecell";
        TableCell btncell = new TableCell();
        btncell.ID = "btncell";
        TableCell labelcell = new TableCell();
        labelcell.ID = "labelcell";
        //Create Campaigns DDL
        DropDownCheckBoxes DDL_checkbox = new DropDownCheckBoxes();
        DDL_checkbox.ID = "MainDDL_Countries";
        DDL_checkbox.AutoPostBack = true;
        DDL_checkbox.ForeColor = System.Drawing.Color.MidnightBlue;
        DDL_checkbox.Font.Size = FontUnit.Point(8);
        DDL_checkbox.Font.Bold = true;
        DDL_checkbox.Font.Name = "Arial";
        DDL_checkbox.AddJQueryReference = true;
        DDL_checkbox.UseButtons = true;
        DDL_checkbox.UseSelectAllNode = true;
        DDL_checkbox.Style.SelectBoxWidth = 200;
        DDL_checkbox.Style.DropDownBoxBoxWidth = 200;
        DDL_checkbox.Style.DropDownBoxBoxHeight = 130;
        DDL_checkbox.Texts.SelectBoxCaption = "Select Countries";
        DDL_checkbox.Items.Add(new ListItem("SINGAPORE"));
        DDL_checkbox.Items.Add(new ListItem("UNITED KINGDOM"));
        DDL_checkbox.Items.Add(new ListItem("MALAYSIA"));
        DDL_checkbox.Items.Add(new ListItem("INDIA"));
        DDL_checkbox.Items.Add(new ListItem("FRANCE"));
        DDL_checkbox.Items.Add(new ListItem("GERMANY"));
        DDL_checkbox.Items.Add(new ListItem("NORWAY"));
        DDL_checkbox.DataTextField = "Country Name";
        DDL_checkbox.DataBind();
        DDL_checkbox.AutoPostBack = true;
        DDL_checkbox.EnableViewState = false;
        Button submitbutton = new Button();
        submitbutton.ID = "mybutton";
        submitbutton.Text = "SubmitSelectedCountries";
        submitbutton.Click += new EventHandler(Buttonnew_Click);
        submitbutton.Font.Name = "Arial";
        submitbutton.Font.Bold = true;
        submitbutton.Font.Size = FontUnit.Point(8);
        submitbutton.ForeColor = System.Drawing.Color.MidnightBlue;
        submitbutton.BackColor = System.Drawing.Color.LightGray;
        submitbutton.UseSubmitBehavior = false;
        Label lblCampaignName = new Label();
        lblCampaignName.ID = "Countries";
        lblCampaignName.Font.Bold = true;
        lblCampaignName.Font.Size = FontUnit.Point(8);
        lblCampaignName.ForeColor = System.Drawing.Color.MidnightBlue;
        lblCampaignName.BackColor = System.Drawing.Color.LightGray;
        campaignnamecell.Controls.Add(DDL_checkbox);
        campaignnamecell.Controls.Add(submitbutton);
        campaignnamecell.Controls.Add(lblcountryname);
        campaign_table_row.Controls.Add(countrycell);
        dynamic_filter_table.Controls.Add(country_table_row);
        DynamicPanel.Controls.Add(dynamic_filter_table);
        SelectPanel.Controls.Add(DynamicPanel);
    }
c#代码使用按钮检索标签中的下拉复选项,单击
 protected void Buttonnew_Click(object sender, EventArgs e)
    {
        Table maintable = Select.FindControl("dynamic_filter_table_id") as Table;
        DropDownCheckBoxes DDL_checkbox = maintable.FindControl("MainDDL_Contries") as DropDownCheckBoxes;
        Label lblcountryname = maintable.FindControl("Country") as Label;
        List<String> Country_List = new List<string>();
        foreach (System.Web.UI.WebControls.ListItem item in DDL_checkbox.Items)
        {
            if (item.Selected)
            {
                Country_List.Add(item.Text);
            }
            lblcountryname .Text = String.Join(",", Country_List.ToArray());
        }           
    }

如何根据标签或下拉复选框选中的项目的参数化SQL查询输入搜索国家详细信息?

protected void Button4_Click(object sender, EventArgs e)
    {
       Table maintable = Select.FindControl("dynamic_filter_table_id") as Table;
        int rc = maintable.Rows.Count;
        if (rc == 2)
             {
                    //Three country selected in DDL_checkbox
                    DropDownCheckBoxes d4 = maintable.FindControl("MainDDL_Countries") as DropDownCheckBoxes;
                    Label lblcountryname = maintable.FindControl("Countries") as Label;
                    var countryname= test.ToString().Split(new[] { ',', ''n' }).ToArray();
                    if(countryname.Count() >=1 && countryname.Count() <=3)
                   {
                    value1 = countryname.ElementAt(0).ToString();
                    value2 = countryname.ElementAt(1).ToString();
                    value3 = countryname.ElementAt(2).ToString();
                   }
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandText = "SELECT C.Country_Name,C.Address FROM COUNTRYTABLE as C WHERE C.Country_Name in(@t4,@t5,@t6)";                        
                    cmd.Parameters.Add("@t4", SqlDbType.VarChar).Value = value1;
                    cmd.Parameters.Add("@t5", SqlDbType.VarChar).Value = value2;
                    cmd.Parameters.Add("@t6", SqlDbType.VarChar).Value = value3;
                    con.Open();
                    cmd.ExecuteNonQuery();
                    SqlDataAdapter sql = new SqlDataAdapter(cmd);
                    DataSet data = new DataSet();
                    sql.Fill(data);
                    con.Close();
                    Session["DataforSearch_DDL"] = data.Tables[0];
             }
     }

如何将多个选择项文本从DropDownListCheckBoxes传递到参数化Sql

无可否认这只是部分答案。它会让你开始。

当您提交包含多个选择项的表单时,所选择的项将作为逗号分隔的值传递。像这样:

value1,value2,etc

你希望你的查询有一个where子句,像这样:

where someTextfield in ('value1','value2','etc')

或不带引号的数值字段。然而,你明智地说你想使用参数。

部分答案到此结束

很久很久以前,我是这样做的:

for (int i = 0; i < param.Length; i++)
    if (param[i] != "" && param[i] != null) 
        s_comm.Parameters.AddWithValue(tParam + i.ToString(), param[i]);

地点:

private string tParam = "@Param";
string[] paramName // Name of the parameters
string[] param // Values for those parameters.

,我创建了这样一个语句:

string where = "";
if (paramName != null)
   for (int i = 0; i < paramName.Length; i++)
       if (paramName[i] != "" && paramName[i] != null)
          if (i == 0)
             where = " WHERE " + paramName[i] + " = " + tParam + i.ToString();
          else
             where += ", " + paramName[i] + " = " + tParam + i.ToString();
       else throw new Exception(noColumnName + " at position #:" + i.ToString());
else where = "";
if (table != "") return "SELECT * FROM " + table + where;

我相信有更优雅的解决方案,但这只是一个开始,对吗?

相关文章:
  • 没有找到相关文章