J
Jared
Hi
I have been looking for a few hours and couldn't find much in these
groups, so I thought I would post it here in case anybody else is
trying to figure it out.
I have an ascx User control with an attribute called 'Value'. I want
to set this at run time from a class with App_Code. I pass the control
to the class as a generic Control (called myData.FormControl here). I
am setting the 'Value' attribute to the string held in
row[myData.FieldName].ToString().
Type CurrentType = myData.FormControl.GetType();
System.Reflection.PropertyInfo myValue =
CurrentType.GetProperty("Value");
myValue.SetValue(myData.FormControl, row[myData.FieldName].ToString(),
null);
Hope this helps someone (and makes sense - I have posted the whole
method below to try to help clarify). There appear to be a lot of
complicated ways of achieving this, such as placing the .acsx in the
App_Code folder and writing Interfaces, but this seems to work ok.
Jared
private void DataEntryDataSet(ArrayList arrFields, string strView, int
ID)
{
string strFields = "";
foreach (csDataObject myData in arrFields)
{
strFields += myData.FieldName + ", ";
}
string strSql = "Select " + strFields;
strSql += "dteDateCreated, strUserCreated, dteDateUpdated,
strUserUpdated, bitArchived, strUserArchived, dteDateArchived,
bitDeleted, strUserDeleted, dteDateDeleted";
strSql += " from " + strView + " Where GetID = " + ID;
SqlDataAdapter adapter = new SqlDataAdapter(strSql, _conn);
try
{
_conn.Open();
adapter.Fill(_results, "Results");
foreach(DataRow row in _results.Tables["Results"].Rows)
{
foreach (csDataObject myData in arrFields)
{
switch(myData.FormControl.GetType().Name)
{
case "TextBox":
TextBox myTextBox = myData.FormControl as
TextBox;
myTextBox.Text =
row[myData.FieldName].ToString();
break;
case "DropDownList":
DropDownList myDropDownList =
myData.FormControl as DropDownList;
myDropDownList.SelectedValue =
row[myData.FieldName].ToString();
break;
case "CheckBox":
CheckBox myCheckBox = myData.FormControl
as CheckBox;
if(row[myData.FieldName].ToString() ==
"1")
{
myCheckBox.Checked = true;
}
else
{
myCheckBox.Checked = false;
}
break;
case "ctrltextboxcalendar_ascx":
Type CurrentType =
myData.FormControl.GetType();
System.Reflection.PropertyInfo myValue =
CurrentType.GetProperty("Value");
myValue.SetValue(myData.FormControl,
row[myData.FieldName].ToString(), null);
break;
}
}
}
}
catch (Exception err)
{
throw new Exception(err.Message);
}
finally
{
_conn.Close();
}
}
I have been looking for a few hours and couldn't find much in these
groups, so I thought I would post it here in case anybody else is
trying to figure it out.
I have an ascx User control with an attribute called 'Value'. I want
to set this at run time from a class with App_Code. I pass the control
to the class as a generic Control (called myData.FormControl here). I
am setting the 'Value' attribute to the string held in
row[myData.FieldName].ToString().
Type CurrentType = myData.FormControl.GetType();
System.Reflection.PropertyInfo myValue =
CurrentType.GetProperty("Value");
myValue.SetValue(myData.FormControl, row[myData.FieldName].ToString(),
null);
Hope this helps someone (and makes sense - I have posted the whole
method below to try to help clarify). There appear to be a lot of
complicated ways of achieving this, such as placing the .acsx in the
App_Code folder and writing Interfaces, but this seems to work ok.
Jared
private void DataEntryDataSet(ArrayList arrFields, string strView, int
ID)
{
string strFields = "";
foreach (csDataObject myData in arrFields)
{
strFields += myData.FieldName + ", ";
}
string strSql = "Select " + strFields;
strSql += "dteDateCreated, strUserCreated, dteDateUpdated,
strUserUpdated, bitArchived, strUserArchived, dteDateArchived,
bitDeleted, strUserDeleted, dteDateDeleted";
strSql += " from " + strView + " Where GetID = " + ID;
SqlDataAdapter adapter = new SqlDataAdapter(strSql, _conn);
try
{
_conn.Open();
adapter.Fill(_results, "Results");
foreach(DataRow row in _results.Tables["Results"].Rows)
{
foreach (csDataObject myData in arrFields)
{
switch(myData.FormControl.GetType().Name)
{
case "TextBox":
TextBox myTextBox = myData.FormControl as
TextBox;
myTextBox.Text =
row[myData.FieldName].ToString();
break;
case "DropDownList":
DropDownList myDropDownList =
myData.FormControl as DropDownList;
myDropDownList.SelectedValue =
row[myData.FieldName].ToString();
break;
case "CheckBox":
CheckBox myCheckBox = myData.FormControl
as CheckBox;
if(row[myData.FieldName].ToString() ==
"1")
{
myCheckBox.Checked = true;
}
else
{
myCheckBox.Checked = false;
}
break;
case "ctrltextboxcalendar_ascx":
Type CurrentType =
myData.FormControl.GetType();
System.Reflection.PropertyInfo myValue =
CurrentType.GetProperty("Value");
myValue.SetValue(myData.FormControl,
row[myData.FieldName].ToString(), null);
break;
}
}
}
}
catch (Exception err)
{
throw new Exception(err.Message);
}
finally
{
_conn.Close();
}
}