【问题标题】:How to retain the dynamically generated web control inputs in viewstates for search functions如何在搜索功能的视图状态中保留动态生成的 Web 控件输入
【发布时间】:2014-06-26 20:48:51
【问题描述】:

我能够根据 DropDownlist 选择的输入生成动态 Web 控件。我需要实现一个按钮单击事件,该事件应该遍历来自这些动态 Web 控件的所有输入并在 JQGrid 表中显示结果。

要求:如何使用 SavedViewState() 和 LoadViewState() 保留来自这些动态 Web 控件的所有输入文本。我是动态 Web 控件和视图状态的新手。需要帮助

我已经给出了完整的示例代码。

我的 C# 代码:

  private void BindDropDownLists()
     {
         column_list_for_filter.ConnectionString = connection;
         string item = "--Select--";

         column_list_for_filter.SelectCommand = "SELECT DATA_TYPE + '_' + convert(varchar(10), ROW_NUMBER() OVER(ORDER BY DATA_TYPE))as DATA_TYPE, COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE (TABLE_NAME = 'RESULT' AND COLUMN_NAME IN ('Column1','Column2','Column3','Column4'))";
         DropDownList5.DataTextField = "COLUMN_NAME";
         DropDownList5.DataValueField = "DATA_TYPE";
         DropDownList5.DataBind();
         DropDownList5.Items.Insert(0, item);
     }

  protected void createdynamiccontrols_decimal()
     {
         int i = DropDownList5.SelectedIndex;
         ++i;
         TableRow row = new TableRow();
         row.ID = "TableRow_";
         TableCell cell1 = new TableCell();
         cell1.ID = "TableCell_";

         DropDownList Range_DDL_Decimal = new DropDownList();
         Range_DDL_Decimal.ID = "RandeDDL_Decimal" + i.ToString();
         Range_DDL_Decimal.Items.Insert(0, new ListItem("--Select--", "--Select--"));
         Range_DDL_Decimal.Items.Insert(1, new ListItem("Equal", "Equal"));
         Range_DDL_Decimal.Items.Insert(2, new ListItem("NotEqual", "NotEqual"));
         Range_DDL_Decimal.Items.Insert(3, new ListItem("greater than", "greater than"));
         Range_DDL_Decimal.Items.Insert(4, new ListItem("lesser than", "lesser than"));
         Range_DDL_Decimal.Items.Insert(5, new ListItem("greater than or equal to", "greater than or equal to"));
         Range_DDL_Decimal.Items.Insert(6, new ListItem("lesser than or equal to", "lesser than or equal to"));
         Range_DDL_Decimal.Items.Insert(7, new ListItem("Contains", "Contains"));
         Range_DDL_Decimal.Items.Insert(8, new ListItem("Is Null", "Is Null"));
         Range_DDL_Decimal.Items.Insert(9, new ListItem("Is Not Null", "Is Not Null"));
         Range_DDL_Decimal.Items.Insert(10, new ListItem("Between", "Between"));
         Range_DDL_Decimal.AutoPostBack = true;
         Range_DDL_Decimal.SelectedIndexChanged += new System.EventHandler(Range_DDL_Decimal_SelectedIndexChanged);
         cell1.Controls.Add(Range_DDL_Decimal);

         //// Add the TableCell to the TableRow  
         row.Cells.Add(cell1);
         dynamic_filter_table.Rows.Add(row);
         dynamic_filter_table.EnableViewState = true;
         ViewState["dynamic_filter_table"] = true;
     }

     protected void Range_DDL_Decimal_SelectedIndexChanged(object sender, EventArgs e)
     {
         int j = DropDownList5.SelectedIndex;
         ++j;

         TableCell cell2 = new TableCell();

         TextBox tb1 = new TextBox();
         TextBox tb2 = new TextBox();

         Label lbl1 = new Label();
         Label lbl2 = new Label();

         // Set a unique ID for each TextBox added      
         tb1.ID = "lowerbound_" + j.ToString();
         tb2.ID = "upperbound_" + j.ToString();
         lbl1.Text = "LowerBound:";
         lbl1.Font.Size = FontUnit.Point(10);
         lbl1.Font.Bold = true;
         lbl1.Font.Name = "Arial";

         lbl2.Text = "UpperBound:";
         lbl2.Font.Size = FontUnit.Point(10);

         lbl2.Font.Bold = true;
         lbl2.Font.Name = "Arial";

         cell2.Controls.Add(lbl1);
         cell2.Controls.Add(tb1);
         cell2.Controls.Add(lbl2);
         cell2.Controls.Add(tb2);

         TableRow rowtwo = dynamic_filter_table.FindControl("TableRow_") as TableRow;
         rowtwo.Cells.Add(cell2);


         dynamic_filter_table.Rows.Add(rowtwo);

         dynamic_filter_table.EnableViewState = true;
         ViewState["dynamic_filter_table"] = true;

     }

protected override object SaveViewState()
 {
      /***1. Retain DDL 1 selected text
       2. Retain DDL 2 selected text DDL2 generated by DDL 1
       3. Retain Tb1.text and tb2.text generated by DDL2 ***/
       //How to Proceed  
 }

protected override void LoadViewState(object savedState)
 {
       //How to Proceed
 }

   protected void Button1_Click(object sender, EventArgs e)
    {

         int j = DropDownList5.SelectedIndex;
         ++j;
         Panel6.Visible = true;
         JQGrid9.Visible = true;

         //Find Control not working
         TextBox lowerboundd = dynamic_filter_table.FindControl("lowerbound_" + j.ToString()) as TextBox;

         TextBox upperbound = dynamic_filter_table.FindControl("upperbound_" + j.ToString()) as TextBox;

          con.Open();

         // **How to pass values to this query from savedstate**  
         SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM RESULT WHERE " + DropDownList5.Text + DDL2.Text + " >= " + lowerboundd.Text + " AND " + DropDownList5.Text + " <= " + upperbound.Text, con);
         DataSet ds = new DataSet();
         da.Fill(ds);
         con.Close();
         Session["DataforSearch"] = ds.Tables[0];

     }

 protected void Page_Load(object sender, EventArgs e)
    {

        Panel6.Visible = false;
        JQGrid9.Visible = false;


        if (Session["DataforSearch"] != null)
        {
            Panel6.Visible = true;
            JQGrid9.Visible = true;
            JQGrid9.DataSource = Session["DataforSearch"] as string;

         }

       if (!IsPostBack)
        {
            BindDropDownLists();
        }

        else
        {
            if (!String.IsNullOrEmpty(DropDownList5.SelectedValue))
            {
                if (DropDownList5.SelectedValue.Contains("decimal"))
                {
                    createdynamiccontrols_decimal();
                }

                else if (DropDownList5.SelectedValue.Contains("varchar"))
                {
                    createdynamiccontrols_varchar();
                }
                else if (DropDownList5.SelectedValue.Contains("datetime"))
                {
                    createdynamiccontrols_datetime();
                }

                else if (DropDownList5.SelectedValue.Contains("int"))
                {
                    createdynamiccontrols_int();
                }

            }

        }
    }

  protected void Page_PreInit(object sender, EventArgs e)
     {

         if (!IsPostBack)
         {
             BindDropDownLists();
         }

         else
         {
             if (!String.IsNullOrEmpty(DropDownList5.SelectedValue))
             {
                 if (DropDownList5.SelectedValue.Contains("decimal"))
                 {
                     createdynamiccontrols_decimal();
                 }

                 else if (DropDownList5.SelectedValue.Contains("varchar"))
                 {
                     createdynamiccontrols_varchar();
                 }
                 else if (DropDownList5.SelectedValue.Contains("datetime"))
                 {
                     createdynamiccontrols_datetime();
                 }

                 else if (DropDownList5.SelectedValue.Contains("int"))
                 {
                     createdynamiccontrols_int();
                 }

             }

         }

【问题讨论】:

  • 您可以保留/保存第一次创建它们的步骤/选定值,然后您需要在 Page Init 上重新创建它们。
  • @Aristos 你能帮我在上面添加几行代码吗?
  • found 一种方法,这里还有其他方法

标签: c# asp.net dynamic viewstate web-controls


【解决方案1】:

您不需要覆盖 SaveViewState/LoadViewState。但是您必须在 ViewState 中保留选择才能在回发时创建控件。这应该有效:

internal enum DataType
{
    None = 0,
    Decimal
}

internal enum Operator
{
    Equal,
    GreaterThan,
    None
}

public partial class _Default : Page
{
    private DataType DataType
    {
        get
        {
            object dataType = ViewState["DataType"];
            if (dataType != null) return (DataType)dataType;
            return DataType.None;
        }
        set { ViewState["DataType"] = value; }
    }
    private int? DataTypeIndex
    {
        get { return ViewState["DataTypeIndex"] as int?; }
        set { ViewState["DataTypeIndex"] = value; }
    }
    private Operator Operator
    {
        get
        {
            var @operator = ViewState["Operator"];
            if (@operator != null)
                return (Operator)@operator;
            return Operator.None;
        }
        set { ViewState["Operator"] = value; }
    }
    private void BindDropDownLists()
    {
        column_list_for_filter.ConnectionString = connection;
        string item = "--Select--";

        column_list_for_filter.SelectCommand = "SELECT DATA_TYPE + '_' + convert(varchar(10), ROW_NUMBER() OVER(ORDER BY DATA_TYPE))as DATA_TYPE, COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE (TABLE_NAME = 'RESULT' AND COLUMN_NAME IN ('Column1','Column2','Column3','Column4'))";
        DropDownList5.DataTextField = "COLUMN_NAME";
        DropDownList5.DataValueField = "DATA_TYPE";
        DropDownList5.DataBind();
        DropDownList5.Items.Insert(0, item);
    }

    protected void DropDownList5_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataTypeIndex = DropDownList5.SelectedIndex;
        if (DropDownList5.SelectedValue.Contains("decimal"))
        {
            DataType = DataType.Decimal;
        }
        CreateDataTypeControl();
        //else if (DropDownList5.SelectedValue.Contains("varchar"))
        //{
        //DataType = DataType.Varchar;
        //}
        //else if (DropDownList5.SelectedValue.Contains("datetime"))
        //{
        //DataType = DataType.DateTime;
        //}
        //else if (DropDownList5.SelectedValue.Contains("int"))
        //{
        //DataType = DataType.Integer;
        //}
    }
    protected void Range_DDL_Decimal_SelectedIndexChanged(object sender, EventArgs e)
    {
        var ddl = (DropDownList)sender;
        switch (ddl.SelectedIndex)
        {
            case 1:
                Operator = WebApplication1.Operator.Equal;
                break;
        }
        CreateRangeControls();
    }

    protected void createdynamiccontrols_decimal()
    {
        int i = DataTypeIndex.GetValueOrDefault();
        ++i;
        TableRow row = new TableRow();
        row.ID = "TableRow_";
        TableCell cell1 = new TableCell();
        cell1.ID = "TableCell_";

        DropDownList Range_DDL_Decimal = new DropDownList();
        Range_DDL_Decimal.ID = "RandeDDL_Decimal" + i.ToString();
        Range_DDL_Decimal.Items.Insert(0, new ListItem("--Select--", "--Select--"));
        Range_DDL_Decimal.Items.Insert(1, new ListItem("Equal", "Equal"));
        Range_DDL_Decimal.Items.Insert(2, new ListItem("NotEqual", "NotEqual"));
        Range_DDL_Decimal.Items.Insert(3, new ListItem("greater than", "greater than"));
        Range_DDL_Decimal.Items.Insert(4, new ListItem("lesser than", "lesser than"));
        Range_DDL_Decimal.Items.Insert(5, new ListItem("greater than or equal to", "greater than or equal to"));
        Range_DDL_Decimal.Items.Insert(6, new ListItem("lesser than or equal to", "lesser than or equal to"));
        Range_DDL_Decimal.Items.Insert(7, new ListItem("Contains", "Contains"));
        Range_DDL_Decimal.Items.Insert(8, new ListItem("Is Null", "Is Null"));
        Range_DDL_Decimal.Items.Insert(9, new ListItem("Is Not Null", "Is Not Null"));
        Range_DDL_Decimal.Items.Insert(10, new ListItem("Between", "Between"));
        Range_DDL_Decimal.SelectedIndexChanged += new System.EventHandler(Range_DDL_Decimal_SelectedIndexChanged);
        Range_DDL_Decimal.AutoPostBack = true;
        cell1.Controls.Add(Range_DDL_Decimal);

        //// Add the TableCell to the TableRow  
        row.Cells.Add(cell1);
        dynamic_filter_table.Rows.Add(row);
        dynamic_filter_table.EnableViewState = true;
        ViewState["dynamic_filter_table"] = true;
    }
    protected void CreateRangeTextBoxes()
    {
        int j = DataTypeIndex.GetValueOrDefault();
        ++j;

        TableCell cell2 = new TableCell();
        cell2.ID = "Range";

        TextBox tb1 = new TextBox();
        TextBox tb2 = new TextBox();

        Label lbl1 = new Label();
        Label lbl2 = new Label();

        // Set a unique ID for each TextBox added      
        tb1.ID = "lowerbound_" + j.ToString();
        tb2.ID = "upperbound_" + j.ToString();
        lbl1.Text = "LowerBound:";
        lbl1.Font.Size = FontUnit.Point(10);
        lbl1.Font.Bold = true;
        lbl1.Font.Name = "Arial";

        lbl2.Text = "UpperBound:";
        lbl2.Font.Size = FontUnit.Point(10);

        lbl2.Font.Bold = true;
        lbl2.Font.Name = "Arial";

        cell2.Controls.Add(lbl1);
        cell2.Controls.Add(tb1);
        cell2.Controls.Add(lbl2);
        cell2.Controls.Add(tb2);

        TableRow rowtwo = dynamic_filter_table.FindControl("TableRow_") as TableRow;
        rowtwo.Cells.Add(cell2);


        dynamic_filter_table.Rows.Add(rowtwo);

        dynamic_filter_table.EnableViewState = true;
        ViewState["dynamic_filter_table"] = true;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int j = DropDownList5.SelectedIndex;
        ++j;
        Panel6.Visible = true;
        JQGrid9.Visible = true;

        Find Control not working
        TextBox lowerboundd = dynamic_filter_table.FindControl("lowerbound_" + j.ToString()) as TextBox;
        TextBox upperbound = dynamic_filter_table.FindControl("upperbound_" + j.ToString()) as TextBox;
        DropDownList range = dynamic_filter_table.FindControl("RandeDDL_Decimal" + j.ToString()) as DropDownList;

        con.Open();

        SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM RESULT WHERE " + DropDownList5.Text + DDL2.Text + " >= " + lowerboundd.Text + " AND " + DropDownList5.Text + " <= " + upperbound.Text, con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        Session["DataforSearch"] = ds.Tables[0];

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Panel6.Visible = false;
        JQGrid9.Visible = false;

        if (Session["DataforSearch"] != null)
        {
            Panel6.Visible = true;
            JQGrid9.Visible = true;
            JQGrid9.DataSource = Session["DataforSearch"] as string;

        }
        CreateDataTypeControl();
        CreateRangeControls();
    }

    void CreateDataTypeControl()
    {
        switch (DataType)
        {
            case DataType.Decimal:
                createdynamiccontrols_decimal();
                break;
        }
    }

    void CreateRangeControls()
    {
        switch (Operator)
        {
            case WebApplication1.Operator.GreaterThan:
            case Operator.Equal:
                CreateRangeTextBoxes();
                break;
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多