samedi 25 avril 2015

Accessing and checking values of dynamic controls ASP.NET


I have a scenario. following is the code:

Home.aspx

protected void Button1_Click(object sender, EventArgs e)
{
    try
        {
            if (!String.IsNullOrEmpty(txtbox_query.Text.Trim()))
                {
                    if (isTrue)
                        {
                            // To do statements
                        }
                        else
                        {
                            List<RequestAndResponse.Parameter> parameters = request.getParameter(txtbox_query.Text.Trim(), sourcePath, parameterValue);
                            Session["Data"] = parameters;
                            Response.Redirect("Result.aspx",false);
                        }

                    }

    }
        catch (Exception error)
        {
                Response.Write(error.Message);
        }
}

Result.aspx

protected void Page_Load(object sender, EventArgs e)
{
    parameters = (List<RequestAndResponse.Parameter>)Session["Data"];
        ContentPlaceHolder content = (ContentPlaceHolder)this.Form.FindControl("MainContent");
        for (int j = 1; j <= _arrViewState; j++)
        {
            string _id = j.ToString();
                TextBox txtfname = new TextBox();
                txtfname.ID = "TextBox_" + _id + "_";
                txtfname.Width = 160;
                txtfname.Text = parameters[(j - 1)].Value.ToUpper();
                txtfname.Attributes.Add("style", "color:#015D84;font-weight:bold;font-size:12px;padding:10px;");
                txtfname.EnableViewState = true;
                content.Controls.Add(txtfname);
                content.Controls.Add(new LiteralControl("<br/>"));
        }
        Button btnSubmit = new Button();
        btnSubmit.ID = "btnSubmit";
        btnSubmit.Text = "Submit";
        btnSubmit.Click += new System.EventHandler(btnSubmit_click);
        btnSubmit.Enabled = false;
        content.Controls.Add(btnSubmit);
}

protected void btnSubmit_click(object sender, EventArgs e)
{
    // How to find the dynamically created textbox
}

Now How to find the dynamically created controls I know the basic like:

Form.FindControl("TextBox ID");

But here i dont know the textbox id and also i even dont know how many textbox will be their as it totally depends on user input i.e. from 2 TO N textboxes What i want is on bttn_Click i will fetch the text from all the textboxes How will i achieve this. Also i want to check if all Textbox is empty or not on bttn_Click


Aucun commentaire:

Enregistrer un commentaire