Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to clear the data of content page controls which is inheriting from master page using button click event which is present on content page

i have written following code in button click .but i am not able to clear the controls date
can any body help me out in this regard

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Reuseable;
 

namespace ModelCode
{
public partial class GenerateConnectionString : System.Web.UI.Page
{
Utilities Utils = new Utilities();
protected void Page_Load(object sender, EventArgs e)
{

}
 
protected void btnGenerateConnectionString_Click(object sender, EventArgs e)
{
 
lblResult.Text = "server=" + txtServerName.Text + "; database=" + txtDatabaseName.Text + "; user id=" + txtUserId.Text + "; password=" + txtPassword.Text + ";";
lblResult.Text = Utils.EncrypString(lblResult.Text);
}
 
protected void Button1_Click(object sender, EventArgs e)
{
 
CleartextBoxes(this.Page);
//Response.Redirect("~/GenerateConnectionString.aspx");

}
public void CleartextBoxes(Control parent)
{
 
foreach (Control ctrl in parent.Controls)
{
if (ctrl is TextBox)
{
TextBox tb = (TextBox)ctrl;
tb.Text = string.Empty;
}
else if (ctrl is DropDownList)
{
DropDownList dl = (DropDownList)ctrl;
dl.SelectedIndex = 0;
}
else if (ctrl is CheckBox)
{
CheckBox cb = (CheckBox)ctrl;
cb.Checked = false;
}
}
} 

}
}
Posted
Updated 11-Dec-13 18:26pm
v4
Comments
ZurdoDev 11-Dec-13 13:15pm    
Debug it. It sounds like it isn't hitting your controls.
joginder-banger 11-Dec-13 13:30pm    
i can't under stand what you want. can you share more code for more understanding your problem

1 solution

A larger code sample would be helpful however I suspect your having scope issues. What is 'this' referring to in relation to where your code is being executed?

Try this:

C#
private void Button1_Click(object sender, System.EventArgs e)
{
    string allTextBoxValues = "";
    foreach (Control c in Page.Controls)
    {
        foreach (Control childc in c.Controls)
        {
            if (childc is TextBox)
            {
                allTextBoxValues += ((TextBox)childc).Text + ",";
            }
        }
    }
}


For more information on accessing the control collection[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900