簡體   English   中英

動態生成的DropDownList Web控件在選定項上隱藏

[英]Dynamically Generated DropDownList web controls hides on selected

我在創建動態Web控件時遇到麻煩。 我有一個帶有DataTextField =“ All_Columns”和DataValueField =“ DATA_TYPE”的SQL Databounded DropDownlist。

在DropDownList1_SelectedIndexChanged之后,我必須檢查數據類型為十進制還是Varchar為選定值。 如果是十進制,則必須調用Createdynamicwebcontrols_decimal()並創建DropDownList2。 如果是varchar,則必須調用Createdynamicwebcontrols_varchar()。

當前問題:在DropDownList2_SelectedIndexChanged上,我必須創建兩個動態文本框,並且必須保留輸入值,並借助Button Click事件在JQGrid中搜索並顯示數據。 但是DDL控件完全隱藏了

如何實現以上所有功能。 請幫助。 動態Web控件的新功能。

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#代碼:

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

      }

您可以在以下鏈接上找到源代碼和示例: http : //www.codeproject.com/Articles/20714/Dynamic-ASP-NET-control-creation-using-C

我也在網上找到了此代碼,希望對您有所幫助:您可以根據需要在此代碼中對此代碼進行修改,單擊另一個按鈕后會添加一個按鈕。

向表單添加新控件真的很容易。

您要做的就是像創建其他任何類一樣創建控件的新實例。 例如:

Button NewButton = new button();

然后,填寫控件的屬性。

NewButton.Text = "My New Button";

設置完成后,將創建一個事件處理程序。

NewButton.Click += new EventHandler(NewButton_Click);

然后,在按鈕處理程序中使用sender參數來告訴您按下了哪個動態按鈕。

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

CurrentButton.Text = "I was clicked";
}

最后,設置按鈕的最后一步是將控件添加到表單。

this.Controls.Add(NewButton);

暫無
暫無

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

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