Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Saturday, March 31, 2012

SendingEMail

I have an ASP.NET web app that I am trying to sens an email from. I use
the code below to send it.
mailMsg := MailMessage.Create;
mailMsg.From := self.Session['SMTPADDRESS'].ToString;
mailMsg.&To := aEMail;
mailMsg.BCC := aAdminEMail;
mailMsg.Subject := 'Your LoginID and Password';
mailMsg.BodyFormat := MailFormat.Text;
mailMsg.Body := 'Your LoginID is '''+aLogin+''' and Password
'''+aPassword+'''';
SmtpMail.SmtpServer := aSMTPServer;
SmtpMail.Send(mailMsg);
My mail server requires password authentication. Where do I assign the
password?
TIAThe following solution is officially unsupported - but it works (you
otherwise need to use CDO or something else to authenticate).
Add these lines to your code before you call SmtpMail.Send()
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthe
nticate",
"1"); //basic authentication
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusern
ame",
myLoginID); //set your username here
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassw
ord",
myPassword); //set your password here
-HTH
"gh" <gh@.at.ne> wrote in message
news:OFCyfZsjFHA.3012@.TK2MSFTNGP12.phx.gbl...
>I have an ASP.NET web app that I am trying to sens an email from. I use
>the code below to send it.
> mailMsg := MailMessage.Create;
> mailMsg.From := self.Session['SMTPADDRESS'].ToString;
> mailMsg.&To := aEMail;
> mailMsg.BCC := aAdminEMail;
> mailMsg.Subject := 'Your LoginID and Password';
> mailMsg.BodyFormat := MailFormat.Text;
> mailMsg.Body := 'Your LoginID is '''+aLogin+''' and Password
> '''+aPassword+'''';
> SmtpMail.SmtpServer := aSMTPServer;
> SmtpMail.Send(mailMsg);
> My mail server requires password authentication. Where do I assign the
> password?
> TIA
Check this out
http://www.systemwebmail.com/faq/3.8.aspx
info@.donotspam dowhileloop.com
http://www.dowhileloop.com -- Website Development
http://publicjoe.dowhileloop.com -- C# & VB.NET Tutorials
"gh" <gh@.at.ne> wrote in message
news:OFCyfZsjFHA.3012@.TK2MSFTNGP12.phx.gbl...
> I have an ASP.NET web app that I am trying to sens an email from. I use
> the code below to send it.
> mailMsg := MailMessage.Create;
> mailMsg.From := self.Session['SMTPADDRESS'].ToString;
> mailMsg.&To := aEMail;
> mailMsg.BCC := aAdminEMail;
> mailMsg.Subject := 'Your LoginID and Password';
> mailMsg.BodyFormat := MailFormat.Text;
> mailMsg.Body := 'Your LoginID is '''+aLogin+''' and Password
> '''+aPassword+'''';
> SmtpMail.SmtpServer := aSMTPServer;
> SmtpMail.Send(mailMsg);
> My mail server requires password authentication. Where do I assign the
> password?
> TIA

SendMail

I am using SendMail to send email. I am using following code.

SmtpMail.SmtpServer = ConfigurationSettings.AppSettings["smtp_Server_Name"];
string strTo = ConfigurationSettings.AppSettings["contact_To"];
MailMessage msg = new MailMessage();

msg.To = strTo;
msg.From = txtFrom.Text;
msg.Subject = txtSubject.Text;
msg.Body = txtContent.Text;

lblStatus.Text = "Sending...";
SmtpMail.Send(msg);
lblStatus.Text = "Your Message send successfully.";

******************************************
It works great. But it sends email twice to the same address.

I am not sure why it is doing that?

Thanks
Jijo.Um, i got one suggestion and one comment here.

Suggestion:
Make sure your email is only entered once in your web config for ConfigurationSettings.AppSettings["contact_To"];

Comment:
Otherwise it's not a coding problem, as I am having a similar scenario. The code used to work, however i believe there has been changes recently to many servers in schemas against spam and viruses; Ever since those changes about a week (or two) ago the double recieving of emails have started for me, but only in the coding aspect. If i recieve emails from someone else its all fine.

What servers are you on ?

Sendmail config

I've been given the following template code:

// Generic method for sending emails

publicstaticvoid SendMail(string from,string to,string subject,string body)

{

// Configure mail client (may need additional// code for authenticated SMTP servers)SmtpClient mailClient =newSmtpClient

(

GuitarShackConfiguration.MailServer);// Create the mail message//MailMessage mailMessage = new MailMessage(from, to, subject, body);/*

// For SMTP servers that require authentication

message.Fields.Add

("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);

message.Fields.Add

("http://schemas.microsoft.com/cdo/configuration/sendusername",

"SmtpHostUserName");

message.Fields.Add

("http://schemas.microsoft.com/cdo/configuration/sendpassword",

"SmtpHostPassword");

*/

// Send mail//mailClient.Send(mailMessage);

}

but i can't seem to get it to work with any mailhosts. Could someone kindly show me how to make this work with someone like hotmail or gmail or even a regular pop3 mail provider?

First of all be aware that most of the mail server requires you to authenticate before sending emails. I'v attached a code where you can use to send emails below.


Dim MailObjAs New System.Net.Mail.SmtpClient MailObj.Host ="smtp server goes here"Dim credAs New Net.NetworkCredential("username","password")' authentication MailObj.Credentials = cred MailObj.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.NetworkDim msgAs New System.Net.Mail.MailMessageWith msg .Body ="Body goes here" .From =New System.Net.Mail.MailAddress("from email","from name") .To.Add("destination email address") .Subject ="subject goes here"End With MailObj.Send(msg)


Regards


First of all be aware that most of the mail server requires you to authenticate before sending emails. I'v attached a code where you can use to send emails below.


Dim MailObjAs New System.Net.Mail.SmtpClient
MailObj.Host ="smtp server goes here"
Dim credAs New Net.NetworkCredential("username","password")' authentication
MailObj.Credentials = cred
MailObj.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network
Dim msgAs New System.Net.Mail.MailMessage
With msg
.Body ="Body goes here"
.From =New System.Net.Mail.MailAddress("from email","from name")
.To.Add("destination email address")
.Subject ="subject goes here"
End With

MailObj.Send(msg)


Regards


Hi,

Any question about Sending Mail may be answered at this website:

http://www.systemnetmail.com/

Good luck!

separate .cs file for global.asax

Simple question: I have a c# asp.net project that was given to me. It has
the c# code within the <scriptsection of the global.asax file. I would
rather have it in separate files; e.g., global.asax.cs; So, I deleted the
existing global.asax file (there was nothing important in it yet), and went
to add a new one through VS2005. I chose the "Global Application Class"
(Global.asax) item from the list of new items. The "Place code in separate
file" checkbox option is DISABLED! WHY?
Thanks.Hi,

if you want to add a code-behind class for global.asax, you do it by adding
a class to App_Code (or into external class library) which derives from
HttpApplication and then associate this class with global.asax by specifying
CodeFile and Inherits attributes in @.Application directive.

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
"Larry Epn" <larryepn@.noemail.noemailwrote in message
news:4F16F9B8-3CD2-480E-8FF9-5911014EFF98@.microsoft.com...

Quote:

Originally Posted by

Simple question: I have a c# asp.net project that was given to me. It
has
the c# code within the <scriptsection of the global.asax file. I would
rather have it in separate files; e.g., global.asax.cs; So, I deleted the
existing global.asax file (there was nothing important in it yet), and
went
to add a new one through VS2005. I chose the "Global Application Class"
(Global.asax) item from the list of new items. The "Place code in
separate
file" checkbox option is DISABLED! WHY?
Thanks.


Thanks for the reply. I know this construct and have manually put this into
some files; however, I just don't know why VS2005 is being so stubborn. Is
there a project or environment configuration setting that I can use to turn
this on? The thing is that the option is ENABLED when I add a new web page,
but not when I want to add a global.asax. It makes no sense.

"Teemu Keiski" wrote:

Quote:

Originally Posted by

Hi,
>
if you want to add a code-behind class for global.asax, you do it by adding
a class to App_Code (or into external class library) which derives from
HttpApplication and then associate this class with global.asax by specifying
CodeFile and Inherits attributes in @.Application directive.
>
--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
>
"Larry Epn" <larryepn@.noemail.noemailwrote in message
news:4F16F9B8-3CD2-480E-8FF9-5911014EFF98@.microsoft.com...

Quote:

Originally Posted by

Simple question: I have a c# asp.net project that was given to me. It
has
the c# code within the <scriptsection of the global.asax file. I would
rather have it in separate files; e.g., global.asax.cs; So, I deleted the
existing global.asax file (there was nothing important in it yet), and
went
to add a new one through VS2005. I chose the "Global Application Class"
(Global.asax) item from the list of new items. The "Place code in
separate
file" checkbox option is DISABLED! WHY?
Thanks.


>
>
>


There is no setting for that. You need to set that manually

See. http://dallas.sark.com/SarkBlog/cbo...11/14/2234.aspx
and
http://groups.google.com/group/micr...9acb5c0fbd1bc54
--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
"Larry Epn" <larryepn@.noemail.noemailwrote in message
news:BDD1A75B-64E9-4BCB-BAAE-3DEDFD1A60CC@.microsoft.com...

Quote:

Originally Posted by

Thanks for the reply. I know this construct and have manually put this
into
some files; however, I just don't know why VS2005 is being so stubborn.
Is
there a project or environment configuration setting that I can use to
turn
this on? The thing is that the option is ENABLED when I add a new web
page,
but not when I want to add a global.asax. It makes no sense.
>
"Teemu Keiski" wrote:
>

Quote:

Originally Posted by

>Hi,
>>
>if you want to add a code-behind class for global.asax, you do it by
>adding
>a class to App_Code (or into external class library) which derives from
>HttpApplication and then associate this class with global.asax by
>specifying
>CodeFile and Inherits attributes in @.Application directive.
>>
>--
>Teemu Keiski
>ASP.NET MVP, AspInsider
>Finland, EU
>http://blogs.aspadvice.com/joteke
>>
>"Larry Epn" <larryepn@.noemail.noemailwrote in message
>news:4F16F9B8-3CD2-480E-8FF9-5911014EFF98@.microsoft.com...

Quote:

Originally Posted by

Simple question: I have a c# asp.net project that was given to me. It
has
the c# code within the <scriptsection of the global.asax file. I
would
rather have it in separate files; e.g., global.asax.cs; So, I deleted
the
existing global.asax file (there was nothing important in it yet), and
went
to add a new one through VS2005. I chose the "Global Application
Class"
(Global.asax) item from the list of new items. The "Place code in
separate
file" checkbox option is DISABLED! WHY?
Thanks.


>>
>>
>>


Hi Larry,

Besides Teemu's input, you can also find related feedback here:
https://connect.microsoft.com/Visua...ack.aspx?Feedba
ckID=104811. According to Visual Studio Product team, it will be considered
in future version of Visual Studio. Thank you for your understanding.

Sincerely,
Walter Wang (wawang@.online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscript...ault.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscript...rt/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Separate by comma

Hi,

I have a datalist on my page to select data from a table

I want to access it with code to insert its values to another table. Te datalist will return more than one row, so i'd like the values seperated by commas when they are inserted to the new table (all in one row).

I have looked around and it seems that I need to do something to the select statement of the other control first - I have got:

SELECT [Usersname]= COALESCE (Usersname + ' , ' , ' ')
FROM [Transactions] WHERE ([Itemid] = @dotnet.itags.org.Itemid)

Does that look right?

And when I have selected these values, how do I insert them in the same format (separated by commas) ?:

DataView dv = SqlDataSource2.Select(DataSourceSelectArguments.Empty) as DataView;
string receivername = dv[0]["receivername"].ToString();
command.Parameters.AddWithValue("@dotnet.itags.org.recievername", receivername);

Would that still work?

Thanks,

Jon

pls help!!


You can pass the value (comma separated value) to a stored procedure as a parameter.

Within the procedure you can insert the data into the table. Before inserting the data into the table we have to split it by comma and then we have to place in a temporary table.

Now we have to extract the data from temp table to actual table.

There is a function called "split" is available to split the data and it will return the data as a table. You can use the same function to insert into your table.

You can get this function at of this URL:http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648

http://vadivel.blogspot.com/2006/05/split-function-in-sql-server-method-1.html

Hope this helps you.

Separate code file

Hi

I have a button on a webform. When I double click the button to create click
event it is created in the same file (aspx). How can I tell vs2005 to create
the code in a separate aspx.vb file?

Thanks

RegardsOn Feb 19, 4:54 am, "John" <J...@.nospam.infovis.co.ukwrote:

Quote:

Originally Posted by

Hi
>
I have a button on a webform. When I double click the button to create click
event it is created in the same file (aspx). How can I tell vs2005 to create
the code in a separate aspx.vb file?
>
Thanks
>
Regards


You enable the ASP .NET code-behind feature by adding attributes to
the @.Page directive.

for VB

<%@. Page Language="VB" Inherits="myClass" CodeBehind="myPage.aspx.vb"
%>

for C#

<%@. Page Language="C#" Inherits="myClass" CodeBehind="myPage.aspx.cs"
%>
Does that mean that it is not something one should normally be doing
otherwise it would have been provided by default?

Thanks

Regards

"Alexey Smirnov" <alexey.smirnov@.gmail.comwrote in message
news:1171872789.521209.303890@.v45g2000cwv.googlegr oups.com...

Quote:

Originally Posted by

On Feb 19, 4:54 am, "John" <J...@.nospam.infovis.co.ukwrote:

Quote:

Originally Posted by

>Hi
>>
>I have a button on a webform. When I double click the button to create
>click
>event it is created in the same file (aspx). How can I tell vs2005 to
>create
>the code in a separate aspx.vb file?
>>
>Thanks
>>
>Regards


>
You enable the ASP .NET code-behind feature by adding attributes to
the @.Page directive.
>
for VB
>
<%@. Page Language="VB" Inherits="myClass" CodeBehind="myPage.aspx.vb"
%>
>
for C#
>
<%@. Page Language="C#" Inherits="myClass" CodeBehind="myPage.aspx.cs"
%>
>


On Feb 19, 3:56 pm, "John" <J...@.nospam.infovis.co.ukwrote:

Quote:

Originally Posted by

Does that mean that it is not something one should normally be doing
otherwise it would have been provided by default?
>


VS creates that by default, when you add a new WebForm into project.
How do you created it?
Hi

I suspect I copied from another predone webform. I have now added this line
at the top;

<%@. Page Language="VB" MasterPageFile="MyMaster.master"
AutoEventWireup="false" CodeFile="MyPage.aspx.vb"
Inherits="Events_Settings_MyPage" Title="My Title" %>

and it says 'file '/Events/Settings/MyPage.aspx.vb' does not exist.' which
of course is true as the page did not have code behind to start with. How
can I now get vs to create the MyPage.aspx.vb file and link to the webform?

Thanks

Regards

"Alexey Smirnov" <alexey.smirnov@.gmail.comwrote in message
news:1171897535.364021.46260@.l53g2000cwa.googlegro ups.com...

Quote:

Originally Posted by

On Feb 19, 3:56 pm, "John" <J...@.nospam.infovis.co.ukwrote:

Quote:

Originally Posted by

>Does that mean that it is not something one should normally be doing
>otherwise it would have been provided by default?
>>


>
VS creates that by default, when you add a new WebForm into project.
How do you created it?
>


On Feb 19, 5:25 pm, "John" <J...@.nospam.infovis.co.ukwrote:

Quote:

Originally Posted by

Hi
>
I suspect I copied from another predone webform. I have now added this line
at the top;
>


Well, I think the best way is to add a new webform to your project. VS
will create aspx and the one for code-behind. Then you can copy your
code to this new webform, or use created code-behind *.vb as an
example.

Separate code file

Hi
I have a button on a webform. When I double click the button to create click
event it is created in the same file (aspx). How can I tell vs2005 to create
the code in a separate aspx.vb file?
Thanks
RegardsOn Feb 19, 4:54 am, "John" <J...@.nospam.infovis.co.uk> wrote:
> Hi
> I have a button on a webform. When I double click the button to create cli
ck
> event it is created in the same file (aspx). How can I tell vs2005 to crea
te
> the code in a separate aspx.vb file?
> Thanks
> Regards
You enable the ASP .NET code-behind feature by adding attributes to
the @.Page directive.
for VB
<%@. Page Language="VB" Inherits="myClass" CodeBehind="myPage.aspx.vb"
%>
for C#
<%@. Page Language="C#" Inherits="myClass" CodeBehind="myPage.aspx.cs"
%>
Does that mean that it is not something one should normally be doing
otherwise it would have been provided by default?
Thanks
Regards
"Alexey Smirnov" <alexey.smirnov@.gmail.com> wrote in message
news:1171872789.521209.303890@.v45g2000cwv.googlegroups.com...
> On Feb 19, 4:54 am, "John" <J...@.nospam.infovis.co.uk> wrote:
> You enable the ASP .NET code-behind feature by adding attributes to
> the @.Page directive.
> for VB
> <%@. Page Language="VB" Inherits="myClass" CodeBehind="myPage.aspx.vb"
> %>
> for C#
> <%@. Page Language="C#" Inherits="myClass" CodeBehind="myPage.aspx.cs"
> %>
>
On Feb 19, 3:56 pm, "John" <J...@.nospam.infovis.co.uk> wrote:
> Does that mean that it is not something one should normally be doing
> otherwise it would have been provided by default?
>
VS creates that by default, when you add a new WebForm into project.
How do you created it?
Hi
I suspect I copied from another predone webform. I have now added this line
at the top;
<%@. Page Language="VB" MasterPageFile="MyMaster.master"
AutoEventWireup="false" CodeFile="MyPage.aspx.vb"
Inherits="Events_Settings_MyPage" Title="My Title" %>
and it says 'file '/Events/Settings/MyPage.aspx.vb' does not exist.' which
of course is true as the page did not have code behind to start with. How
can I now get vs to create the MyPage.aspx.vb file and link to the webform?
Thanks
Regards
"Alexey Smirnov" <alexey.smirnov@.gmail.com> wrote in message
news:1171897535.364021.46260@.l53g2000cwa.googlegroups.com...
> On Feb 19, 3:56 pm, "John" <J...@.nospam.infovis.co.uk> wrote:
> VS creates that by default, when you add a new WebForm into project.
> How do you created it?
>
On Feb 19, 5:25 pm, "John" <J...@.nospam.infovis.co.uk> wrote:
> Hi
> I suspect I copied from another predone webform. I have now added this lin
e
> at the top;
>
Well, I think the best way is to add a new webform to your project. VS
will create aspx and the one for code-behind. Then you can copy your
code to this new webform, or use created code-behind *.vb as an
example.

Separate data by comma

Hi,

I have a datalist on my page and I want to access it with code to insert its values, seperated by commas.

I have looked around and it seems that I need to do something to the select statement of the other control first - I have got:

SELECT [Usersname]= COALESCE (Usersname + ' , ' , ' ')
FROM [Transactions] WHERE ([Itemid] = @dotnet.itags.org.Itemid)

Does that look right?

And when I have selected these values, how do I insert them in the same format (separated by commas) ?:

DataView dv = SqlDataSource2.Select(DataSourceSelectArguments.Empty) as DataView;
string receivername = dv[0]["receivername"].ToString();
command.Parameters.AddWithValue("@dotnet.itags.org.recievername", receivername);

Would that still work?

Thanks,

Jon

pls help!


If I m not wrong, I understand from your question that you need to enter multiple rows in database.

You can refer

http://zirmandli.wordpress.com/2007/09/22/data-access-application-block-gets-batch-update-functionality-in-enterprise-library-30/

I hope this will help u....


Hi,

No, I didnt explain well enough. I will repost.

Thanks anyway

Jon

separate HTML from codebehind for user control page

I have a site that uses header.ascx as a user control for the Top Menu. I'm trying (to do it the right way) to separate the HTML code from the codebehind but I'm having a difficult time w/ it... I looked at some articles on codebehinds but most of them are event driven like if you click on a button and some event happens... but for my page, I just want to display a menu at a specific location when the form loads... I guess I have to put it in a function and then call it from my main page? But I'm not sure how??
============================
header.ascx
============================
<%@dotnet.itags.org. Control Language="vb" AutoEventWireup="false" Codebehind="header.ascx.vb" Inherits="intranet.header" %>

============================
header.ascx.vb
============================
PublicClass nav_header
Inherits System.Web.UI.UserControl
Public strPageTitleAsString
Public strTopMenu1AsString

PrivateSub Page_Load(ByVal senderAs System.Object,ByVal eAs System.EventArgs)HandlesMyBase.Load

Response.Write("<title>" & strPageTitle & "</title>")
Response.Write("</head><body>")
Response.Write("<table><tr><td>test...</td></tr>")
Response.Write("<tr><td><table><tr>")

Dim strTopMenu2()AsString
SelectCase strTopMenu1

Case "Header 1"
strTopMenu=NewString() {"Corporate_CatHeader1", "../Corporate/header1.aspx", "Corporate_CatHeader2", "../Corporate/header2.aspx"}
Case "Header 2"
strTopMenu =NewString() {"Practices_CatHeader1", "../Practices/header1.aspx", "Practices_CatHeader2", "../Practices/header2.aspx"}
Case "Header 3"
strTopMenu =NewString() {"WebMail_CatHeader1", "../WebMail/header1.aspx", "WebMail_CatHeader2", "../WebMail/header2.aspx"}
CaseElse' no menu requested, so include a blank spacer

ExitSub
EndSelect

Dim intTopMenu2ItemAsInteger
Dim intSelectedTopMenu1AsInteger
Dim iAsInteger

intSelectedTopMenu1 = (intTopMenu2Item - 1) * 2
For intTopMenu2Item = 0To UBound(strTopMenu2)Step 2
Response.Write("<td> <A href='" & strTopMenu2(intTopMenu2Item + 1) & "'>" & strTopMenu2(intTopMenu2Item) & "</a>")
Response.Write(" | </td>" & vbCrLf)

Next

Response.Write("</tr></table>")
Response.Write("</td></tr></table>")

EndSub

EndClass

In the inherits atribute of your page ensure it matches your class name. In this case your class name is nav_header but the inherits is intranet.header. For starters if you are not using Visual Studio then the intranet part is not necessary unless you wish to encompass the class in a namespace. The quick solution is to change inherits to header and the class name to header.
Sorry I mistyped some of the names... here's a short version.. please help..
===============
header.ascx
===============
<%@. Control Language="vb" AutoEventWireup="false" Codebehind="header.ascx.vb" Inherits="myapp.header" %>
<table><tr>
<td>
How can I display the test() here??
</td>
</tr></table>
===============
header.ascx.vb
===============
Public Class header
Inherits System.Web.UI.UserControl
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub test()
Response.Write("test")
End Sub
End Class
Have you tried inherits="header" instead of myapp.header?

separate files

I am new to ASP.NET and VB.Net
I have a few questions about putting code into separate files.

First, can someone define codebehind? wierd. I have a CS degree, but
I have never heard this term before.

Also, what is the most effective way of storing classes and functions in a separate,
reuseable file?

I tried an ascx file, but I came upon a problem:
i have my index.aspx page, filea.ascx, and fileb.ascx. I'm basically using
filea, and fileb to hold functions. now i need a function in filea to use
a function in fileb. how do i do that? i can't seem to be able to do it, since
I need to create an instance of the usercontrol before i can use it, but
I can't create a user control inside of an ascx file.A codebehind file is a file that contains all the code (methods, event handlers, variables, etc.) for a particular UI page (.aspx or .ascx). If you've done any windows forms programming you will understand this concept better: each page has its own codebehind file to handle things like button-presses. It's the same for asp.net programming: any time you want something to "happen" on a page in your application, the page simply calls a postback to the server, and usually some code in the codebehind file is executed (like when somebody presses a button on a web page).

This is one of the huge advantages of using asp.net over asp - you can now write a web application the same way you would write a windows application, in whatever language you choose (as long as you have a .net compliant compiler for that language) since now all the processing happens server-side instead of depending on client-script capabilities in each user's browser. Just wire-up the event handlers for buttons and the like in your codebehind file and it's all executed on the server.

If I've thoroughly confused you, go to this page to understand more about the web forms code model:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconwebformscodemodel.asp

If you want to create reusable code that you can call from anywhere in your application, I would suggest creating a separate Class Library project (.dll). Put all of your classes, static methods, and whatever else you want in a file (or multiple files) in the project. You'll just want to make sure they are all declared within the same namespace. Then compile this project. You will end up with a .dll file that now encapsulates all the classes and methods you defined in the project.

In the project where you want all this functionality exposed, just add a reference to the .dll you created. Now all those classes are exposed to your project.

Hope this helps!

Michael Ferguson
Student Ambassador to Microsoft
thank you, mpherg

The stuff about the dll files. can you point me towards any examples?
How do i reference it? is the file compiled?
You reference it something like this...

first i create a vb-source file.

Namespace [Namespacename]
Public Class [Classname]
Public Function myFunction()
' code goes here
End Function
End Class
End Namespace

now we can reference myFunction from aspx-files...

Dim Testing As [Namespacename].[Classname] = New [Namespacename].[Classname]()
' now i call my function in the vb.class
Testing.myFunction()

Hope this makes it more clear.. if not you can try to download the starterkits from
http://www.asp.net/Default.aspx?tabindex=9&tabid=47
do i need to do something else? like compile the code?
i get this error message:

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30002: Type 'mynamespace.test' is not defined.
you need to cimpile to get the dll.

visual studio will do that for you...
I opened up my test.vb file with visual studio, but i see no "build" option.

is this doable without visual studio?
There is a menu called "Build". Select "Build Solution" under that menu.
OK, How do I do this WITHOUT having to compile it? My web service doesn't allow for compiled dll's. I need to reference other modules from with my page class but when I add the namespace the page class still cannot find this module. Any ideas?

Separate dynamic controls with a "<br>"

I'm adding controls to my page in code and needed <br> between the controls to put them on new lines. I just want to write <br>'s to the page without using the HtmlAnchor control. Is there a better way than this?

Label l1 = new Label();
Label l2 = new Label();
Label l3 = new Label();

HtmlAnchor an1 = new HtmlAnchor();
HtmlAnchor an2 = new HtmlAnchor();

l1.Text = "Label1";
l2.Text = "Label2";
l3.Text = "Label3";

an1.InnerHtml = an2.InnerHtml = "<br>";

Page.Controls.Add(l1);
Page.Controls.Add(an1);
Page.Controls.Add(l2);
Page.Controls.Add(an2);
Page.Controls.Add(l3);You could encapsulate the controls inside User Controls, and simply have a <br> appended in the HTML there.
You could encapsulate the controls inside User Controls, and simply have a <br> appended in the HTML there.
OK now I have the controls in a user control so how can I append the HTML? I still couldn't think of a better way than what I already have.
Just right click and go into the HTML Source for the user control, and put <br> at the bottom of it :)
But the <br> needs to be loaded dynamically between every element inside the user control. I thought I could just use some sort of HTML writer to write between the elements on the page.
Create a Generic Html Control HtmlControl namespace set it up as a <br> and add it between the controls.

If it needs to be added for every added control override the add/added control member and do it there.
Take the time to scroll down your toolbox until you find the Literal Control. That control is rarely seen because you normally have to scroll down to it. You see, it literally outputs what you put into it. So, you can put html into it, and it spits it out into the response stream just as you had put it in the .Text property. This is useful if you want to display html content from a database, or do exactly what you want to do. Example:

Label l1 = new Label();
Label l2 = new Label();
Label l3 = new Label();

LiteralControl lc1= new LiteralControl ();
LiteralControl lc2= new LiteralControl ();

l1.Text = "Label1";
l2.Text = "Label2";
l3.Text = "Label3";

lc1.Text= lc2.Text= "<br>";

Page.Controls.Add(l1);
Page.Controls.Add(lc1);
Page.Controls.Add(l2);
Page.Controls.Add(lc2);
Page.Controls.Add(l3);

Separated DLLs for user controls

Hi,

By default VS.NET generates just one DLL contains all code behinds and
userconrols.
Is it possible to have VS.NET partitione them in their own assemblies for
granular security?

Thanks,
AliYes, you can do it a few ways

1. grab the control inside the current project and compile it using the
command line compiler, specifying the \out parameter
2. Create the control in its own project then reference it back into the
original project

"A.M" <IHateSpam@.sapm123.com> wrote in message
news:%23w0S0kZ9DHA.2524@.TK2MSFTNGP11.phx.gbl...
> Hi,
> By default VS.NET generates just one DLL contains all code behinds and
> userconrols.
> Is it possible to have VS.NET partitione them in their own assemblies for
> granular security?
> Thanks,
> Ali
I think 1 project = 1 DLL. Place them in separate projects if you want
multiple dll's.

"A.M" <IHateSpam@.sapm123.com> wrote in message
news:%23w0S0kZ9DHA.2524@.TK2MSFTNGP11.phx.gbl...
> Hi,
> By default VS.NET generates just one DLL contains all code behinds and
> userconrols.
> Is it possible to have VS.NET partitione them in their own assemblies for
> granular security?
> Thanks,
> Ali
Hi Ali,

Thanks for posting in the community!
Based on my understanding, you're wondering whether its ok to make all the
compoenent classes such as page class , usercontrol classes and other
components classes separately in different dll assemblies and which are
used in the same web application , yes?

I've review the other messages and I think A Lonely Programmer has provided
the informative suggestion, you can manually compile some of the components
classes in different dlls or even make some certain separate projects for
each dll. Then reference them in the main ASP.NET web application project.
For example:
Project A:
make page classes in dllA

ProjectB:
make Control classes in dllB

ProjectC
make other non-UI components in dllC

Main ASP.NET Project?
add all the above assebmlies as referenced and use
the classes in them.

How do you think so. Please check out those suggestions, if you have any
further questions, please feel free to post here.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Thursday, March 29, 2012

Separate vb page?

Question, in two parts, I have separated my vb code from the asp page. But, I have noticed on many code samples this is not 'normally' being done. I have also noticed that it is easier to reference controls if you write your scripts on an asp page. If you separate your vb code from an asp page what is the best way to reference your controls and their properties? Also, What is considered 'best practice'? Should you separate them or not?

Thanks in advanceMarvin

As far as using code behind, I don't think that it is a best practice but a personal choice. I personally like the code behind because it keeps all my code in one place and easier for me to referrance. When I need to add an even or sub, I just double click on either the object or page depending on what I want to do.

OK... what you want to be doing is usingCodeBehind. You can select this as an option when you create your page.

Whether you use codebehind or have it on the page will makeno difference with regards to accessing your controls and their properties.

Many samples don't use code behind because it ismuch easier to post a sample that novice users can just paste into a page when you include the server-side script in the page.


Thank you both for your replies.

James, forgive me I am still new to all of this asp.net. When you say 'codebehind' do you mean... separating the vb from the ASP page? I am assuming this is correct. right?


Codebehind separates the markup (asp to you) and the source code (vb/c#) but maintains a link between them.

See this...http://support.microsoft.com/kb/303247

Separated DLLs for user controls

Hi,

By default VS.NET generates just one DLL contains all code behinds and
userconrols.
Is it possible to have VS.NET partitione them in their own assemblies for
granular security?

Thanks,
AliYes, you can do it a few ways

1. grab the control inside the current project and compile it using the
command line compiler, specifying the \out parameter
2. Create the control in its own project then reference it back into the
original project

"A.M" <IHateSpam@.sapm123.com> wrote in message
news:%23w0S0kZ9DHA.2524@.TK2MSFTNGP11.phx.gbl...
> Hi,
> By default VS.NET generates just one DLL contains all code behinds and
> userconrols.
> Is it possible to have VS.NET partitione them in their own assemblies for
> granular security?
> Thanks,
> Ali
I think 1 project = 1 DLL. Place them in separate projects if you want
multiple dll's.

"A.M" <IHateSpam@.sapm123.com> wrote in message
news:%23w0S0kZ9DHA.2524@.TK2MSFTNGP11.phx.gbl...
> Hi,
> By default VS.NET generates just one DLL contains all code behinds and
> userconrols.
> Is it possible to have VS.NET partitione them in their own assemblies for
> granular security?
> Thanks,
> Ali
Hi Ali,

Thanks for posting in the community!
Based on my understanding, you're wondering whether its ok to make all the
compoenent classes such as page class , usercontrol classes and other
components classes separately in different dll assemblies and which are
used in the same web application , yes?

I've review the other messages and I think A Lonely Programmer has provided
the informative suggestion, you can manually compile some of the components
classes in different dlls or even make some certain separate projects for
each dll. Then reference them in the main ASP.NET web application project.
For example:
Project A:
make page classes in dllA

ProjectB:
make Control classes in dllB

ProjectC
make other non-UI components in dllC

Main ASP.NET Project?
add all the above assebmlies as referenced and use
the classes in them.

How do you think so. Please check out those suggestions, if you have any
further questions, please feel free to post here.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Separating code from design

I want to do this.

I want my programmers to do all the code. All of it - run at server and run
at client.

I then want a graphic artist to make the look and the layout of the pages.

The graphic artists cannot use VS to edit the files. They will use any HTML
tool of their choice.

The files that they edit must not contain any developer code. That is the two
must be independent files so that at a later date a graphic artist can
redesign the site without a developer, or any developer tools.

And the opposite, developers must be able to change the logic (but not what
controls exist, or add new ones) without modifying the designer files, or
involving a designer.

It appears that this is not possible in ASP.net. I know about code behind,
but its my understanding that client side JS and other aspects would still
end up in the files the desinger would need.

a) Is my understanding correct.

b) Is there a way to do this is in ASP.net now? If not how about in ASP.net
2?

c) What is the guessimated time of release for ASP.net 2?

--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

ELKNews - Get your free copy at http://www.atozedsoftware.comHi Chad,

> a) Is my understanding correct.

Sort of. Your problems are not what you think they are. They are more along
the lines of the fact that this is not a passive web site, but a web
application. An HTML document is static; a web application Page is dynamic.
It is not layout, but uses layout to present the user with an interactive,
dynamic User Interface. Therefore, you can't simply separate the HTML from
the code. The code CREATES the HTML.

> b) Is there a way to do this is in ASP.net now? If not how about in
ASP.net 2?

Again, sort of. You can do a number of things which will effectively
separate MOST of your logic from your layout, particularly if you use CSS to
manage your layout. See http://csszengarden.com for examples of CSS handling
almost all the layout characteristics of a web page. However, again, we're
talking about an application which MUST generate HTML for the user. So, your
interface can't be entirely developed by one group or the other. On the
other hand, if you go that route (correctly), the graphic designers will
probably only have to ever change the CSS files, and not touch the code.

> c) What is the guessimated time of release for ASP.net 2?

I don't know, but don't expect it to solve your problem. This is simply an
inherent quality of dynamic web applications.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Chad Z. Hower aka Kudzu" <cpub@.hower.org> wrote in message
news:Xns947AAAD5C090cpub@.127.0.0.1...
> I want to do this.
> I want my programmers to do all the code. All of it - run at server and
run
> at client.
> I then want a graphic artist to make the look and the layout of the pages.
> The graphic artists cannot use VS to edit the files. They will use any
HTML
> tool of their choice.
> The files that they edit must not contain any developer code. That is the
two
> must be independent files so that at a later date a graphic artist can
> redesign the site without a developer, or any developer tools.
> And the opposite, developers must be able to change the logic (but not
what
> controls exist, or add new ones) without modifying the designer files, or
> involving a designer.
> It appears that this is not possible in ASP.net. I know about code behind,
> but its my understanding that client side JS and other aspects would still
> end up in the files the desinger would need.
> a) Is my understanding correct.
> b) Is there a way to do this is in ASP.net now? If not how about in
ASP.net
> 2?
> c) What is the guessimated time of release for ASP.net 2?
>
> --
> Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
> "Programming is an art form that fights back"
>
> ELKNews - Get your free copy at http://www.atozedsoftware.com
It sounds like you have a good understanding, so what's the problem? You'll
never be able to separate code from design totally.

I think you have to understand that separating code from design starts with
the design of your application, and the skills of everyone on your team. It
just takes good project management skills. You also have to realize many
people have hybrid skills. I use Photoshop and Flash as much as I use
VS.NET. When I have trouble with my code, I'll go to a senior programmer for
help.

In VS.NET you can have the graphic artists give you the PSD's or the whole
page in HTML from which the programmers can use as a reference when they
program the controls that render the pages.

-Max

"Chad Z. Hower aka Kudzu" <cpub@.hower.org> wrote in message
news:Xns947AAAD5C090cpub@.127.0.0.1...
> I want to do this.
> I want my programmers to do all the code. All of it - run at server and
run
> at client.
> I then want a graphic artist to make the look and the layout of the pages.
> The graphic artists cannot use VS to edit the files. They will use any
HTML
> tool of their choice.
"Max" <maximus@.portvista.com> wrote in
news:MphQb.101367$I05.1911965@.twister.tampabay.rr. com:
> It sounds like you have a good understanding, so what's the problem?
> You'll never be able to separate code from design totally.

I can do all, but my graphic talents are not good. Technical ability does
not equate to talent. And in many cases does not.

Most programmers are not good cross overs and produce layouts and designs
that they are very proud of but make the rest of the world go ack!

And most graphics types often dabble in scripting languages and do ok on
very simple stuff. They then gain enough confidence to venture deeper and
in most (not all cases) produce spaghetti code that a programmer has to
then come in and clean up (or more often is the case rewrite it).

If there could be true separation, or a least a very high degree then you
can have the best of both worlds. Sure Ive seen small companies where the
janitor might also be an accountant - but is that really what we want?

I see it that the tools are forcing, or more or less "pushing" programmers
to be designers and vice versa. That is - we are working around the tool
constraints, not because its the best practice.

My question was based on how we would like to do our development and making
sure that my assesment of how ASP.net can do it and to what degree were
accurate. We are working on something for ASP.net that will quite likely
increase the separation very very close to the degree I described, to the
point that it is very practicable as such.

I would love for nothing more than to be able to build a complete
functional application and afterward pass it to a in house or contracted
out web design team and let them use their tools without ever having to
touch mine. And after have it integrated without me or my team needing to
"push" or cram things back into our system.

Then later we can change at whim, either the design, or the code without
the need to bother the other side or be dependent on them.

> I think you have to understand that separating code from design starts
> with the design of your application, and the skills of everyone on your

I do understand that - but design is limited or made more difficult, or
easier by the limitations and capabilities of the framework and tools being
used.

> team. It just takes good project management skills. You also have to
> realize many people have hybrid skills. I use Photoshop and Flash as

I dont want Janitor/Accountant/Dental surgeons.

> In VS.NET you can have the graphic artists give you the PSD's or the
> whole page in HTML from which the programmers can use as a reference
> when they program the controls that render the pages.

That creates a liability, the programmers have to push it into their design
and work around it. Often breaking it and making it harder to update later.

--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

ELKNews - Get your free copy at http://www.atozedsoftware.com
"Kevin Spencer" <kevin@.takempis.com> wrote in
news:ON9WlFg4DHA.2888@.tk2msftngp13.phx.gbl:
> Sort of. Your problems are not what you think they are. They are more
> along the lines of the fact that this is not a passive web site, but a

Actualy it is - the layout and static parts are exactly that. Passive. As an
application developer I am controlling only the dynamic parts.

> Again, sort of. You can do a number of things which will effectively
> separate MOST of your logic from your layout, particularly if you use
> CSS to manage your layout. See http://csszengarden.com for examples of
> CSS handling almost all the layout characteristics of a web page.

Ive been there before. I didnt look at the sources of these particular ones,
but I assume they are like the others and rely on XSLT transformations? ie
thus eliminating the tools preferred by the graphic artists.

> I don't know, but don't expect it to solve your problem. This is simply
> an inherent quality of dynamic web applications.

Inherent quality of limitation we treat as a inherent quality?

--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

ELKNews - Get your free copy at http://www.atozedsoftware.com
Well, here's one thought. I've never done this but I've read that it can be
done.
If you make your entire site a Flash app, the Flash folks can call some .Net
Components (Web services usually) and handle getting data. You take a
performance hit with Web services, but you get the 100% separation you are
looking for.

Take a look at this:
http://www.dotnetjunkies.com/HowTo/...2D2E537421.dcik

HTH,
Michael

"Chad Z. Hower aka Kudzu" <cpub@.hower.org> wrote in message
news:Xns947A151E580Dcpub@.127.0.0.1...
> "Max" <maximus@.portvista.com> wrote in
> news:MphQb.101367$I05.1911965@.twister.tampabay.rr. com:
> > It sounds like you have a good understanding, so what's the problem?
> > You'll never be able to separate code from design totally.
> I can do all, but my graphic talents are not good. Technical ability does
> not equate to talent. And in many cases does not.
> Most programmers are not good cross overs and produce layouts and designs
> that they are very proud of but make the rest of the world go ack!
> And most graphics types often dabble in scripting languages and do ok on
> very simple stuff. They then gain enough confidence to venture deeper and
> in most (not all cases) produce spaghetti code that a programmer has to
> then come in and clean up (or more often is the case rewrite it).
> If there could be true separation, or a least a very high degree then you
> can have the best of both worlds. Sure Ive seen small companies where the
> janitor might also be an accountant - but is that really what we want?
> I see it that the tools are forcing, or more or less "pushing" programmers
> to be designers and vice versa. That is - we are working around the tool
> constraints, not because its the best practice.
> My question was based on how we would like to do our development and
making
> sure that my assesment of how ASP.net can do it and to what degree were
> accurate. We are working on something for ASP.net that will quite likely
> increase the separation very very close to the degree I described, to the
> point that it is very practicable as such.
> I would love for nothing more than to be able to build a complete
> functional application and afterward pass it to a in house or contracted
> out web design team and let them use their tools without ever having to
> touch mine. And after have it integrated without me or my team needing to
> "push" or cram things back into our system.
> Then later we can change at whim, either the design, or the code without
> the need to bother the other side or be dependent on them.
> > I think you have to understand that separating code from design starts
> > with the design of your application, and the skills of everyone on your
> I do understand that - but design is limited or made more difficult, or
> easier by the limitations and capabilities of the framework and tools
being
> used.
> > team. It just takes good project management skills. You also have to
> > realize many people have hybrid skills. I use Photoshop and Flash as
> I dont want Janitor/Accountant/Dental surgeons.
> > In VS.NET you can have the graphic artists give you the PSD's or the
> > whole page in HTML from which the programmers can use as a reference
> > when they program the controls that render the pages.
> That creates a liability, the programmers have to push it into their
design
> and work around it. Often breaking it and making it harder to update
later.
>
> --
> Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
> "Programming is an art form that fights back"
>
> ELKNews - Get your free copy at http://www.atozedsoftware.com

--
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.564 / Virus Database: 356 - Release Date: 1/19/2004
"Michael Pearson" <michaelp_extrajunktoremove@.televox.com> wrote in
news:eoTU9Yh4DHA.504@.TK2MSFTNGP11.phx.gbl:
> Well, here's one thought. I've never done this but I've read that it
> can be done.
> If you make your entire site a Flash app, the Flash folks can call some
> .Net Components (Web services usually) and handle getting data. You

Thanks for the ideas. But this one wont work either. Need to stick to HTML
and JS. And unless the artists are just going to make nice flash, it then
requires programming which puts the mixture element back. :)

Flash can separate it - but the UI is still being programmed. All they've
done is split it into a tier and turn the web brower into a local executable.
ActiveX + SOAP or the like.

That is - you've seprated the logic from UI, but not the aesthetic from
source code. This can be done with ASP.net now by just using business
objects.

> Take a look at this:
http://www.dotnetjunkies.com/HowTo/...2D2E537421.dcik

--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

ELKNews - Get your free copy at http://www.atozedsoftware.com

Separating html from code

If I have a form which I only want to display if a certain variable in
Test.aspx.vb becomes True, how can I do that without using the old method
inside the Test.aspx code:

<%
If bolVar = True then
%>
<form id=myFrm name=myFrm action='<%=strTargetPage%>'>
</form>
<%
End If
%Why not do this in the button_click event? I mean leave the action alone, do
your work in the event upon postback and rediect/pass the info acordingly.

--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com

"Manuel" <My@.NoMailLand.com> wrote in message
news:ZKidnUMSCYGVEDbcRVn-tQ@.giganews.com...
> If I have a form which I only want to display if a certain variable in
> Test.aspx.vb becomes True, how can I do that without using the old method
> inside the Test.aspx code:
> <%
> If bolVar = True then
> %>
> <form id=myFrm name=myFrm action='<%=strTargetPage%>'>
> </form>
> <%
> End If
> %
It never occurred to me :p

I'm going to try it now, thx.

"Curt_C [MVP]" <software_AT_darkfalz.com> wrote in message
news:%23tC8rdl1EHA.2068@.TK2MSFTNGP10.phx.gbl...
> Why not do this in the button_click event? I mean leave the action alone,
> do your work in the event upon postback and rediect/pass the info
> acordingly.
> --
> Curt Christianson
> Owner/Lead Developer, DF-Software
> Site: http://www.Darkfalz.com
> Blog: http://blog.Darkfalz.com
>
> "Manuel" <My@.NoMailLand.com> wrote in message
> news:ZKidnUMSCYGVEDbcRVn-tQ@.giganews.com...
>> If I have a form which I only want to display if a certain variable in
>> Test.aspx.vb becomes True, how can I do that without using the old method
>> inside the Test.aspx code:
>>
>> <%
>> If bolVar = True then
>> %>
>> <form id=myFrm name=myFrm action='<%=strTargetPage%>'>
>> </form>
>> <%
>> End If
>> %>
>>

Separating html from code

If I have a form which I only want to display if a certain variable in
Test.aspx.vb becomes True, how can I do that without using the old method
inside the Test.aspx code:
<%
If bolVar = True then
%>
<form id=myFrm name=myFrm action='<%=strTargetPage%>'>
</form>
<%
End If
%>Why not do this in the button_click event? I mean leave the action alone, do
your work in the event upon postback and rediect/pass the info acordingly.
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com
"Manuel" <My@.NoMailLand.com> wrote in message
news:ZKidnUMSCYGVEDbcRVn-tQ@.giganews.com...
> If I have a form which I only want to display if a certain variable in
> Test.aspx.vb becomes True, how can I do that without using the old method
> inside the Test.aspx code:
> <%
> If bolVar = True then
> %>
> <form id=myFrm name=myFrm action='<%=strTargetPage%>'>
> </form>
> <%
> End If
> %>
>
It never occurred to me :p
I'm going to try it now, thx.
"Curt_C [MVP]" <software_AT_darkfalz.com> wrote in message
news:%23tC8rdl1EHA.2068@.TK2MSFTNGP10.phx.gbl...
> Why not do this in the button_click event? I mean leave the action alone,
> do your work in the event upon postback and rediect/pass the info
> acordingly.
> --
> Curt Christianson
> Owner/Lead Developer, DF-Software
> Site: http://www.Darkfalz.com
> Blog: http://blog.Darkfalz.com
>
> "Manuel" <My@.NoMailLand.com> wrote in message
> news:ZKidnUMSCYGVEDbcRVn-tQ@.giganews.com...
>

Separating presentation and data access tier. Best Practices for writing simple/intermedia

Hi,

I am writing a simple code, where I am a accessing database table, and based on the content of the table I dynamically generate a webform. In the webform I have a few buttons which when clicked do some dataaccess.

I have a class that performs all the dataacess, another class that I use to place the controls on the webform.
I am not sure if the code design is appropriate. The presentation and biz logic layer are not completely separated. I would like to know some best practice coding tips from the experianced ppl here. Or if anyone can send a pseudocode of his/her coding methods.

I would like to know when writing simple modules, which typically involves some dataaccess, data manipulation and then data presentation, which layer should go into the events like Page_load and CmdButton_click.

In the code below, I am not quite comfortable with SQL statement generation in the Page_Load event

My code structure looks like


Page_load()
{

string SQL="some sql stmt"
DataSet dsdata=new DataSet()
dsdata=dataaccessclass.getdata(SQL)

foreach(datarow dr in dsdata)
{

if(some condition)
//create control A
//set control properties based on dr
//call addcontrolclass.placecontrolAfunction
else
{

}

}

Cmd_Button_click()
{

//check which button is clicked
//make a SQL statment as string
//call dataaccessclass.performoperation(SQL)

}

Hey there,

I find when I'm writing, the best way to keep it all separate is to write a bindcontrols() procedure that makes calls to the logic classes/methods, this way populating your controls is done by calls the furthest away you can get from your presentation layer.

Hope it helps, it is saturday night where I am and I have had a few brewskies!

Cheers

Steve
I would seriously contemplating looking at the MS Data Access Applcation Blocks. I Class Library that takes care of your DAL quite elegantly (it's free, and all the source code is included, and it's really simple to use):

Data Access Applcation Blocks

Seperate Content from Code

After nearing completion of my first full .Net web application I am left with a page consisitng of around 600 lines of code, approximately split half and half between VB script and HTML page content.

Can anyone point me in the right direction of the best way to encapsulate the VB code into its own seperate file and then rerefence this from the form in the HTML page?

Hi,

you could place the code into separate aspx.vb file into its own class deriving form System.Web.UI.Page. Then putting the aspx to inherit from this code-behind class (with Inherits directive) would work.

Say you have files

page1.aspx (important ones are CodeFile and Inherits attributes)

<%@. Page Language="VB" AutoEventWireup="false" CodeFile="page1.aspx.vb" Inherits="page1" %>

...

page1.aspx.vb

Partial Class page1

Inherits System.Web.UI.Page

End Class

When you could put code into aspx.vb file into the class.

More information :http://www.asp.net/QuickStart/aspnet/doc/pages/codebehind.aspx

Of course if have code & logic which is such that it's common to more than one form, you might want to consider having a separate component (class library or in App_Code) where you could centralize logic that is needed in multiple places.


Thanks joteke, I think I'm slowly getting there.

So the .aspx file inherits the class that you create in the aspx.vb file ?

Then within that class you simply insert all the code that was previously in the <script> section at the top of the .aspx file?

What does the'AutoEventWireup' attribute do?


Also, do i need to import the namespaces in the aspx file or the aspx.vb file ?

duncanhall:

So the .aspx file inherits the class that you create in the aspx.vb file ?

Yes. Basically with partial classes in ASP.NET 2.0 (when using CodeFile attribute) a second partial class is created. This class contains control declarations for controls residing on aspx (removes the need for you to declare them on aspx.vb as you had to do with ASP.NEt v1.x).

These two partial classes are merged when compilation occurs and are used as base class for class which is created when the actual aspx is parsed and compiled (yes, basically the aspx itself)

duncanhall:



Then within that class you simply insert all the code that was previously in the <script> section at the top of the .aspx file?

Yes, note that the code-behind class is now equally part of your page.

duncanhall:


What does the'AutoEventWireup' attribute do?

It indicates whether Page's event are autowired. If enabled, it means that you have a method which matches Page_[EventName] or [ControlID]_[EventName] name so that method would automatically be assigned as event handler for the correspondng event of P?ager or the Control. Normally these event methods need to be wired explicitly.

See:ASP.NET Web Server Control Event Model


If code is in code-behind, then you import in code-behind.
Thanks a lot joteke, everything seems to make sense now, and my first app is pretty much finished!

Seperating code from HTML markup

Hello All:
I am writing to ask for your opinions. I have a colleague who combines his
code with the markup used to display the code (reckoning back to classic
ASP). Here's an example of a datagrid column:
<asp:TemplateColumn>
<ItemStyle CssClass="TableData" Width="15%"></ItemStyle>
<ItemTemplate>
<a href="http://links.10026.com/?link=<%# ShowURL(DataBinder.Eval(Container.DataItem,
"FORM_ID"))%>" target="_blank">
<%# DataBinder.Eval(Container.DataItem, "FORM_NUMBER")%></a>
</ItemTemplate>
</asp:TemplateColumn>
or another column
<asp:TemplateColumn>
<ItemStyle CssClass="TableData" Width="20%"></ItemStyle>
<ItemTemplate>
<%# DisplayState(DataBinder.EvalContainer.DataItem,
"FORM_STATE_CD"))%>
</ItemTemplate>
</asp:TemplateColumn>
Where ShowURL and DisplayState are defined in the code-behind. We have the
restriction that we can not use ViewState when creating our webforms
(security breach due to how they have architected their web app). I wonder
if there is a bettre way to do this.
In my opinion, this is sloppy programming. I, however, could be wrong.
Maybe this is the best way to do this. So I am asking:
What is your opinion regarding mixiing content and functionality.
If you think that this could have been done differnetly, What would you have
done? Is there a better way to do this?
Finally, the IDE will not display the Design View of the page that contains
this markup. The message says "Could not open in Design View. Quote values
differently inside of a '<% ...value... %>' block."
Thank you for your input.
--
JoeAs for the designer error, simply switching:
a href="http://links.10026.com/?link=<%# ShowURL(DataBinder.Eval(Container.DataItem, "FORM_ID"))%>"
target="_blank">
to:
a href='<%# ShowURL(DataBinder.Eval(Container.DataItem, "FORM_ID"))%>'
target="_blank">
should solve that (replace the " around the <%# ... %> with ' )
As far as I'm concerned, the question is pretty trivial. The separation of
code and HTML is almost complete in your examples below. ShowURL and
DisplayState both represent functionality that resides outside the HTML, and
if it needs to be reused, it could easily be extracted to a seperate code
file. The only way to achieve even greater separation is to hook into the
OnItemDataBound event and write a considerable chunk of code. For any
complex binding, this will result in a lot of code generating HTML. I've
seen code that took this approach, and it's an absolute nightmare. You end
with huge amounts of html created in code (new Table(), new TableRow(),
table.Rows.Add(tr), new TableCell(), new Label(), label.Text = SomeDBValue;
cell.Controls.Add(label), row.Cells.Add(cell)...), which is far harder to
maintain and change than the examples below.
The code has pretty good abstraction
1 - it uses methods located in codebehind for any actual processing
2 - It uses DataBinder.Eval which abstracts away the business layer
implementation (is Container.DataItem a datarowview ora custom class? who
knows, and why should the presentation layer care?)
I think you'll find that the benefits of further separation aren't worth
the high price you'll end up paying
Karl
--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Joe" <Joe@.discussions.microsoft.com> wrote in message
news:65EF2A6E-0440-4DFC-B8B5-AE49C4C8BBA1@.microsoft.com...
> Hello All:
> I am writing to ask for your opinions. I have a colleague who combines
> his
> code with the markup used to display the code (reckoning back to classic
> ASP). Here's an example of a datagrid column:
> <asp:TemplateColumn>
> <ItemStyle CssClass="TableData" Width="15%"></ItemStyle>
> <ItemTemplate>
> <a href="http://links.10026.com/?link=<%# ShowURL(DataBinder.Eval(Container.DataItem,
> "FORM_ID"))%>" target="_blank">
> <%# DataBinder.Eval(Container.DataItem, "FORM_NUMBER")%></a>
> </ItemTemplate>
> </asp:TemplateColumn>
> or another column
> <asp:TemplateColumn>
> <ItemStyle CssClass="TableData" Width="20%"></ItemStyle>
> <ItemTemplate>
> <%# DisplayState(DataBinder.EvalContainer.DataItem,
> "FORM_STATE_CD"))%>
> </ItemTemplate>
> </asp:TemplateColumn>
> Where ShowURL and DisplayState are defined in the code-behind. We have
> the
> restriction that we can not use ViewState when creating our webforms
> (security breach due to how they have architected their web app). I
> wonder
> if there is a bettre way to do this.
> In my opinion, this is sloppy programming. I, however, could be wrong.
> Maybe this is the best way to do this. So I am asking:
> What is your opinion regarding mixiing content and functionality.
> If you think that this could have been done differnetly, What would you
> have
> done? Is there a better way to do this?
> Finally, the IDE will not display the Design View of the page that
> contains
> this markup. The message says "Could not open in Design View. Quote
> values
> differently inside of a '<% ...value... %>' block."
> Thank you for your input.
> --
> Joe
Thank you Karl. This helped. As always, I appreciate your input.
--
Joe
"Karl Seguin [MVP]" wrote:

> As for the designer error, simply switching:
> a href="http://links.10026.com/?link=<%# ShowURL(DataBinder.Eval(Container.DataItem, "FORM_ID"))%>"
> target="_blank">
> to:
> a href='<%# ShowURL(DataBinder.Eval(Container.DataItem, "FORM_ID"))%>'
> target="_blank">
> should solve that (replace the " around the <%# ... %> with ' )
> As far as I'm concerned, the question is pretty trivial. The separation of
> code and HTML is almost complete in your examples below. ShowURL and
> DisplayState both represent functionality that resides outside the HTML, a
nd
> if it needs to be reused, it could easily be extracted to a seperate code
> file. The only way to achieve even greater separation is to hook into the
> OnItemDataBound event and write a considerable chunk of code. For any
> complex binding, this will result in a lot of code generating HTML. I've
> seen code that took this approach, and it's an absolute nightmare. You en
d
> with huge amounts of html created in code (new Table(), new TableRow(),
> table.Rows.Add(tr), new TableCell(), new Label(), label.Text = SomeDBValue
;
> cell.Controls.Add(label), row.Cells.Add(cell)...), which is far harder to
> maintain and change than the examples below.
> The code has pretty good abstraction
> 1 - it uses methods located in codebehind for any actual processing
> 2 - It uses DataBinder.Eval which abstracts away the business layer
> implementation (is Container.DataItem a datarowview ora custom class? who
> knows, and why should the presentation layer care?)
> I think you'll find that the benefits of further separation aren't worth
> the high price you'll end up paying
> Karl
> --
> http://www.openmymind.net/
> http://www.fuelindustries.com/
>
> "Joe" <Joe@.discussions.microsoft.com> wrote in message
> news:65EF2A6E-0440-4DFC-B8B5-AE49C4C8BBA1@.microsoft.com...
>
>