Update Web User Control From App_Code Class

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();
}
}
 
B

bruce barker

a better approach instead of reflection is to have the control implement
a value interface. then the appcode just casts the control to the
interface and calls the properties and methods directly.


public interface IControlValue
{
string Value { get; set; }
}


implement in your user control. as the control has the code, just add
the interface to the class def.

then from app code call

string v = ((IControlValue) myData.FormControl).Value;

if you make FormControl a IControlValue you don't even need a cast.

-- bruce (sqlwork.com)

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();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top