473,320 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Update Web User Control From App_Code Class

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

Apr 26 '07 #1
1 3580
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();
}
}
Apr 26 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Samuel | last post by:
Hi, I am building a page that makes use of user control as a templating technique. The following is that I have in mind and it is actually working: Root/ -- login.aspx -- login.aspx.vb --...
2
by: Alan Silver | last post by:
Hello, I have a user control that has one .cs file, with several .ascx files that call it. The reason for this is that I can pick whichever of the ..ascx files I want (based on a site owner...
5
by: Jay Douglas | last post by:
I have a set of pages that inherit from a base class in the App_Code folder. The class looks something like: public class MyBaseClass : System.Web.UI.Page In various stages of the life cycle I...
2
by: john | last post by:
Maybe I haven't had that "a-ha" moment yet, but I think the new approach to web projects is a step in the wrong direction. My main beef is that control over the assembly generation process has...
8
by: Umut Tezduyar | last post by:
I know that, in asp.net 2.0, the assembly for the web site is splitted into pieces and each time you build it, it generates a random name for assembly. My question is, if i create a custom web...
7
by: hummh | last post by:
Hello out there, I´m making my first steps with ASP.NET 2.0 and have he following problem: I´ve implemented a Web User Control that sits in the root of my ASP.NET Website. I want to use the...
4
by: Eric | last post by:
I got a particular problem in visual studio 2005 There's a user control on page and I want to meka a cast like this MyPage mp=(MyPage)this.Page; Error is : cannot cast from...
2
by: rn5a | last post by:
Assume that a user control (MyUC.ascx) encapsulates 2 TextBoxes with the IDs 'txt1' & 'txt2' respectively. To use this user control in an ASPX page, the following Register directive will be...
11
by: Web Search Store | last post by:
Hello, I set up a web page with 2 user controls. In classic asp, the first one did all the declarations, and the second one used the values, and could reset it. In ASP.Net so far I can't...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.