简体   繁体   English

动态生成的DropDownList Web控件在选定项上隐藏

[英]Dynamically Generated DropDownList web controls hides on selected

I am facing troubles in creation of dynamic web controls. 我在创建动态Web控件时遇到麻烦。 I have a SQL Databounded DropDownlist with DataTextField ="All_Columns" and DataValueField="DATA_TYPE". 我有一个带有DataTextField =“ All_Columns”和DataValueField =“ DATA_TYPE”的SQL Databounded DropDownlist。

Upon DropDownList1_SelectedIndexChanged i must check for the datatype as decimal or Varchar for the Selected value. 在DropDownList1_SelectedIndexChanged之后,我必须检查数据类型为十进制还是Varchar为选定值。 If it is decimal i must call Createdynamicwebcontrols_decimal() and create DropDownList2. 如果是十进制,则必须调用Createdynamicwebcontrols_decimal()并创建DropDownList2。 If it is varchar i must call Createdynamicwebcontrols_varchar(). 如果是varchar,则必须调用Createdynamicwebcontrols_varchar()。

Current Issue: Upon DropDownList2_SelectedIndexChanged i must create two dynamic textboxes and input values must be retained and search and display the data in a JQGrid with the help of Button Click event. 当前问题:在DropDownList2_SelectedIndexChanged上,我必须创建两个动态文本框,并且必须保留输入值,并借助Button Click事件在JQGrid中搜索并显示数据。 But the DDL control hides completely 但是DDL控件完全隐藏了

How to achieve above all features. 如何实现以上所有功能。 Help needed please. 请帮助。 New to dynamic web controls. 动态Web控件的新功能。

Aspx Code: Aspx代码:

<asp:DropDownList ID="DropDownList5" runat="server" DataSourceID="column_list_for_filter" DataTextField="All_Columns" DataValueField="DATA_TYPE" OnSelectedIndexChanged ="DropDownList5_SelectedIndexChanged"  AutoPostBack="true" >
            </asp:DropDownList>
            <asp:SqlDataSource ID="column_list_for_filter" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString %>" SelectCommand="SELECT COLUMN_NAME AS 'All_Columns', DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE (TABLE_NAME = 'TABLE')"></asp:SqlDataSource>

C# code: C#代码:

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;

        }

    }

protected void Page_PreInit(object sender, EventArgs e)
     {

         //How to Proceed
     }

   protected void DropDownList5_SelectedIndexChanged(object sender, EventArgs e)
     {
       if(DropDownList5.SelectedValue == "decimal")
      {
          createdynamiccontrols_decimal();
      }

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

       else if(DropDownList5.SelectedValue == "varchar")
      {
          createdynamiccontrols_varchar();
      }

    //How to Proceed

     }

 protected void createdynamiccontrols_decimal()

     {
         int i = DropDownList5.SelectedIndex;
         ++i;
         TableRow row;
         row = new TableRow();

         TableCell cell1;
         cell1 = new TableCell();

         //DropDownList2
         DropDownList Range_DDL;
         Range_DDL = new DropDownList();
         Range_DDL.ID = "RandeDDL" + i.ToString();
         Range_DDL.Items.Insert(0, new ListItem("--Select--", "--Select--"));
         Range_DDL.Items.Insert(1, new ListItem("Equal", "Equal"));
         Range_DDL.Items.Insert(2, new ListItem("NotEqual", "NotEqual"));
         Range_DDL.Items.Insert(3, new ListItem("greater than", "greater than"));
         Range_DDL.Items.Insert(2, new ListItem("lesser than", "lesser than"));
         Range_DDL.Items.Insert(2, new ListItem("greater than or equal to", "greater than or equal to"));
         Range_DDL.Items.Insert(2, new ListItem("lesser than or equal to", "lesser than or equal to"));
         Range_DDL.Items.Insert(2, new ListItem("Contains", "Contains"));
         Range_DDL.Items.Insert(2, new ListItem("Is Null", "Is Null"));
         Range_DDL.Items.Insert(2, new ListItem("Is Not Null", "Is Not Null"));
         Range_DDL.Items.Insert(2, new ListItem("Between", "Between"));
         Range_DDL.AutoPostBack = true;
         Range_DDL.SelectedIndexChanged += new System.EventHandler(Range_DDL_SelectedIndexChanged);

         cell1.Controls.Add(Range_DDL);


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


     }

 //DropdownList2 to create dynamic text boxes
 protected void Range_DDL_SelectedIndexChanged(object sender, EventArgs e)
     {
         int j = DropDownList5.SelectedIndex;
         ++j;

         TableRow row;
         row = new TableRow();

         TableRow rowtwo;
         rowtwo = new TableRow();

         TableCell cell1;
         cell1 = new TableCell();

         TableCell cell2;
         cell2 = new TableCell();

         TableCell cell3;
         cell3 = new TableCell();

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

         lbl1 = new Label();
         Label lbl2;
         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";

         Button addmore;
         addmore = new Button();
         addmore.ID = "Button" + j.ToString();
         addmore.Text = "+";
         addmore.Click += new System.EventHandler(addmore_click);


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

         cell3.Controls.Add(addmore);

         row.Cells.Add(cell1);
         row.Cells.Add(cell2);
         rowtwo.Cells.Add(cell3);

         dynamic_filter_table.Rows.Add(row);

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

     }

//Button Click to retrieve the input from dynamic generated text box and display in JQGrid.
 protected void Button1_Click(object sender, EventArgs e)
    {

             int j = DropDownList5.SelectedIndex;
             ++j;
             Panel6.Visible = true;
             JQGrid9.Visible = true;
             TextBox lowerboundd = dynamic_filter_table.FindControl("lowerbound_" + j.ToString()) as TextBox;

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

             con.Open();
             SqlDataAdapter da = new SqlDataAdapter("SELECT a,b,c FROM TABLE WHERE " + DropDownList5.SelectedValue + " >= " + lowerboundd.Text + " AND " + DropDownList5.SelectedValue + " <= " + upperbound.Text, con);
            **//Problems in retrieving the input of textboxes - Object Reference error**
             DataSet ds = new DataSet();
             da.Fill(ds);
             con.Close();
             Session["DataforSearch"] = ds.Tables[0];

      }

You can find the source code and example on following link : http://www.codeproject.com/Articles/20714/Dynamic-ASP-NET-control-creation-using-C 您可以在以下链接上找到源代码和示例: http : //www.codeproject.com/Articles/20714/Dynamic-ASP-NET-control-creation-using-C

I also found this code online, I hope it would help you: You can modify this according to your needs in this code Button is added on the click of an another button. 我也在网上找到了此代码,希望对您有所帮助:您可以根据需要在此代码中对此代码进行修改,单击另一个按钮后会添加一个按钮。

Adding a new control to a form is really easy. 向表单添加新控件真的很容易。

All you have to do is create a new instance of the control like any other class. 您要做的就是像创建其他任何类一样创建控件的新实例。 Eg: 例如:

Button NewButton = new button();

Then you fill out the properties of the control. 然后,填写控件的属性。

NewButton.Text = "My New Button";

After this setup an event handler. 设置完成后,将创建一个事件处理程序。

NewButton.Click += new EventHandler(NewButton_Click);

Then in the button handler to tell which dynamic button was pressed you use the sender parameter. 然后,在按钮处理程序中使用sender参数来告诉您按下了哪个动态按钮。

void NewButton_Click(object sender, EventArgs e)
{
Button CurrentButton = (Button)sender;

CurrentButton.Text = "I was clicked";
}

Then finally the last step in setting the button up is to add the control to the form. 最后,设置按钮的最后一步是将控件添加到表单。

this.Controls.Add(NewButton);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM