Re: Update Web User Control From App_Code Class

Tech-Archive recommends: Speed Up your PC by fixing your registry



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

.



Relevant Pages

  • Re: Accessing value of a Variable in parent from custom control
    ... get some kind of information from the parent form in which the control ... If Form1 implements an interface, it means that you must provide the ... Implements iUsesMyControl ... Public Property FormString() As String Implements ...
    (microsoft.public.dotnet.framework)
  • Re: Accessing value of a Variable in parent from custom control
    ... problem is that the control is not awair of either the iUsesMycontrol ... get some kind of information from the parent form in which the control ... If Form1 implements an interface, it means that you must provide the ...
    (microsoft.public.dotnet.framework)
  • Re: Possible to pass strings back to ASP from ActriveX?
    ... afx_msg int AboutBox; ... helpstring("AxTemplateTest1 ActiveX Control module"), ... // Primary dispatch interface for CAxTemplateTest1Ctrl ... helpstring("Dispatch interface for AxTemplateTest1 Control")] ...
    (microsoft.public.windowsce.embedded)
  • Re: Replace a VB ocx using a new MFC ocx
    ... I am using the IDL from the VB6 control in my MFC ActiveX now. ... The interface MFC wizard created is ... I've got the VB ocx file called A.ocx, ...
    (microsoft.public.win32.programmer.ole)
  • Re: Possible to pass strings back to ASP from ActriveX?
    ... afx_msg int AboutBox; ... helpstring("AxTemplateTest1 ActiveX Control module"), ... // Primary dispatch interface for CAxTemplateTest1Ctrl ... helpstring("Dispatch interface for AxTemplateTest1 Control")] ...
    (microsoft.public.windowsce.embedded)