Re: Update Web User Control From App_Code Class
- From: bruce barker <nospam@xxxxxxxxxx>
- Date: Thu, 26 Apr 2007 12:46:56 -0700
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)
Jared wrote:
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();
}
}
- References:
- Update Web User Control From App_Code Class
- From: Jared
- Update Web User Control From App_Code Class
- Prev by Date: webdev.webserver fails to start
- Next by Date: Free Automated Web Site Test Tools for IE 6.0?
- Previous by thread: Update Web User Control From App_Code Class
- Next by thread: Password length minimum: 7. Non-alphanumeric characters required: 1.
- Index(es):
Relevant Pages
|