I have a code which saves the control id and its text in database..(and it is working fine)
This code is written in default.aspx.cs which will save all the controls(of deault.aspx) id and name in database.
I want to make a class file and then call this class file in page rather than writing the code in page because there will be multiple pages where this can be called.
Please guide me to write this function DisplyDetails() in class file..
code is as below..
public partial class _Default : System.Web.UI.Page
{
string controlId, propertyValue,other;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["abc"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
DisplyDetails(this);
}
private void DisplyDetails(Control control)
{
foreach (Control controls in this.form1.Controls)
{
string controlId = controls.ID;
Response.Write(controlId);
Type controlType = controls.GetType();
PropertyInfo[] properties = controlType.GetProperties();
foreach (PropertyInfo controlProperty in properties)
{
if (controlProperty.Name == "Text" && controlProperty.PropertyType == typeof(String))
{
propertyValue = GetPropertyValue(controlProperty.Name, controls);
Response.Write(propertyValue);
//coding to save controlid and control'sname
SqlCommand cmd = new SqlCommand("insert into lang1(controlid,controlname1,controlname2) values ('" + controlId + "','" + propertyValue + "','" + other + "')", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
private string GetPropertyValue(string pName, Control control)
{
Type type = control.GetType();
string propertyName = pName;
BindingFlags flags = BindingFlags.GetProperty;
Binder binder = null;
object[] args = null;
object value = type.InvokeMember( propertyName,flags,binder,control,args);
return value.ToString();
}
}