A Place for C Sharpers/.Netters

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

Archive for the ‘ASP.NET’ Category

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 »

Stop Wrapping using NOWRAP

Posted by kiranpatils on June 6, 2009

Challenge:

I am using repeater in that i have one column named as “EmployeeName” in which i have one record “Bill Gates” but when i view the page it shows in two lines..

Bill

Gates

I would like to see “Bill Gates” in one line..How can i do this?

Solution:

<TD NOWRAP>Bill Gates</TD>

That’s it for whatever row you want to stop wrapping add attribute NOWRAP.

Webliography:

http://www.htmlcodetutorial.com/tables/_TD_NOWRAP.html

Programming with Fun! :)

Posted in ASP.NET | Leave a Comment »

How to Access MailSettings Configuration Settings Programmatically

Posted by kiranpatils on June 5, 2009

Challenge:

I have my mail settings in web.config file in mailSettings section..like as shown below:

<system.net>
 <mailSettings>
 <smtp from=myEmail@mySite.com>
 <network defaultCredentials="true" host="mail.myhost.com" password="xxx" userName="xxx"/>
 </smtp>
 </mailSettings>
</system.net>

i like to access it in my code. How can i do that?

Solution:

In .NET World nothing is impossible[As per my thinking in real world too :) ].

 //get current application's configuration file
 Configuration configurationFile =     WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
//access mailsetting section and cast it to MailSettingsSectionGroup Class
MailSettingsSectionGroup     
mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;

 if (mailSettings != null)
 {
 string form = mailSettings.Smtp.From;
 int port = mailSettings.Smtp.Network.Port;
 string host = mailSettings.Smtp.Network.Host;
 string password = mailSettings.Smtp.Network.Password;
 string username = mailSettings.Smtp.Network.UserName;
 }

Hope this helps

Happy programming!!

Posted in ASP.NET | Leave a Comment »