tag:blogger.com,1999:blog-35060694306760323702024-03-22T00:17:59.054+05:30Dot Net SparksC#.NET,ASP.NET,ADO.NET,LINQ,AJAX,JavaScript,SharePointChandra kaanthhttp://www.blogger.com/profile/10895546723390076324noreply@blogger.comBlogger9125tag:blogger.com,1999:blog-3506069430676032370.post-9587415787151855382013-02-08T18:06:00.000+05:302013-02-08T18:08:40.442+05:30Configure Forms-Based Authentication using Active Directory Membership Provider in SharePoint 2010<div dir="ltr" style="text-align: left;" trbidi="on">
In this post, I am going to explain you how to configure forms-based authentication using active directory membership provider.<br />
<br />
First, let’s create a web application. Go to Central Administration<br />
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi45bWophMDIJucJ_OyNIIJCAqaFNva5-3Gjg6bazvNCEDF_mR5MkcwmxaZHTZDcDERnAJuxmBfWo3FdIIdlvmUNw6peqO-rKBAD7n4Awij1OSUtFF6YUi08BY9aMKuw84kHv4B9Z7UKh0/s1600/CentralAdmin.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="296" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi45bWophMDIJucJ_OyNIIJCAqaFNva5-3Gjg6bazvNCEDF_mR5MkcwmxaZHTZDcDERnAJuxmBfWo3FdIIdlvmUNw6peqO-rKBAD7n4Awij1OSUtFF6YUi08BY9aMKuw84kHv4B9Z7UKh0/s640/CentralAdmin.png" width="640" /></a></div>
<div>
<br />
<a name='more'></a></div>
<div>
Click on Manage web applications under Application Management </div>
<div>
<br /></div>
Click New from the ribbon to create a new web application <br />
<br />
<div>
Select Claims Based Authentication under Authentication Category of Create New Web Application Dialog.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj1qKjl-X1NOsCJUiUdS_7Ebx0NJEvMPaDTGSriKoFhH8u7YBLQyF3tfZpuR48Qfb6bdJhrhKYbCH9L_CLsNL1rVFx-q6hc-q4cL18_emPMl3sn08kRuS64hrLckhwA4hNFhZp4I6KxjF8/s1600/CNWebApp.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj1qKjl-X1NOsCJUiUdS_7Ebx0NJEvMPaDTGSriKoFhH8u7YBLQyF3tfZpuR48Qfb6bdJhrhKYbCH9L_CLsNL1rVFx-q6hc-q4cL18_emPMl3sn08kRuS64hrLckhwA4hNFhZp4I6KxjF8/s640/CNWebApp.png" width="640" /></a></div>
<div>
<br /></div>
Under Claims Authentication Types, Check ‘Enable Forms Based Authentication (FBA)’ check box and enter membership provider name in ASP.NET Membership provider name text box.<br />
<br />
Membership provider name is user defined and can be anything. Here, I am using ‘ADMembershipProvider’. This name will be used at the time of modifying web.congif’s.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj0fURkpIDosFSXD_awwJSj3GHfzhZS-EItbNoSngh7UJ7GIjIQPvMrAyISUbKXwvmySMgSti2IqQl6C4l9PnllgGcuCpHi6xwWq2mn84eZvYHkf73cKfiGZuRns_Nr8LKyOztC5UfPjo8/s1600/CAT.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="417" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj0fURkpIDosFSXD_awwJSj3GHfzhZS-EItbNoSngh7UJ7GIjIQPvMrAyISUbKXwvmySMgSti2IqQl6C4l9PnllgGcuCpHi6xwWq2mn84eZvYHkf73cKfiGZuRns_Nr8LKyOztC5UfPjo8/s640/CAT.png" width="640" /></a></div>
<div>
<div class="MsoListParagraph">
<br /></div>
<div class="MsoListParagraph">
Click OK. </div>
<div class="MsoListParagraph">
<br />
In order to configure FBA, we need to modify web.config of<br />
<br />
1) Central Administration<br />
2) Security Token Service Application and,<br />
3) Your Claims based web application <br />
<br />
<span style="background-color: white;"><span style="color: red;">Warning: Take a backup of above mentioned web.configs before modifying. </span></span><br />
<br />
1)Open SharePoint Central Administration web.config in your favorite editor. Find <b></configSections></b> and add the following below it.<br />
<br />
<pre class="xml" name="code"><connectionstrings>
<add connectionstring="LDAP://domain1.com/CN=Users,DC=domain1,DC=com" name="ADConnectionString" />
</connectionstrings></pre>
<br />
To know your connection string, open ‘Active Directory Users and Computers’ in ‘Administrative Tools’.</div>
<br />
In the console tree, right click the domain object -> All Tasks -> Resultant Set Of Policy (Planning)… <br />
<br />
Click the browse button next to Container under User information and select the container. This will get you the container path. <br />
<br /></div>
<div>
Now, your connection string will be like LDAP://domain object/container path<br />
<br /></div>
<div>
Next, find <b><PeoplePickerWildcards></b> and replace its content with the following<br />
<br />
<pre class="xml" name="code"><clear />
<add key="AspNetSqlMembershipProvider" value="%" />
<add key="ADMembershipProvider" value="%" /></pre>
<br />
Next, find <b><membership></b> and replace its content with the following<br />
<pre class="xml" name="code">
<providers>
<add attributemapusername="sAMAccountName" connectionstringname="ADConnectionString" enablesearchmethods="true" name="ADMembershipProvider" type="System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</providers></pre>
<br />
<br />
<div class="MsoListParagraph">
2) Open Security Token Service Application web.config. Find <b></system.net></b> and add the following below it.<br />
<br />
<pre class="xml" name="code">
<system.web>
<membership defaultProvider="i">
<providers>
<add name="ADMembershipProvider" type="System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ADConnectionString" enableSearchMethods="true" attributeMapUsername="sAMAccountName"/>
</providers>
</membership>
</system.web>
<connectionStrings>
<add name ="ADConnectionString" connectionString="LDAP://domain1.com/CN=Users,DC=domain1,DC=com" />
</connectionStrings> </pre>
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="MsoListParagraph">
<br />
3) Now, open your claims based web application web.config <br />
<br />
Find <b></configSections></b> and add the following below it.<br />
<pre class="xml" name="code">
<connectionStrings>
<add name ="ADConnectionString" connectionString="LDAP://domain1.com/CN=Users,DC=domain1,DC=com" />
</connectionStrings>
</pre>
<br />
Next, find <b><PeoplePickerWildcards></b> and replace its content with the following<br />
<pre class="xml" name="code">
<clear />
<add key="AspNetSqlMembershipProvider" value="%" />
<add key="ADMembershipProvider" value="%" />
</pre>
<br />
Next, find <b><membership defaultProvider="i"></b> and replace its content with the following<br />
<pre class="xml" name="code">
<providers>
<add name="i" type="Microsoft.SharePoint.Administration.Claims.SPClaimsAuthMembershipProvider, Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
<add name="ADMembershipProvider" type="System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ADConnectionString" enableSearchMethods="true" attributeMapUsername="sAMAccountName" />
</providers></pre>
<br />
Now, let’s create site collection for the previously created web application<br />
<br />
1) Set the title for the site collection<br />
2) Under Primary Site Collection Administrator category, Click<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJAnGtA2mSXLgqVejcy6nMsK_h4auCBaAoAd5-Ncx_0QG55Zn3VTB7K58Diw7-_lQq4gO_QBroSjgEpTfyv4Jh_2VsxJPvYfilS8YcVR-kTk6sEIolVN_2sUHvTDsfJpkOn_8HemT7k5k/s1600/Browse.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJAnGtA2mSXLgqVejcy6nMsK_h4auCBaAoAd5-Ncx_0QG55Zn3VTB7K58Diw7-_lQq4gO_QBroSjgEpTfyv4Jh_2VsxJPvYfilS8YcVR-kTk6sEIolVN_2sUHvTDsfJpkOn_8HemT7k5k/s1600/Browse.png" /></a> and search for the active directory user. <br />
<br />
Select the user under Form Auth category of Select People dialog and click OK.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhzQUN31KcykKtfVI0qpzE6NsBwXmX4G_6av5GuzaMtnhuOnmCaXjsWy80OcbBnHX2cr0Qcb4Wgh0Ljpvy5nWws6rPkkJpmZ9XNdcPtby3B61RtN22FALO2Da6oZJxmLKP01EacC4irm3s/s1600/SelectPeople.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="580" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhzQUN31KcykKtfVI0qpzE6NsBwXmX4G_6av5GuzaMtnhuOnmCaXjsWy80OcbBnHX2cr0Qcb4Wgh0Ljpvy5nWws6rPkkJpmZ9XNdcPtby3B61RtN22FALO2Da6oZJxmLKP01EacC4irm3s/s640/SelectPeople.png" width="640" /></a></div>
<br />
3) Click OK to finish site collection creation.<br />
<br /></div>
Now, browser the web application and select Forms Authentication<br />
<br /></div>
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiK5voLZiKV2gbSXXr33CEKd2IWwVERogHF-sAfOR94gTA-D5QIAulV-1T3zXTLr41232a0x_FNmcfbp6cHVCs2AOjmfVKcRnLUAuuPwzRXG14S9seO4k-erW2BcRcshGXuUkRo3meJX1U/s1600/SignInFormAuth.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="251" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiK5voLZiKV2gbSXXr33CEKd2IWwVERogHF-sAfOR94gTA-D5QIAulV-1T3zXTLr41232a0x_FNmcfbp6cHVCs2AOjmfVKcRnLUAuuPwzRXG14S9seO4k-erW2BcRcshGXuUkRo3meJX1U/s640/SignInFormAuth.png" width="640" /></a></div>
<div class="MsoListParagraph">
<br /></div>
Enter the user name and password and click Sign In<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg87crCwIdAa2HKu3ovNZ3WZIGWnl8Fa9i7v_RkZh8kuey1tlUErikgOjfPNoQwyKYWXZ6ZAvTbyKDLv3lH1qsXuWYR2JZqCGY1hQV1IGczQIl28ieah8sY9k6Ffb_Fdo_DNy3kcUDa33k/s1600/SignInUP.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="296" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg87crCwIdAa2HKu3ovNZ3WZIGWnl8Fa9i7v_RkZh8kuey1tlUErikgOjfPNoQwyKYWXZ6ZAvTbyKDLv3lH1qsXuWYR2JZqCGY1hQV1IGczQIl28ieah8sY9k6Ffb_Fdo_DNy3kcUDa33k/s640/SignInUP.png" width="640" /></a></div>
<br />
And, now you are done<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhl-iFlbR16_OzZ8sj1o9cqLkE_jsFnGAfq4laJ4mWU3-Y72i2MF33TKj3AINudXfcZTWmcofBOKbzAcUbDzkCv4idwbLGd-e1b_YB-fqHaqF39yeJIBNLvDPWMdHRRlxJ1aBgLMksw7W0/s1600/WebSite.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="233" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhl-iFlbR16_OzZ8sj1o9cqLkE_jsFnGAfq4laJ4mWU3-Y72i2MF33TKj3AINudXfcZTWmcofBOKbzAcUbDzkCv4idwbLGd-e1b_YB-fqHaqF39yeJIBNLvDPWMdHRRlxJ1aBgLMksw7W0/s640/WebSite.png" width="640" /></a></div>
<div>
<br /></div>
</div>
Chandra kaanthhttp://www.blogger.com/profile/10895546723390076324noreply@blogger.com2tag:blogger.com,1999:blog-3506069430676032370.post-28786536771333307722012-06-02T12:01:00.000+05:302012-08-08T12:17:01.802+05:30Open application page in SharePoint 2010 dialog framework using Code-Behind<div dir="ltr" style="text-align: left;" trbidi="on">
In the previous post, I have explained you how to open application page in SharePoint 2010 dialog framework, where we used the script with in web part i.e., .ascx.<br />
<br />
In this post I will explain you how to open the same application page in dialog framework using code-behind model with some modifications to the previous solution.<br />
<br />
Here, we gonna do only two things, first we will add an anchor tag with attributes id= “anchor” and runat=”server” in the SP2010DFWebPartUserControl.ascx, in order to have the access in the code-behind. Next, in the code-behind file, we write some JavaScript and assign it to script variable and register the script to emit on page load. <br />
<br />
<a name='more'></a>In SP2010DFWebPartUserControl.ascx, just add the following code
<br />
<pre class="html" name="code"><a id="anchor" runat="server">Click here</a>
</pre>
And, in the SP2010DFWebPartUserControl.ascx.cs, add the following code in the page load event
<br />
<pre class="csharp" name="code"> var script = @"function openMyDialog() {
var options = {
url: '/_layouts/SP2010DF/MyApplicationPage.aspx',
width: 400,
height: 250,
title: 'My Dialog',
allowMaximize: true,
showClose: true
};
SP.UI.ModalDialog.showModalDialog(options);
}
//SP.SOD.executeOrDelayUntilScriptLoaded(display, 'sp.ui.dialog.js'); ";
//Updated the above statement by replacing "display" with "openMyDialog"
SP.SOD.executeOrDelayUntilScriptLoaded(openMyDialog, 'sp.ui.dialog.js'); ";
//The following statement registers the script on page load
Page.ClientScript.RegisterStartupScript(typeof(LayoutsPageBase), Guid.NewGuid().ToString(), script, true);
anchor.HRef = "javascript:openMyDialog()";
</pre>
That’s it. Now, you can deploy the solution and test it.
</div>Chandra kaanthhttp://www.blogger.com/profile/10895546723390076324noreply@blogger.com2tag:blogger.com,1999:blog-3506069430676032370.post-52267937518786297612012-05-05T16:28:00.000+05:302012-05-05T16:42:45.452+05:30Open application page in SharePoint 2010 dialog framework<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
</div>
This example shows how to open application page in SharePoint 2010 dialog framework.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHqSSoXhDJqYq_meeHEgivkYqZx2vdnmza4IrchH0oUR_bVMxcoa2OxcuVAl7vtKqDE1irZuQ66CqiUEdGfdbLB7MN7VRF7VDi0bwuxYKHgj0HVYS1-irI7U5_L5VffuT8KdIN5_hBICk/s1600/SP2010DF.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="264" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHqSSoXhDJqYq_meeHEgivkYqZx2vdnmza4IrchH0oUR_bVMxcoa2OxcuVAl7vtKqDE1irZuQ66CqiUEdGfdbLB7MN7VRF7VDi0bwuxYKHgj0HVYS1-irI7U5_L5VffuT8KdIN5_hBICk/s640/SP2010DF.png" width="640" /></a></div>
<br />
So, first we will create a visual web part in that we will have a link to open the application page in a dialog box.<br />
<br />
Now, let’s us start step by step<br />
<a name='more'></a><br />
Create an Empty SharePoint Project in Visual Studio 2010. Name the project as "SP2010DF".<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjE1qqbefc5lVlBhIsPLPXUxjIr5WG1nAVeWEbTvOtaHdiTs62eSITp2UsoX_1ng1GBIwBo_31PDHe2kNWCQl1wtUdUq40wkZqP0HAgnj6z4T3cy3WpLufwe3_hCytRWa9KWWRQ0PevZrQ/s1600/SP2010DF_NewProject.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="332" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjE1qqbefc5lVlBhIsPLPXUxjIr5WG1nAVeWEbTvOtaHdiTs62eSITp2UsoX_1ng1GBIwBo_31PDHe2kNWCQl1wtUdUq40wkZqP0HAgnj6z4T3cy3WpLufwe3_hCytRWa9KWWRQ0PevZrQ/s640/SP2010DF_NewProject.png" width="640" /></a></div>
<br />
In the SharePoint Customization Wizard window, type the url of the SharePoint site and Click Validate.<br />
<br />
After connecting successfully to the SharePoint site, select Deploy as a Farm Solution option and Click Finish.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQ-fcBWN7m5pn_ONyhra4h_wWAw0rCUCXWrsC-Dq4AgmH3oi4nMcIZ1TJMk-Gpo21wlGmrPYF17b8s0YU09IAC7vS1oMUwgzE3vmLMlji5i3kK7-lHn8NrKC9-tVXUt6VJt91mjdXVTBQ/s1600/SP2010DFCustWiz.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="283" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQ-fcBWN7m5pn_ONyhra4h_wWAw0rCUCXWrsC-Dq4AgmH3oi4nMcIZ1TJMk-Gpo21wlGmrPYF17b8s0YU09IAC7vS1oMUwgzE3vmLMlji5i3kK7-lHn8NrKC9-tVXUt6VJt91mjdXVTBQ/s400/SP2010DFCustWiz.png" width="400" /></a></div>
<br />
Now, right click the project in the solution explorer, Click Add -> New Item. Select visual web part and name it as "SP2010DFWebPart"<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg3qgls89itCu3U7d9dRt1CEO1XOaCEkBoOjOs-M6fANNprf0tQzdP6cb6PdVZ7RIyKBp5BzMbw0AErhHCo3AGCC4swFTK4qT7blfOdgGG-iXw0NaSfDYO4uO9ck6yI3WTp17mHPCm_zAY/s1600/SP2010DF_AddNewItem.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="334" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg3qgls89itCu3U7d9dRt1CEO1XOaCEkBoOjOs-M6fANNprf0tQzdP6cb6PdVZ7RIyKBp5BzMbw0AErhHCo3AGCC4swFTK4qT7blfOdgGG-iXw0NaSfDYO4uO9ck6yI3WTp17mHPCm_zAY/s640/SP2010DF_AddNewItem.png" width="640" /></a></div>
<br />
Now, let's add the application page. Right click on the project, click Add -> SharePoint "Layouts" Mapped folder. This adds Layouts Folder and SP2010DF as its sub folder.<br />
<br />
Right click SP2010DF sub folder, click Add -> New Item -> Type MyApplicationPage.aspx and click Add.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhyvUN7da5v3BNw-0EuRcWGeCYJ_Sn6d_-YhzKZW2xVGaMKwi1kMU2kxCEz3d5Tiv3IpgQ0VUTeIabpnP_l8ZfbO__ZJtmkfaMRpxdNYouKSdid2fYhjffAx1nmE6erE4gpYIGhTMBzjbY/s1600/SP2010DF_AddNewItemApp.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="332" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhyvUN7da5v3BNw-0EuRcWGeCYJ_Sn6d_-YhzKZW2xVGaMKwi1kMU2kxCEz3d5Tiv3IpgQ0VUTeIabpnP_l8ZfbO__ZJtmkfaMRpxdNYouKSdid2fYhjffAx1nmE6erE4gpYIGhTMBzjbY/s640/SP2010DF_AddNewItemApp.png" width="640" /></a></div>
<br />
In MyApplicationPage.aspx, add your own text or controls in the main content placeholder section. Here, I just added some text "This is my application page".<br />
<br />
Now, MyApplicationPage.aspx looks like<br />
<br />
<pre class="html" name="code"><%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MyApplicationPage.aspx.cs" Inherits="SP2010DF.Layouts.SP2010DF.MyApplicationPage" DynamicMasterPageFile="~masterurl/default.master" %>
<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
</asp:Content>
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
This is my application page
</asp:Content>
<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
Application Page
</asp:Content>
<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >
My Application Page
</asp:Content>
</pre>
<br />
In the SP2010DFWebPartUserControl.ascx, we are going to add some javascript to display the application page in dialog box and an anchor tag to fire the dialog.<br />
<br />
Now, SP2010DFWebPartUserControl. ascx looks like<br />
<br />
<pre class="xml" name="code">
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="SP2010DFWebPartUserControl.ascx.cs" Inherits="SP2010DF.SP2010DFWebPart.SP2010DFWebPartUserControl" %>
<script type="text/javascript">
function openMyDialog() {
var options = {
url: "/_layouts/SP2010DF/MyApplicationPage.aspx",
width: 400,
height: 250,
title: "My Dialog",
allowMaximize: true,
showClose: true
};
SP.UI.ModalDialog.showModalDialog(options);
}
</script>
<a href="javascript:openMyDialog()">Click here</a></pre>
<pre class="brush:html;"> </pre>
<br />
That’s it. Now you can deploy the solution, add web part to the website and test it.
</div>Chandra kaanthhttp://www.blogger.com/profile/10895546723390076324noreply@blogger.com2tag:blogger.com,1999:blog-3506069430676032370.post-34122081317255671832012-01-23T15:16:00.001+05:302012-02-24T16:47:24.388+05:30Adding custom action to Personal Actions menu of SharePoint 2010<div dir="ltr" style="text-align: left;" trbidi="on">This example shows how to create a custom action menu item to PersonalActions menu. In order to create custom action menu item we need an empty element and a feature.<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj5xyphnnRYVmWMmEyzLQTcwgsFFem2lMqfj4mdvKIy04EEE8SpEaML3Tqh1Uh-UCxav1ddod8KhVV2btpuT1bC9LlrFKreeVpoR_bt19QsbwyBVJzcVrLGUvXx9_HQEzPvlSQM2lkInB0/s1600/MyCustomAction_PersonalActions.png" imageanchor="1"><img border="0" height="311" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj5xyphnnRYVmWMmEyzLQTcwgsFFem2lMqfj4mdvKIy04EEE8SpEaML3Tqh1Uh-UCxav1ddod8KhVV2btpuT1bC9LlrFKreeVpoR_bt19QsbwyBVJzcVrLGUvXx9_HQEzPvlSQM2lkInB0/s320/MyCustomAction_PersonalActions.png" width="256" /></a></div><br />
<a name='more'></a>Now, let’s us start step by step.<br />
Create an Empty SharePoint Project in Visual Studio 2010. Name the project as “MyCustomAction_PersonalActions”. <br /><br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiv7101_jkU3aTDwfaub6jeLcSSM7wHzvZWqGVPC862dRNi8UuQMJv7ulPMiocwN6sX0usAQQkdsXnV_UCqHM2-yhmPYgOAAuuZBXDVyAt4jc58CzTl3KAChT3jcN7rYLx8aHD9BU9Jzr4/s1600/New_Project.png" imageanchor="1"><img border="0" height="187" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiv7101_jkU3aTDwfaub6jeLcSSM7wHzvZWqGVPC862dRNi8UuQMJv7ulPMiocwN6sX0usAQQkdsXnV_UCqHM2-yhmPYgOAAuuZBXDVyAt4jc58CzTl3KAChT3jcN7rYLx8aHD9BU9Jzr4/s320/New_Project.png" width="320" /></a></div><br /><br />
Now, in the SharePoint Customization Wizard, type the url of the SharePoint site and Click Validate. After connecting successfully to the SharePoint site, select Deploy as a Farm Solution option and Click Finish. <br /><br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiD2Paifd5tt7WfUGViviyCVxw7Db-tcME3ubH2EDBm_JMUDGDOfAUdG0AAy6oD5gG9mrWNmQ-C8-_ZhrFcdj4SNP_C_SyUm37aUr1UHHqB2jX1My9IXV3A9tmpaLDC7Ir5A_0uqPblGZs/s1600/SharePoint_Customization_Wizard.png" imageanchor="1"><img border="0" height="228" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiD2Paifd5tt7WfUGViviyCVxw7Db-tcME3ubH2EDBm_JMUDGDOfAUdG0AAy6oD5gG9mrWNmQ-C8-_ZhrFcdj4SNP_C_SyUm37aUr1UHHqB2jX1My9IXV3A9tmpaLDC7Ir5A_0uqPblGZs/s320/SharePoint_Customization_Wizard.png" width="320" /></a></div><br /><br />
Now, right click on the project in the solution explorer and select Add -> New Item.<br /><br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj7xH75muVEf8XF-h-_Xx7JKPACsy6B1YeKyvu8FvB21RT1LWBKRwuAtLXFN32d5qh1FKV-b-eNMBfcrnTscbrdl68PAwoYPdwoK1gKKWXaEAfCEcZyXnY-hdkxpRqiPgnCS2QrGwH6n8g/s1600/New_Item.png" imageanchor="1"><img border="0" height="164" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj7xH75muVEf8XF-h-_Xx7JKPACsy6B1YeKyvu8FvB21RT1LWBKRwuAtLXFN32d5qh1FKV-b-eNMBfcrnTscbrdl68PAwoYPdwoK1gKKWXaEAfCEcZyXnY-hdkxpRqiPgnCS2QrGwH6n8g/s320/New_Item.png" width="320" /></a></div><br /><br />
In the Add New Item dialog box select Empty Element and name it as “MyCustomActionElement” and click Add.<br /><br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhg4hOP6ohZ86zGZTXojN6aFZGf-w8MHruhFjQ2_6Rn_oPKinNbaaxe4wuDsJoa-ERiF2JRc-_O8enHfVMcmF_wUE9Ie3TCyIXW-8WHIPeys3fAlHIYkwKLBotxcDTE0mD-FNGowMv0cms/s1600/Add_New_Item.png" imageanchor="1"><img border="0" height="187" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhg4hOP6ohZ86zGZTXojN6aFZGf-w8MHruhFjQ2_6Rn_oPKinNbaaxe4wuDsJoa-ERiF2JRc-_O8enHfVMcmF_wUE9Ie3TCyIXW-8WHIPeys3fAlHIYkwKLBotxcDTE0mD-FNGowMv0cms/s320/Add_New_Item.png" width="320" /></a></div><br /><br />
This adds the empty element and a feature to the project. Now, in the Elements.xml add the following code <br />
<pre name="code" class="xml"><elements xmlns="http://schemas.microsoft.com/sharepoint/">
<customaction description="This is my custom action." groupid="PersonalActions" id="MyCustomAction" location="Microsoft.SharePoint.StandardMenu" sequence="1000" title="My Custom Action">
<urlaction url="javascript:alert('This is my custom action.');">
</urlaction></customaction>
</elements>
</pre>That’s it. Now you can deploy the solution and test it. </div>Chandra kaanthhttp://www.blogger.com/profile/10895546723390076324noreply@blogger.com0tag:blogger.com,1999:blog-3506069430676032370.post-22859149887129472362012-01-20T11:58:00.002+05:302012-02-24T16:37:31.769+05:30FileUpload Control in ASP.NET<div dir="ltr" style="text-align: left;" trbidi="on"><div dir="ltr" style="text-align: left;" trbidi="on">This article illustrates how to upload a file to server using FileUpload control. Here we are going to use one FileUpload control to select a file and one button control to upload the selected file to server.<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEheTFDVQXdnGxDMBaMqgsk2b6ZINGKBLP1e2jKsMG2mIlQon8-cVeLHT11vimjnUX7y8FCbQmALyPTuoxcK-PAfKPz7xWvs1fbXYmfeaCb5BzH-lMEZtJdacnJQHt5AUizK8vUeuOmaDQM/s1600/FileUpload_Control.png" imageanchor="1"><img border="0" height="94" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEheTFDVQXdnGxDMBaMqgsk2b6ZINGKBLP1e2jKsMG2mIlQon8-cVeLHT11vimjnUX7y8FCbQmALyPTuoxcK-PAfKPz7xWvs1fbXYmfeaCb5BzH-lMEZtJdacnJQHt5AUizK8vUeuOmaDQM/s320/FileUpload_Control.png" width="320" /></a></div></div><br />
<a name='more'></a>Create a new website in VS and add the following code that designs our aspx page with FileUpload control and button. Add a folder to the solution and name it as “Uploaded Files” because we are going to use it to store the uploaded files. <br />
<pre name="code" class="html"><%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="FileUpload_Example._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<b>FileUpload Control Example</b>
<br />
<table cellpadding="2">
<tr>
<td>
<asp:Label ID="lblMsg" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" /><asp:Button ID="btnUpload" runat="server"
Text="Upload" Width="100px" OnClick="btnUpload_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
</pre>And it’s code behind <br />
<pre name="code" class="csharp">using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace FileUpload_Example
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lblMsg.Visible = false;
}
else
{
lblMsg.Visible = true;
}
}
protected void btnUpload_Click(object sender, EventArgs e)
{
try
{
//Check wheather FileUpload1 control has file or not
if (FileUpload1.HasFile)
{
//get the selected file name.
string filename = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
//get the server path
string SaveLocation = Server.MapPath("~/Uploaded Files/") + filename;
//the following line saves the file to the server.
FileUpload1.PostedFile.SaveAs(SaveLocation);
lblMsg.Visible = true;
lblMsg.Text = "File upload Successful.";
lblMsg.ForeColor = System.Drawing.Color.Green;
}
else
{
lblMsg.Visible = true;
lblMsg.Text = "Please choose a file to upload.";
lblMsg.ForeColor = System.Drawing.Color.Red;
}
}
catch (Exception ex)
{
string str = ex.Message;
lblMsg.Visible = true;
lblMsg.Text = "Unable to upload file.";
lblMsg.ForeColor = System.Drawing.Color.Red;
}
}
}
}
</pre></div>Chandra kaanthhttp://www.blogger.com/profile/10895546723390076324noreply@blogger.com0tag:blogger.com,1999:blog-3506069430676032370.post-86943212498673827242011-12-19T16:36:00.000+05:302011-12-19T16:37:18.599+05:30Error “Word experienced an error trying to open the file”<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOwDWhzdW9il3fsoe4W_JFqAgDlZ5w7GT7wkDanjl_kJDw5Bc-o8-IV53CBNqlmWL6hIaHixjcBqCqIaZ0rFRl5U2GliRqAsXslQf18UpenZP_wzg4V5DaNz10fQAIhPl0bjRDYfjVUqI/s1600/Word_experienced_an_error_trying_to_open_the_file.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="168" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOwDWhzdW9il3fsoe4W_JFqAgDlZ5w7GT7wkDanjl_kJDw5Bc-o8-IV53CBNqlmWL6hIaHixjcBqCqIaZ0rFRl5U2GliRqAsXslQf18UpenZP_wzg4V5DaNz10fQAIhPl0bjRDYfjVUqI/s320/Word_experienced_an_error_trying_to_open_the_file.png" width="320" /></a></div>
<a name='more'></a><br />
Try the following steps:-<br />
1. Open Word 2010.<br />
2. Click on File -> Options. <br />
3. Select Trust Center -> Trust center settings. <br />
4. Select Protected view.<br />
5. Uncheck all the options under Protected View > OK. <br />
<br />
Now, try opening the word file.</div>Chandra kaanthhttp://www.blogger.com/profile/10895546723390076324noreply@blogger.com2tag:blogger.com,1999:blog-3506069430676032370.post-28887216155038653402011-11-29T17:06:00.001+05:302012-02-24T16:36:32.799+05:30Password Generator in ASP.NET<div dir="ltr" style="text-align: left;" trbidi="on">This article illustrates how to create a simple password generator.<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFuzp749yN2ZCOhpc_mgmVBPHX54BnIodoTbU4cA6mp5JF0m0XZtZtdDrztyA2DkOgCUa45k-seAkc82JQhP0y-8987OHJAkc8M95JbQyL7Z3ED9UXPgn5oyzgpn5gfr9j5X4kRmIcDk8/s1600/Password_Generator.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="74" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFuzp749yN2ZCOhpc_mgmVBPHX54BnIodoTbU4cA6mp5JF0m0XZtZtdDrztyA2DkOgCUa45k-seAkc82JQhP0y-8987OHJAkc8M95JbQyL7Z3ED9UXPgn5oyzgpn5gfr9j5X4kRmIcDk8/s320/Password_Generator.png" width="320" /></a></div><br />
<a name='more'></a><br />
Create a new website in VS and add the following code that designs our aspx page.<br />
<pre name="code" class="html"><%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Password Generator</title>
<script type="text/javascript">
//Javascript function to allow only numeric values in the text box.
function isNumeric(keyCode) {
if (keyCode == 16)
isShift = true;
return ((keyCode >= 48 && keyCode <= 57 || keyCode == 8 || (keyCode >= 96 && keyCode <= 105)) && isShift == false);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<b>Password Generator</b><br />
<br />
<table cellpadding="2">
<tr>
<td>
Password Length
</td>
<td>
<asp:TextBox ID="txtPassLength" runat="server" Width="180px" onkeydown = "return isNumeric(event.keyCode);"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnGeneratePassword" runat="server" Text="Generate Password" OnClick="btnGeneratePassword_Click" />
</td>
</tr>
</table>
<asp:Label ID="lblPassword" runat="server" Text="Password" ForeColor="#993300"></asp:Label>
</div>
</form>
</body>
</html>
</pre>And it’s code behind as follows<br />
<pre name="code" class="csharp">using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
private char[] _passCharactors = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`~!@#$%^&*".ToCharArray();
private int _passLength = 0;
private string _password;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lblPassword.Visible = false;
}
txtPassLength.Attributes.Add("onkeydown", "return isNumeric(event.keyCode);");
}
protected void btnGeneratePassword_Click(object sender, EventArgs e)
{
Random rnd = new Random();
if (!string.IsNullOrEmpty(txtPassLength.Text))
{
_passLength = Convert.ToInt32(txtPassLength.Text);
if (_passLength > 0)
{
for (int i = 0; i < _passLength; i++)
{
int randNumber = rnd.Next(0, _passCharactors.Length - 1);
_password += _passCharactors[randNumber];
}
lblPassword.Visible = true;
lblPassword.Text = "Your Password : " + _password;
}
}
else
{
lblPassword.Visible = true;
lblPassword.Text = "Please enter password length.";
}
}
}
</pre></div>Chandra kaanthhttp://www.blogger.com/profile/10895546723390076324noreply@blogger.com0tag:blogger.com,1999:blog-3506069430676032370.post-73379467832602011142011-11-28T19:29:00.001+05:302012-02-24T16:32:33.103+05:30Send mail in ASP.NET<div dir="ltr" style="text-align: left;" trbidi="on">This example shows how to send mail in asp.net. To send mail in asp.net <b>MailMessage</b> and <b>SmtpClient</b> class are used, which are in <b>System.Net.Mail</b> namespace.<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGGm1Tpmo1kMjZfTDtvrujHBM1gIXF2x3r0Rxo1LyOTFqlrObEl1hxOZJiP-XNR-ng-1HFm2LXntEngJ8t1_s4CoC9QlRd5WPHQBvcjN-aK1XrHhc-ex09ZcrQy-63RnXTbO1OPdVnZ7o/s1600/Send_Mail_in_ASP.NET.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="193" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGGm1Tpmo1kMjZfTDtvrujHBM1gIXF2x3r0Rxo1LyOTFqlrObEl1hxOZJiP-XNR-ng-1HFm2LXntEngJ8t1_s4CoC9QlRd5WPHQBvcjN-aK1XrHhc-ex09ZcrQy-63RnXTbO1OPdVnZ7o/s320/Send_Mail_in_ASP.NET.png" width="320" /></a></div><div class="separator" style="clear: both; text-align: center;"></div><br />
<a name='more'></a>Create a new website in VS and add the following code that designs our aspx page with text boxes, buttons, etc.<br />
<pre name="code" class="html"><%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Send Mail Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<b>Send Mail Example</b>
<table cellpadding="2">
<tr>
<td colspan="2">
<asp:Label ID="lblInfo" runat="server" Text="Info" ForeColor="#990000"></asp:Label>
</td>
</tr>
<tr>
<td>
From
</td>
<td>
<asp:TextBox ID="txtFrom" runat="server" Width="300px"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvFrom" runat="server" ControlToValidate="txtFrom"
Display="Dynamic" ErrorMessage="*"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revFrom" runat="server" ControlToValidate="txtFrom"
Display="Dynamic" ErrorMessage="Invalid Email ID" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
To
</td>
<td>
<asp:TextBox ID="txtTo" runat="server" Width="300px"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvTo" runat="server" ControlToValidate="txtTo" Display="Dynamic"
ErrorMessage="*"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revTo" runat="server" ControlToValidate="txtTo"
Display="Dynamic" ErrorMessage="Invalid Email ID" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
Subject
</td>
<td>
<asp:TextBox ID="txtSubject" runat="server" Width="300px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Message
</td>
<td>
<asp:TextBox ID="txtMessage" runat="server" Rows="10" TextMode="MultiLine" Width="500px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
&nbsp;
</td>
<td>
<asp:Button ID="btnSend" runat="server" Text="Send" Width="150px" OnClick="btnSend_Click" />
<asp:Button ID="btnClear" runat="server" Text="Clear" Width="150px" OnClick="btnClear_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
</pre>And it’s code behind<br />
<pre name="code" class="csharp">using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lblInfo.Visible = false;
}
}
protected void btnSend_Click(object sender, EventArgs e)
{
try
{
MailMessage message = new MailMessage();
message.From = new MailAddress(txtFrom.Text);
message.To.Add(new MailAddress(txtTo.Text));
message.Subject = txtSubject.Text;
message.Body = txtMessage.Text;
message.IsBodyHtml = true;
message.Priority = MailPriority.High;
SmtpClient client = new SmtpClient("localhost");
client.Send(message);
lblInfo.Visible = true;
lblInfo.Text = "Your message has been sent.";
txtFrom.Text = "";
txtTo.Text = "";
txtSubject.Text = "";
txtMessage.Text = "";
}
catch (Exception ex)
{
lblInfo.Visible = true;
lblInfo.Text = ex.Message;
}
}
protected void btnClear_Click(object sender, EventArgs e)
{
//Response.Redirect("Default.aspx");
txtFrom.Text = "";
txtTo.Text = "";
txtSubject.Text = "";
txtMessage.Text = "";
}
}
</pre><b>Note:</b> This method of sending mail requires application or website to be hosted on some webserver. </div>Chandra kaanthhttp://www.blogger.com/profile/10895546723390076324noreply@blogger.com0tag:blogger.com,1999:blog-3506069430676032370.post-84615423808565608092011-11-25T18:46:00.001+05:302012-02-24T16:31:28.765+05:30Cookies in ASP.NET<div dir="ltr" style="text-align: left;" trbidi="on">A cookie is a small bit of text that accompanies requests and pages as they go between the Web server and browser. The cookie contains information the Web application can read whenever the user visits the site.<br />
<br />
This example shows how to work with cookies in asp.net.<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijd-cviNEl4LHtNtpCaRZoaqMaAYycsU1eaxWhAw7LZeRIES2jA51p2TuBEZgNvO1eixMIrIF_vEVDZ0PUolxrj-kqKMyLLmGUto48DAWSyQ8W0O-DyQAEM_JlJ8rSwPUJtjbJW83oP6M/s1600/Cookies_Example.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijd-cviNEl4LHtNtpCaRZoaqMaAYycsU1eaxWhAw7LZeRIES2jA51p2TuBEZgNvO1eixMIrIF_vEVDZ0PUolxrj-kqKMyLLmGUto48DAWSyQ8W0O-DyQAEM_JlJ8rSwPUJtjbJW83oP6M/s1600/Cookies_Example.png" /></a></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjS1DEk341r7cj6HOir1T2WvFLIQnfzkU7f1KtuC54h1Xq2YqamBT77ZGSz3AMFs3kHMdjIsBre4LklR5PhCdl-gGCh-XMmai_-XQunnD4tBx4Vj0fIExnfHXUBhvIqCRE9K9pd9ZWWM0Y/s1600/Cookies_ASP.NET_Design_Page.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"></a></div><a name='more'></a>Create a new website in VS and add the following code that designs our aspx page with two text boxes and three buttons.<br />
<br />
<pre name="code" class="html"><%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" EnableEventValidation="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Cookies Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<b>Cookies Example</b>
<table cellpadding="2" cellspacing="2">
<tr>
<td colspan="2">
<asp:Label ID="lblInfo" runat="server" Text="Label" ForeColor="#993300"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="txtSetCookie" runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnSetCookie" runat="server" Text="Set Cookie" OnClick="btnSetCookie_Click"
Width="120px" />
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="txtGetCookie" runat="server" Enabled="False"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnGetCookie" runat="server" Text="Get Cookie" OnClick="btnGetCookie_Click"
Width="120px" />
</td>
</tr>
<tr>
<td>
&nbsp;
</td>
<td>
<asp:Button ID="btnDeleteCookie" runat="server" Text="Delete Cookie" OnClick="btnDeleteCookie_Click"
Width="120px" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
</pre>And it’s code behind <br />
<pre name="code" class="csharp">using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
HttpCookie cookie;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lblInfo.Visible = false;
}
else
{
lblInfo.Visible = true;
}
}
protected void btnSetCookie_Click(object sender, EventArgs e)
{
//Checks wheather the text box is empty or not
if (txtSetCookie.Text != null && txtSetCookie.Text != "")
{
cookie = new HttpCookie("MyCookie");
cookie.Value = txtSetCookie.Text;
cookie.Expires = DateTime.Now.AddMinutes(1);
Response.SetCookie(cookie);
lblInfo.Text = "Cookie created.";
txtSetCookie.Text = "";
}
else
{
lblInfo.Text = "Please enter cookie value.";
}
}
protected void btnGetCookie_Click(object sender, EventArgs e)
{
txtGetCookie.Text = "";
//Checks wheather the cookie is null or not
if (Request.Cookies["MyCookie"] != null)
{
txtGetCookie.Text = Request.Cookies["MyCookie"].Value;
}
else
{
lblInfo.Text = "Please set cookie.";
}
}
protected void btnDeleteCookie_Click(object sender, EventArgs e)
{
if (Request.Cookies["MyCookie"] != null)
{
Response.Cookies["MyCookie"].Expires = DateTime.Now.AddDays(-1);
lblInfo.Text = "Cookie deleted.";
txtGetCookie.Text = "";
}
else
{
lblInfo.Text = "Please set cookie.";
}
}
}
</pre></div>Chandra kaanthhttp://www.blogger.com/profile/10895546723390076324noreply@blogger.com0