动态生成的DropDownList Web控件揣上选择 [英] Dynamically Generated DropDownList web controls hides on selected
问题描述
我面对创作动态web控件的烦恼。我有一个SQL Databounded DROPDOWNLIST与DataTextField =ALL_COLUMNS和DataValueField =DATA_TYPE。
一旦DropDownList1_SelectedIndexChanged我必须检查的数据类型为十进制或VARCHAR的选定值。如果是小数我必须调用Createdynamicwebcontrols_decimal(),并创建DropDownList2。如果它是VARCHAR我必须调用Createdynamicwebcontrols_varchar()。
本期:当DropDownList2_SelectedIndexChanged我必须用按钮的Click事件的帮助下创建两个动态文本框和输入值必须保留搜索和显示在jqGrid的数据。但是DDL控制完全隐藏
如何实现以上所有功能。需要帮助请。新的动态Web控件。
.aspx的code:
< ASP:DropDownList的ID =DropDownList5=服务器的DataSourceID =column_list_for_filterDataTextField =ALL_COLUMNSDataValueField =DATA_TYPEOnSelectedIndexChanged =DropDownList5_SelectedIndexChanged的AutoPostBack =真正的>
< / ASP:DropDownList的>
< ASP:SqlDataSource的ID =column_list_for_filter=服务器的ConnectionString =<%$的ConnectionStrings:DatabaseConnectionString%>中的SelectCommand =SELECT COLUMN_NAME AS'ALL_COLUMNS,DATA_TYPE information_schema.columns在(TABLE_NAME ='表')>< / ASP:SqlDataSource的>
C#code:
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
Panel6.Visible = FALSE;
JQGrid9.Visible = FALSE; 如果(会话[DataforSearch]!= NULL)
{
Panel6.Visible = TRUE;
JQGrid9.Visible =真;
JQGrid9.DataSource =会话[DataforSearch]作为字符串; } }保护无效Page_ preINIT(对象发件人,EventArgs的发送)
{ //如何进行
} 保护无效DropDownList5_SelectedIndexChanged(对象发件人,EventArgs的发送)
{
如果(DropDownList5.SelectedValue ==十进制)
{
createdynamiccontrols_decimal();
} 否则,如果(DropDownList5.SelectedValue ==INT)
{
createdynamiccontrols_int();
} 否则,如果(DropDownList5.SelectedValue ==VARCHAR)
{
createdynamiccontrols_varchar();
} //如何进行 } 保护无效createdynamiccontrols_decimal() {
INT I = DropDownList5.SelectedIndex;
++我;
连续的TableRow;
行=新的TableRow(); TableCell的CELL1;
CELL1 =新的TableCell(); // DropDownList2
DropDownList的Range_DDL;
Range_DDL =新的DropDownList();
Range_DDL.ID =RandeDDL+ i.ToString();
Range_DDL.Items.Insert(0,新的ListItem( - 选择---, - 选择 - ));
Range_DDL.Items.Insert(1,新的ListItem(平等,平等));
Range_DDL.Items.Insert(2,新的ListItem(NotEqual,NotEqual));
Range_DDL.Items.Insert(3,新的ListItem(大于,大于));
Range_DDL.Items.Insert(2,新的ListItem(比小,比较小));
Range_DDL.Items.Insert(2,新的ListItem(大于或等于,大于或等于));
Range_DDL.Items.Insert(2,新的ListItem(比较小或等于,比更少或等于));
Range_DDL.Items.Insert(2,新的ListItem(包含,含有));
Range_DDL.Items.Insert(2,新的ListItem(IS NULL,IS NULL));
Range_DDL.Items.Insert(2,新的ListItem(不为空,不为空));
Range_DDL.Items.Insert(2,新的ListItem(之间,间));
Range_DDL.AutoPostBack = TRUE;
Range_DDL.SelectedIndexChanged + =新System.EventHandler(Range_DDL_SelectedIndexChanged); cell1.Controls.Add(Range_DDL);
////了的TableCell添加到的TableRow
row.Cells.Add(CELL1); dynamic_filter_table.Rows.Add(行); dynamic_filter_table.EnableViewState = TRUE;
的ViewState [dynamic_filter_table] =真;
} // DropdownList2创建动态文本框
保护无效Range_DDL_SelectedIndexChanged(对象发件人,EventArgs的发送)
{
INT J = DropDownList5.SelectedIndex;
++焦耳; 连续的TableRow;
行=新的TableRow(); 的TableRow rowtwo;
rowtwo =新的TableRow(); TableCell的CELL1;
CELL1 =新的TableCell(); TableCell的CELL 2;
CELL 2 =新的TableCell(); TableCell的CELL3;
CELL3 =新的TableCell(); 文本框TB1;
TB1 =新的TextBox();
文本框TB2;
TB2 =新的TextBox();
标签LBL1; LBL1 =新标签();
标签LBL2;
LBL2 =新标签();
////设置一个唯一ID添加的每个文本框
tb1.ID =lowerbound_+ j.ToString();
tb2.ID =upperbound_+ j.ToString();
lbl1.Text =下界;
lbl1.Font.Size = FontUnit.Point(10);
lbl1.Font.Bold = TRUE;
lbl1.Font.Name =宋体; lbl2.Text =UPPERBOUND:
lbl2.Font.Size = FontUnit.Point(10); lbl2.Font.Bold = TRUE;
lbl2.Font.Name =宋体; 按钮addmore;
addmore =新按钮();
addmore.ID =按钮+ j.ToString();
addmore.Text =+;
addmore.Click + =新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(CELL 2);
rowtwo.Cells.Add(CELL3); dynamic_filter_table.Rows.Add(行); dynamic_filter_table.EnableViewState = TRUE;
的ViewState [dynamic_filter_table] =真; }//按钮单击检索中的jqGrid动态生成的文本框,并显示输入。
保护无效的button1_Click(对象发件人,EventArgs的发送)
{ INT J = DropDownList5.SelectedIndex;
++焦耳;
Panel6.Visible = TRUE;
JQGrid9.Visible =真;
文本框lowerboundd = dynamic_filter_table.FindControl(lowerbound_+ j.ToString())的文本框; 文本框上界= dynamic_filter_table.FindControl(upperbound_+ j.ToString())的文本框; con.Open();
SqlDataAdapter的大=新的SqlDataAdapter(SELECT A,B,C from表where+ DropDownList5.SelectedValue +> =+ lowerboundd.Text +和+ DropDownList5.SelectedValue +< =+ upperbound.Text, CON);
** //在检索文本框中输入问题 - 对象引用错误**
DataSet的DS =新的DataSet();
da.Fill(DS);
con.Close();
会话[DataforSearch] = ds.Tables [0]; }
您可以找到源头code和例如以下链接:
的http://www.$c$cproject.com/Articles/20714/Dynamic-ASP-NET-control-creation-using-C
我也发现了这个code网上,我希望它会帮助你:你可以在此根据本code按钮您的需求上的另一个按钮的点击添加修改
添加一个新的控件到窗体是很容易的。
所有你所要做的就是创建一个像任何其他类控制的新实例。
例如:
按钮NewButton =新按钮();
然后你填写的控件的属性。
NewButton.Text =我的新按钮
安装完之后的事件处理程序。
NewButton.Click + =新的EventHandler(NewButton_Click);
然后在按钮处理程序告诉它动态按钮是pressed使用sender参数。
无效NewButton_Click(对象发件人,EventArgs的发送)
{
按钮CurrentButton =(按钮)发送;CurrentButton.Text =我被点击了;
}
然后终于在设置按钮了最后一步是将控件添加到窗体。
this.Controls.Add(NewButton);
I am facing troubles in creation of dynamic web controls. I have a SQL Databounded DropDownlist with DataTextField ="All_Columns" and DataValueField="DATA_TYPE".
Upon DropDownList1_SelectedIndexChanged i must check for the datatype as decimal or Varchar for the Selected value. If it is decimal i must call Createdynamicwebcontrols_decimal() and create DropDownList2. If it is varchar i must call 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. But the DDL control hides completely
How to achieve above all features. Help needed please. New to dynamic web controls.
Aspx Code:
<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:
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
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.
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);
这篇关于动态生成的DropDownList Web控件揣上选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!