|
I have Created textbox, dropDownList Dynamically at run time.Now how do i get the textbox value and dropDown value in ButtonClick.
|
|
|
|
|
Can you give your sample code, for us its logic flow analysis,
or you did not make his code..??
|
|
|
|
|
TextBox txt = new TextBox();
txt.ID = ID;
txt.Text = DateTime.Now.ToString();
PlcHolder.Controls.Add(txt);
DropDownList ddl = new DropDownList();
ddl.ID = ID;
string strSql = "SELECT Code,[Name] FROM SDTS_GSTYPE";
DataTable dtBranch = getDataTable(strSql);
ddl.DataSource = dtBranch;
ddl.AutoPostBack = true;
ddl.DataTextField = "Name";
ddl.DataValueField = "Code";
ddl.SelectedIndex = 0;
ddl.DataBind();
PlcHolder.Controls.Add(ddl);
This is the code i have written to create a textbox.Now how do i get the textbox value and selected value of dropDownlist in my buttonClick event.
|
|
|
|
|
sorry, long time to replay......
first,
The first,
you should know basic first Page Postback ...
every time you create dynamic controls in codebehind, even when you do click on a control button, the page is always postback, and automatically you will never get the value out of control, even control was not there at all when you try to use the event FinControl ...
so, you have to declare the event re-create control when page IsPostBack.
This sample code to start what you want .. this is just a beginning to open its forward your logic .. you can add it from the code sample I gave .....
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
GenerateControlSelf();
}
else
{
var txt = new TextBox { ID = "tbx", Text = DateTime.Now.ToString(CultureInfo.InvariantCulture), ClientIDMode = ClientIDMode.Static };
holder.Controls.Add(txt);
var ddl = new DropDownList { ID = "ddlx" };
var nextYear = Convert.ToInt32(DateTime.Now.Year.ToString(CultureInfo.InvariantCulture)) + 1;
var lastYear = Convert.ToInt32(DateTime.Now.Year.ToString(CultureInfo.InvariantCulture)) - 10;
for (var n = lastYear; n < nextYear; n++)
{
ddl.Items.Add(n.ToString(CultureInfo.InvariantCulture));
}
ddl.AutoPostBack = true;
ddl.SelectedIndex = 0;
ddl.DataBind();
holder.Controls.Add(ddl);
}
}
private void GenerateControlSelf()
{
var txt = new TextBox { ID = "tbx", Text = DateTime.Now.ToString(CultureInfo.InvariantCulture) ,ClientIDMode = ClientIDMode.Static};
holder.Controls.Add(txt);
var ddl = new DropDownList {ID = "ddlx"};
var nextYear = Convert.ToInt32(DateTime.Now.Year.ToString(CultureInfo.InvariantCulture)) + 1;
var lastYear = Convert.ToInt32(DateTime.Now.Year.ToString(CultureInfo.InvariantCulture)) - 10;
for (var n = lastYear; n < nextYear; n++)
{
ddl.Items.Add(n.ToString(CultureInfo.InvariantCulture));
}
ddl.AutoPostBack = true;
ddl.SelectedIndex = 0;
ddl.DataBind();
holder.Controls.Add(ddl);
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (Control ctl in holder.Controls)
{
if (ctl is DropDownList)
{
DropDownList ddl = (DropDownList) ctl;
object x = ddl.SelectedItem;
}
}
foreach (Control ctl in holder.Controls)
{
if (ctl is TextBox)
{
TextBox tbx = (TextBox)ctl;
object x = tbx.Text;
}
}
}
I remind you again, the sample code is just for starters only, to pave the way you are thinking ...
|
|
|
|
|
Thank you very much.
Its working.
|
|
|
|
|
here i am wellcaom..be happy, do not ever hesitate to ask questions and share your knowledge if you already master ..
|
|
|
|