Thursday, March 29, 2012

Difference in various .NET Frameworks

Below is the difference between Microsoft .net frameworks


Creating Sharepoint Lists service Proxy Class.



1. Browse the service of your sharepoint site using :

http://SiteName:portnumber/_vti_bin/lists.asmx

2. Now to create a wsdl service proxy class:
    1. Run Command prompt and go to path "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin"

then type wsdl.exe followed by the service url as shown below

"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin>wsdl.exe http://SiteName:PortNumber/_vti_bin/lists.asmx /out:FileName.cs"

3. The file will be created in "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin".                  

Copy the file to your solution and use it.






Sunday, March 25, 2012

File Upload to save file to server location


Below code help us to create/move files from one folder to another or from client machine to server machine using c#.

Below is the sample folder structure

Folder Structure :

If above structure is used then no need to read it from web.config file. 
Attachment folder is a folder in which files are stored.

.aspx file snippet for File upload control:
<td align="center" >
        <span>Scanned Documents</span>
        <asp:FileUpload ID="fuScanedDocument" runat="server" /><span> (File Size Limit : 2 MB)</span>
</td>



Code File:

protected void btnUpload_Click(object sender, EventArgs e)
    {

        if (fuScanedDocument.HasFile)
        {
            if ((fuScanedDocument.PostedFile != null) && (fuScanedDocument.PostedFile.ContentLength > 0))
            {
                
//Below code gets the server location
                string SaveLocation = Server.MapPath("Attachments"); 
                try
                {
                    if (Directory.Exists(SaveLocation)
                    {
                        string p = Server.MapPath("Attachments"

// Below code saves the file on specified location
                        fuScanedDocument.PostedFile.SaveAs( SaveLocation   );
                                               
                    }
                }
             
           }
       
        }
       
    }

Thursday, March 15, 2012

Page Life Cycle


Understanding the Page Life Cycle can be very important as you begin to build Pages with MasterPages and UserControls.
Does the Init event fire first for the Page, the MasterPage or the UserControl? 
What about the Load event?
If you make an incorrect assumption about the sequence that these events fire, then you may end up with a page that simply doesn't behave the way you had anticipated.
By running a simple test, we can see exactly when each event fires.  Our test setup is composed of a Page, MasterPage, UserControl, Nested UserControl and Button control as follows:
  • <div mce_keep="true">The Page is tied to the MasterPage</div>
  • <div mce_keep="true">The UserControl is on the Page</div>
  • <div mce_keep="true">The Nested UserControl is on the UserControl</div>
  • <div mce_keep="true">The Button is on the Nested UserControl.</div>
  • <div mce_keep="true">Clicking the Button calls the Page.DataBind method</div>
Each event on these controls has been set to call Debug.WriteLine as each event is raised.  In addition to the events that get raised for regular pages, I've also set up an HttpModule and wired up all of those events as well. The results in the Debug output window of running this page and Clicking the Button are as follows:
BeginRequest - HttpModule
AuthenticateRequest - HttpModule
PostAuthenticateRequest - HttpModule
PostAuthorizeRequest - HttpModule
ResolveRequestCache - HttpModule
PostResolveRequestCache - HttpModule
PostMapRequestHandler - HttpModule
AcquireRequestState - HttpModule
PostAcquireRequestState - HttpModule
PreRequestHandlerExecute - HttpModule
PreInit - Page
Init - ChildUserControl
Init - UserControl
Init - MasterPage
Init - Page
InitComplete - Page
LoadPageStateFromPersistenceMedium - Page 
ProcessPostData (first try) - Page
PreLoad - Page
Load - Page
Load - MasterPage
Load - UserControl
Load - ChildUserControl
ProcessPostData (second try) - Page
RaiseChangedEvents - Page
RaisePostBackEvent - Page
Click - Button - ChildUserControl
    DataBinding - Page
    DataBinding - MasterPage
    DataBinding - UserControl
    DataBinding - ChildUserControl
LoadComplete - Page
PreRender - Page
PreRender - MasterPage
PreRender - UserControl
PreRender - ChildUserControl
PreRenderComplete - Page
SaveViewState - Page
SavePageStateToPersistenceMedium - Page
SaveStateComplete - Page
Unload - ChildUserControl
Unload - UserControl
Unload - MasterPage
Unload - Page
PostRequestHandlerExecute - HttpModule
ReleaseRequestState - HttpModule
PostReleaseRequestState - HttpModule
UpdateRequestCache - HttpModule
PostUpdateRequestCache - HttpModule
EndRequest - HttpModule
PreSendRequestHeaders - HttpModule
PreSendRequestContent - HttpModule 

Wednesday, March 14, 2012

Jquery: Working with ASp.net GridView Control


Highlighting rows/cells of a GridView on hover

<form id="form1" runat="server">
<div align="center">
<fieldset style="width:400px;height:230px;">
<table border="0" cellpadding="3" cellspacing="3">
<tr><td colspan="2" class="header">BOOK CATALOG</td></tr>
<tr>
<td colspan="2">
<asp:GridView ID="GridView1" SkinID="Professional"
runat="server">
</asp:GridView>
</td>
</tr>
</table>
</fieldset>
</div>
</form>




style:



.highlight
{
background-color:#9999FF;
}



<script language="javascript" type="text/javascript">
$(document).ready(function() {
$("#<%=GridView1.ClientID%> tr").hover(
function() {
$(this).addClass("highlight");
},
function() {
$(this).removeClass("highlight");
});
});
</script>


The preceding jQuery solution can be modified slightly to achieve the same effect on a
GridView cell instead of a GridView row.


$("#<%=GridView1.ClientID%> td").hover(
function() {
$(this).addClass("highlight");
},
function() {
$(this).removeClass("highlight");
});



Retrieving the content of a GridView cell on click

<form id="form1" runat="server">
<div align="center">
<fieldset style="width:400px;height:230px;">
<table border="0" cellpadding="3" cellspacing="3">
<tr><td colspan="2" class="header">BOOK CATALOG</td></tr>
<tr><td colspan="2">
<asp:GridView ID="GridView1" SkinID="Professional"
runat="server">
</asp:GridView>
</td></tr>
</table>
</fieldset>
<br />
<div id="message"></div>
</div>
</form>

Style:

.highlight
{
background-color:#9999FF;
}



<script language="javascript" type="text/javascript">
$(document).ready(function() {
$("#<%=GridView1.ClientID%> tr").filter(":not(:has(table,
th))").click(function(e) {
var $cell = $(e.target).closest("td");
$("#<%=GridView1.ClientID%> td").
removeClass("highlight");
$cell.addClass("highlight");
$("#message").text('You have selected: ' + $cell.
text());
});
});
</script






JQuery: Working with ASP.NET Controls



Textbox:
<form id="form1" runat="server">
<p></p>
<div align="center">
<fieldset style="width:400px;height:80px;">
<p><asp:TextBox ID="TextBox1" width="200px"
CssClass="defaultText" ToolTip="Enter your search keyword here"
runat="server"></asp:TextBox>
<asp:Button ID="btnSubmit" Text="SEARCH" runat="server"
/></p>
</fieldset>
</div>
</form>


<script type="text/javascript">
$(document).ready(function() {
var searchBox = $('#<%=TextBox1.ClientID%>');
searchBox.focus(
function() {

if (searchBox.val() == this.title) {
searchBox.removeClass("defaultText");
searchBox.val("");
}



});
searchBox.blur(
function() {
if (searchBox.val() == "") {
searchBox.addClass("defaultText");
searchBox.val(this.title);
}
});
searchBox.blur();
});
</script>



Auto focus on the first TextBox and tab on the Enter key

<form id="form1" runat="server">
<div align="center">
<fieldset style="width:400px;height:200px;">
<table cellpadding="3" cellspacing="3" border="0">
<tr><td>
<asp:Label ID="lblName" Text="Name: " runat="server"></
asp:Label></td>
<td><asp:TextBox ID="txtName" Width="200px" runat="server"></
asp:TextBox></td></tr>
<tr><td><asp:Label ID="lblAddress" Text="Address: "
runat="server"></asp:Label></td>
<td><asp:TextBox ID="txtAddress" Width="200px"
runat="server"></asp:TextBox></td></tr>
<tr><td><asp:Label ID="lblContact" Text="Contact Number: "
runat="server"></asp:Label></td>
<td><asp:TextBox ID="txtContact" Width="200px"
runat="server"></asp:TextBox></td></tr>
<tr><td><asp:Label ID="lblEmail" Text="Email: "
runat="server"></asp:Label></td>
<td><asp:TextBox ID="txtEmail" Width="200px" runat="server"></
asp:TextBox></td></tr>
<tr><td></td><td><asp:Button ID="btnSubmit" Text="SUBMIT"
runat="server" />
<asp:Button ID="btnReset" Text="RESET" runat="server" />
</td></tr>
</table>
</fieldset>
</div>
</form>


<script type="text/javascript">
$(document).ready(function() {
$('input:text:first').focus();
$('input:text').bind("keydown", function(e) {
if (e.which == 13) { //Enter key
e.preventDefault(); //to skip default behavior of
the enter key
var nextIndex = $('input:text').index(this) + 1;
$('input:text')[nextIndex].focus();
}
});
$('#btnReset').click(
function() {
$('form')[0].reset();
});
});
</script>


Disallowing cut/copy/paste operations on a TextBox:

<form id="form1" runat="server">
<div align="center">
<fieldset style="width:400px;height:180px;">

<table cellpadding="3" cellspacing="3" border="0">
<tr><td colspan="2" class="header">CHANGE PASSWORD</td></tr>
<tr><td> <asp:Label id="lblCurrentPwd" Text="Current Password:
" runat="server"/></td>
<td> <asp:TextBox ID="txtCurrentPwd" Width="200px"
runat="server" TextMode="Password"></asp:TextBox></td></tr>
<tr><td><asp:Label id="lblNewPwd" Text="New Password:"
runat="server"/></td>
<td><asp:TextBox ID="txtNewPwd" Width="200px" runat="server"
TextMode="Password"></asp:TextBox></td></tr>
<tr><td><label id="lblConfirmNewPwd" runat="server">Confirm
New Password:</label></td>
<td><asp:TextBox ID="txtConfirmNewPwd" Width="200px"
runat="server" TextMode="Password"></asp:TextBox></td></tr>
<tr><td></td><td><asp:Button ID="btnSubmit" runat="server"
Text="SUBMIT" />
<asp:Button ID="btnReset" runat="server" Text="RESET" /></
td></tr>
</table>
</fieldset>
</div>
</form>




<script type="text/javascript">
$(document).ready(function() {
$('#<%=txtNewPwd.ClientID%>').bind('cut copy paste',
function(e) {
e.preventDefault();
alert("Cut / Copy / Paste disabled in this textbox");
});
$('#<%=txtConfirmNewPwd.ClientID%>').bind('cut copy
paste', function(e) {
e.preventDefault();
alert("Cut / Copy / Paste disabled in this textbox
too!");
});
});
</script>





Displaying selected items of a CheckBoxList:


<form id="form1" runat="server">
<div align="left">
<fieldset style="width:400px;height:150px;">
<p>Different types of packages available:</p>
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem Text="Standard" Value="1"></asp:ListItem>
<asp:ListItem Text="Silver" Value="2"></asp:ListItem>
<asp:ListItem Text="Gold" Value="3"></asp:ListItem>
<asp:ListItem Text="Premier" Value="4"></asp:ListItem>
</asp:CheckBoxList>
</fieldset>
<br />
<div id="message"></div>
</div>
</form>





<script type="text/javascript">
$(document).ready(function() {
$('#<%=CheckBoxList1.ClientID%>').click(
function() {
var str = "";
$('#<%=CheckBoxList1.ClientID%>
input[type=checkbox]:checked').each(function() {
str = str + " " + $(this).next().text();
});
$('#message').text(str);
});
});
</script>


Selecting/deselecting all items in CheckBoxList:


<form id="form1" runat="server">
<div align="left">
<fieldset style="width:400px;height:170px;">
<p>Different types of packages available:</p>
<asp:CheckBox ID="chkSelectAll" runat="server" Text="Select
All" />
<asp:CheckBoxList ID="chkList" runat="server">
<asp:ListItem Text="Standard" Value="1"></asp:ListItem>

<asp:ListItem Text="Silver" Value="2"></asp:ListItem>
<asp:ListItem Text="Gold" Value="3"></asp:ListItem>
<asp:ListItem Text="Premier" Value="4"></asp:ListItem>
</asp:CheckBoxList>
</fieldset>
</div>
</form>


<script type="text/javascript">
$(document).ready(function() {

$('#chkSelectAll').click(function() {
$("#<%=chkList.ClientID%> input[type='checkbox']").
attr('checked', $('#chkSelectAll').is(':checked'));
}
);
});
</script>










Getting selected text and value from 
DropDownList


<form id="form1" runat="server">
<div align="center">
<fieldset style="width:400px;height:80px;">
<p>Different types of packages available:</p>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="--Please Select--" Value=""></
asp:ListItem>
<asp:ListItem Text="Standard" Value="1"></asp:ListItem>
<asp:ListItem Text="Silver" Value="2"></asp:ListItem>
<asp:ListItem Text="Gold" Value="3"></asp:ListItem>
<asp:ListItem Text="Platinum" Value="4"></asp:ListItem>
</asp:DropDownList>
</fieldset>
</div>
<br />
<div align="center" id="message"></div>
</form>


<script type="text/javascript">
$(document).ready(function() {
$('select[id$=<%=DropDownList1.ClientID%>]').bind("keyup
change", function() {
if ($(this).val() != "")
$('#message').text("Text: " + $(this).
find(":selected").text()
+ ' Value: ' + $(this).val());
else
$('#message').text("");
});
});
</script>






Appending items at runtime to a 
DropDownList


<form id="form1" runat="server">
<div align="left">
<fieldset style="width:350px;height:150px;">
<div id="contentArea">
<p>Select the author as required:</p>
<table cellpadding="0" cellspacing="0" border="0">
<tr><td>&nbsp;</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="" Text="--Please Select--"></
asp:ListItem>
<asp:ListItem Value="MA" Text="Mitch Albom"></
asp:ListItem>
<asp:ListItem Value="PC" Text="Paulo Coelho"></
asp:ListItem>
<asp:ListItem Value="JA" Text="Jane Austen"></
asp:ListItem>
</asp:DropDownList>
</td></tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr><td>&nbsp;</td>
<td>
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem Value="" Text="--Please Select--"></
asp:ListItem>
</asp:DropDownList>
</td></tr>
</table>
</div>
</fieldset>
</div>
</form>


<script type="text/javascript">
$(document).ready(function() {
$("select[id$=DropDownList1]").bind("change", function() {
$("select[id$=DropDownList2] option").remove();
$("select[id$=DropDownList2]").append("<option value=''>--
Please Select--</option>");
if ($(this).val() == "MA") {
$("select[id$=DropDownList2]").append("<option
value='MA1'>Have a Little Faith</option>");
$("select[id$=DropDownList2]").append("<option
value='MA2'>Tuesdays with Morrie</option>");
$("select[id$=DropDownList2]").append("<option
value='MA3'>The Five People You Meet in Heaven</option>");
}
if ($(this).val() == "PC") {
$("select[id$=DropDownList2]").append("<option
value='PC1'>The Alchemist</option>");
$("select[id$=DropDownList2]").append("<option
value='PC2'>By the River Piedra I Sat Down and Wept</option>");
$("select[id$=DropDownList2]").append("<option
value='PC3'>The Pilgrimage</option>");

}
if ($(this).val() == "JA") {
$("select[id$=DropDownList2]").append("<option
value='JA1'>Pride and Prejudice</option>");
$("select[id$=DropDownList2]").append("<option
value='JA2'>Emma</option>");
$("select[id$=DropDownList2]").append("<option
value='JA3'>Mansfield Park</option>");
}
});
});
</script>