A Place for C Sharpers/.Netters

I Will do coding till last moment of life-Kiran Patil

Archive for the ‘ASP.NET’ Category

Access Internet/Local Website from Your Windows Mobile Device Emulators

Posted by kiranpatils on November 19, 2009

Challenge

If you want to access any website Or You want your locally developed mobile application/web application to be viewed in Windows Device Emulator then you are at the right post.

Solution
  1. Download ActiveSync and install on your local machine where you would like to run Emulator. How to setup ActiveSync?
  2. Open Visual Studio and click on Tools | Connect to Device:

clip_image002

  1. Select Device emulator to run and say connect.

clip_image004

  1. It will open your device emulator:

clip_image006

  1. Verify that ActiveSync is up and running by its symbol in Notification Area.

clip_image008

  1. Open Device Emulator Manager

clip_image010

  1. Currently running Device Emulator Manager will be shown in Green.

clip_image012

  1. Right click and say “Cradle”

clip_image014

  1. As you click on Cradle ActiveSync window will popup. Select Guest Partnership[Or better do cancel it will by default to Guest Mode]. If your Emulator got synced with ActiveSync then Notification Icon will go Green.
  1. Now you are ready to access website.
    1. Internet – Enter your URL.

clip_image016

    1. Local – To Access your Local Website which is running on Cassini(Development Web Server). To access it you need to access it by IP ADDRESS:PORTNUMBER. Please note that you can’t access it by localhost:PORTNO Because localhost points to Emulator’s localhost and it will never go to your machine. You have to see both Emulator and your local machine as a two different devices even though they are running on same machine.

clip_image018

If this blog helped you. Say a big thanks to my friends who always give me a shout whenever they found something challenging like this and inspire me to learn a new things which finally inspires me to pen down on my blog and share with you :)

Happy Emulating!

Webliography

Setup ActiveSync

Nice blog – Thanks to this blog

If you have Windows Vista or Later

Accessing Device Emulator Without ActiveSync

Feedback

Posted in ASP.NET, Windows Mobile | Tagged: , , | Leave a Comment »

Delivering Multimedia

Posted by kiranpatils on November 16, 2009

Now a days I’ve started preparing for my next Microsoft Certification exam 70-547. And it’s going on really well as i am learning the things which i was knowing but now i am coming in to know the PRO. use of it.

Anyways today i am going to show you how you can deliver Multimedia on your website. Because in this era everyone needs to have it :)

But before we delve in coding part which we all developers love to do. Let’s take a look at some basic things which is good to know.

Non-Streaming :- The Audi/Video files must be downloaded completely before playback can begin.

Non-Streaming Audio Formats are waveform(.wav), sound(.snd), Unix Audio(.au), Audio Interchange File format(.aif.aiff,.aifc)

Non-Streaming Video Formats are Audio-Video Interleaved(.avi)

Streaming :- The Audi/Video files can started playback and files keep downloading itself in background. This one gives better user experience.

Streaming Audio Formats are Moving Pictures Experts Group standard 1, Layer 1,2,3 (.mpa,.mp2,.mp3), Windows Media Audio(.wma)

Streaming Video Formats are Moving Pictures Experts Group standard 1 (.mpg,.mpeg,.mpv,.mpe), Windows Media Video (.wmv)

Hooh..too much theory. Let’s start how we can deliver multimedia on our page.

There are two ways of doing this:

1. Embedded Playback : you can embed Windows Media Player on your page directly. But client should have installed Windows Media Player on his/her computer to view your multimedia files.

The code looks like as below:

<div class="csharpcode"><html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
 <title>MultiMedia Page</title>

 <script type="text/javascript" language="javascript">
 function StartPlayer()
 {
 //Using DOM play your MultiMedia File
 document.player1.URL = "Resources/vande.avi";
 }

 function StartStreamingPlayer()
 {
 document.player1.URL = "Resources/Intro_to_ASP_.NET_3.5.wmv";
 }
 function StopPlayer()
 {
 document.player1.controls.stop();
 }

 function PausePlayer(buttonPauseOrPlay)
 {
 if(buttonPauseOrPlay.value == "Pause")
 {
 document.player1.controls.pause();
 buttonPauseOrPlay.value = "Play";
 }
 else
 {
 document.player1.controls.play();
 buttonPauseOrPlay.value = "Pause";
 }
 }
 </script>

</head>
<body>
 <form id="form1" runat="server">
 <div>
 <object id="player1" height="400" width="590" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6">
 </object>
 <br />
 <br />
 <input type="button" name="btnPlay" value="Start Non-Streaming File(.avi)" onclick="StartPlayer()" />
 <input type="button" name="btnPlay" value="Start Streaming File(.wmv)" onclick="StartStreamingPlayer()" />
 <input type="button" name="btnStop" value="Stop" onclick="StopPlayer()" />
 <input type="button" name="btnPause" value="Pause" onclick="PausePlayer(this)" />
 <br />
 UI Mode :
 <select id="selUIMode" onchange="this.document.player1.uiMode = selUIMode.value;">
 <option value="invisible">Invisible</option>
 <option value="none">None</option>
 <option value="mini">Mini Player</option>
 <option value="full">Full Player</option>
 </select>
 </div>
 </form>
</body>
</html></div>
<div class="csharpcode">

Output of the above piece of code is as below: It will be real fun when you try on your own :)

Media Player In Browser

In above code the main things is :


 </span><object id="player1" height="400" width="590" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6">
 </object>

<pre> 

2. Playing file with an External Player is easy. Just following piece of code does that for you:

this.document.location.replace(FILENAME.wma);

Hope you’ve enjoyed it!

Happy Video Playing!

Posted in ASP.NET | Tagged: | Leave a Comment »

Showing Default Date and Visible Date with Calendar control

Posted by kiranpatils on September 2, 2009

Challenge:

if you are using Calendar control to show Birthday of your friend. So, when you run the application it will show whose b’day is coming in this month or next month or next month …. So, you can be ready for a treat :) . The Challenge here is how to select that particular date and how to make it visible while user runs the application?

Solution:

You can set SelectedDate of Calendar Control to show date selected like this:

DateTime dtBirthday = getUpcomingBirthday(); //assume this method returns upcoming b’day in DateTime

calBirthday.SelectedDate = dtBirthday.Date; //use Date here else it won’t work

You can set VisibleDate of Calendar Control to show that date:

calBirthday.VisibleDate= dtBirthday.Date; //use Date here else it won’t work

Here DateTime.Date part it too important. Else it won’t work. Do you know why? i know but it’s homework for you guys :)

Cheers

Posted in ASP.NET, ASP.NET Controls | Leave a Comment »

Form Reset With Validation Control

Posted by kiranpatils on September 2, 2009

Challenge:

Reset button won’t clear validation control messages.

Solution:

For that you have to write JS onclick of reset button and do hide it like this:

document.getElementById(“regurlarexpressionID”).style.visibility = “hidden”;

See this link:

http://forums.asp.net/t/567612.aspx

Posted in ASP.NET, ASP.NET Controls | Leave a Comment »

Programmatically Posting Data to ASP .NET Web Applications

Posted by kiranpatils on September 2, 2009

Usually what we do. We open a page and fill up a form and submit a form data by pressing “Submit” Button. And after that we get some data/response  as per the logic.  But how to do this programmatically?? — That’s what we have been doing since so long..we faced too many challenges and finally we made it working. You also want to do the same then here we go…

First follow this nice article :

http://dotnet.sys-con.com/node/45127

if you follow all the steps and also COPY-PASTE the whole code given below:

http://gemsres.com/photos/story/res/45127/source.html

IT WON’T WORK

As someone has already commented on that post — That it throws Internal Server Error(500). But unfortunately no solution has been answered. But don’t worry. i’m here to answer that :)

Okay, The problem is in your target ASPX page — Means ASPX Page which has form tag. it should have event validation false:

<%@ Page EnableEventValidation=”false” %>

That’s it. It should work now. And if it dosen’t works then try to debug using Fiddler — Really great tool

Okay, So in short to post form data programmatic ally using ASP.NET following steps are required.

1. ViewState Must be passed.

2. In Target page — enableeventvalidation – false.

3. Fiddler must need to install to see what’s going on :)

Webliography

http://xneuron.wordpress.com/2007/12/05/programmatically-post-a-form-in-aspnet/

http://schleichermann.wordpress.com/2009/07/13/asp-net-programmatically-submit-form-post/

Hope this helps :)

Posted in ASP.NET | Leave a Comment »

How to use asp:table in Mobile Page

Posted by kiranpatils on July 16, 2009

Challenge:

How to use asp:table in Mobile Page?

Or if you got an error

‘asp:Table’ cannot be a top-level form element.

Then solution is here:

Solution:

<%@ Page Language="C#" AutoEventWireup="true" Inherits="Mobile2.Default1" Codebehind="Default1.aspx.cs" %>

<%@ Register TagPrefix="mobile" Namespace="System.Web.UI.MobileControls" Assembly="System.Web.Mobile" %>
<html xmlns="http://www.w3.org/1999/xhtml" >

<body>

<mobile:Form id="Form1" runat="server">
<mobile:panel id="pnlMobile" runat="server">
<asp:Table id ="mainTable" runat="server">
<asp:TableRow>
<asp:TableCell>
<mobile:Label ID="Label1" runat="server">Demo</mobile:Label>
</asp:TableCell>
<asp:TableCell>
<mobile:Label ID="Label2" runat="server">Demo</mobile:Label>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</mobile:panel>
</mobile:Form>
</body>
</html>

Posted in ASP.NET | Leave a Comment »

Parses a query string into a NameValueCollection

Posted by kiranpatils on July 7, 2009

This post don’t need big explanation. Great Work by microsoft guys to manipulate QS in OOPs Way, Have a look at here:

http://msdn.microsoft.com/en-us/library/ms150046.aspx

Keep it up!!

Posted in ASP.NET | Leave a Comment »

How can i click button programmatically?

Posted by kiranpatils on July 7, 2009

Challenge:

Q: How can i click button programmatically?

Okay, Some of the experts will say call btn_click(sender,eventargs); You are right expert.. But i would like to do as below:
I have one User control which has : Two Textbox and one button(btnSave) — and one click event associated with it(btnSave_Click).
I have used this control on my page say Default.aspx — Now, From Default.aspx. I would like to call btnSave_Click on one of my button kept on Default.aspx say FinalSave. So experts how can i do this? :(

Solution:

Okay, This simple guy have a solution it is as follow:

1. Find button control using FINDCONTROL Method.
Button fondButton = UserControl1.FindControl(“btnSave”);

2. Call Button click using following way:
((IPostBackEventHandler)fondButton).RaisePostBackEvent(null);

isn’t it a cool way to call button click??

Thanks to this guy for nice post :
http://www.netsplore.com/PublicPortal/Blog/tabid/284/EntryID/11/Default.aspx

Posted in ASP.NET | Leave a Comment »

New Password should not be same as old password with ChangePassword Control

Posted by kiranpatils on June 9, 2009

Challenge:

You have dropped ChangePassword Control which allows user to change password. User will provide Current Password, New Password. But you want that New Password can’t be same as Old password..for security reason…how can you do this?

Solution:

1. Hook onchangingpassword-[Occurs before the password for a user account is changed by the membership provider]. event of ChangePassword Control:

<asp:ChangePassword ID="ChangePassword1" runat="server"
onchangingpassword="ChangePassword1_ChangingPassword">
</asp:ChangePassword>

2. In Handler compare password:

protected void ChangePassword1_ChangingPassword(object sender, LoginCancelEventArgs e)
{
if (<span style="color:#000000;">ChangePassword1.CurrentPassword == ChangePassword1.NewPassword)</span>
{
//TODO Show Message
//Cancel the event

e.Cancel = true;//set it to true else it will show the msg as well as change the password also
}
}

Happy Programming!!:)

Posted in ASP.NET, ASP.NET Controls | 5 Comments »

Logout problem or Back Button Problem after signout

Posted by kiranpatils on June 8, 2009

Challenge:

1. You are using ASP.NET’s Form Authentication

2. All your pages[Except Login :) page] should be accessible only to authenticated users?

3. You have signout/Logout button where you are doing like this:

4. Now user is logged out and viewing the Login page and if he/she tries to go Back using Back Button of Browser…they can access it [Hoohh..its loophole] Or if some expert user is using your application directly plays with address bar and say /MyAccountSummary.aspx[Nooooooo] and he/she can access the page..

Now you must be wondering that i have logged out the user using standard ASP.NET Methods then also how can user access the secure items?? Don’t get excited and say it is “BUG IN ASP.NET”…[Pls note my words there are so so so less chance you will find a bug in Microsoft's Framework]..So before pointing them out we should have our fundas clear :)

Solution:

Okay, let me tell you why the strange behavior is..It is because of Client Side Browser caching…Browser Guys to improve the performance they cache the pages at client machine..So, user is accessing whatever secured item after Logout it is coming from cache….server is not aware about it[else he is smart enough to stop this:)] …So, Let’s stop this Client Side Browser Caching by following Code:

you can try this to put in your pages: Page_Load – which you don’t want to be cached by client side:

Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();

That’s it :)

Before going for celebration Please see some of my best practices

As per the OOPs Guy i suggest that create a page/Class[NoCachePage.CS] known as “NoCachePage” which derives directly from System.Web.UI.Page looks like as following:

/// <summary>
/// Author : Kiran Patil
/// Date   : 08-June-2009
/// Description: This Page will be used to act
///              as a base page for all the pages
///              which should not get cached at client
///              side
/// </summary>
public partial class NoCachePage : System.Web.UI.Page
{
    /// <summary>
    /// This function will be used to load
    /// initial data for a page
    /// </summary>
    ///
<param name="sender">Page</param>
    ///
<param name="e">EventArguments</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetNoStore(); 

    }
}

Now all your applications page which should not get cached at Client Side derive it from NoCachePage. Looks like following:

/// <summary>
/// Author : Kiran Patil
/// Date   : 08-June-2009
/// Description: This Page will be used to load
///              Account summary of an user
/// </summary>
public partial class AccountSummaryPage : NoCachePage
{
    /// <summary>
    /// This function will be used to load
    /// initial data for a page
    /// </summary>
    ///
<param name="sender">Page</param>
    ///
<param name="e">EventArguments</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        //secure code goes here..I can give you
        //guarantee that it is secure now..
    }
}

HTH

Please don’t forget to have a look at this link:
http://forums.asp.net/t/1432437.aspx

public partial class NoCachePage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
}

Posted in ASP.NET | Leave a Comment »