A Place for C Sharpers/.Netters

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

Archive for the ‘ASP.NET Controls’ 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 »

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 »

<tbody>using Table control

Posted by kiranpatils on May 30, 2009

Challenge:

How I generate <tbody> tag using Table, TableRow, etc … controls ?

I want to generate table o/p like this:

<table border="1">
  <thead>
    <tr>
      <th>Month</th>
      <th>Savings</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>January</td>
      <td>$100</td>
    </tr>
    <tr>
      <td>February</td>
      <td>$80</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>Sum</td>
      <td>$180</td>
    </tr>
  </tfoot>
</table>

Src : http://www.w3schools.com/TAGS/tag_tbody.asp

I want to create table like this from my code

behind using .net f/w classes.

Solution:

TableRow.TableSection is the solution for it:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.tablerow.tablesection.aspx

for whatever row you create just provide it’s TableSetion property to appropriate section e.g Body,head etc.

Hope this helps

Happy Programming!!

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

But how do I run JavaScript onLoad? with MasterPages

Posted by kiranpatils on March 11, 2008

Today, I came across good situation after a page load i want to check that some Hidden variable contains what?. But how can i add javascript which runs Onload..some sharp people says that add it in javascript’s onLoad method. But hey sharp i am using Masterpages. So i can’t add it in Master page’s Body tag. Because so many of my pages referes that master page and don’t want that functionality for all pages i want on one specific page. Really good problem…don’t worry i have its solution also.

Problem

i want to call “LoadData” function of javascript OnLoad event of page with Master Page. I have my.js file which holds this function

Solution

ClientScript.RegisterStartupScript(this.GetType(), “LoadData“, “LoadData();”, true);

     Explanation:

ClientScript = Page.ClientScript Property Gets a ClientScriptManager object used to manage, register, and add script to the page. [http://msdn2.microsoft.com/en-us/library/system.web.ui.page.clientscript.aspx]

RegisterStartupScript : Registers the startup script with the Page object using a type, a key, a script literal, and a Boolean value indicating whether to add script tags.

ClientScriptManager..::.RegisterStartupScript Method (Type, String, String, Boolean) [http://msdn2.microsoft.com/en-us/library/z9h4dk8y.aspx]

This script will run after Page-Load event so [MSDN says if run before page_load] . But working for me

LoadData = Key to register function

LoadData();=function to call

true = Add script tag or not [NOTE: Must do this to true] .

Now put anything in LoadData() function under .js file that’s it.

Another Problem and its solution:

I want to take confirmation from User Yes/no and based on input i need to proceed but i forgot it how to do..the method is use confirm(“MESSAGE”) in javascript returns True/false. for example:

var answer = confirm(“YES/NO?”);
if(answer)
alert(“YES”);
else
alert(“NO”);

Enjoy!!

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

Formatting Date and Time in GridView

Posted by kiranpatils on March 10, 2008

Today, I am playing with Gridview and have to show data in it from SQL SERVER 2005. it has a field called as “JoiningDate” bounded with column “JoiningDate”. it was working fine it was showing date in the format like this: 2007-11-27 10:00:00.000 but i want to show it like this “11 Dec 2007″. I found it from MSDN. so here it is!!

Old Code[O/P=2007-11-27 10:00:00.000]:

<asp:BoundField DataField=”JoiningDate” HeaderText=”JoiningDate” / >

New one [O/P = 11 Dec 2007]

<asp:BoundField DataField=”JoiningDate” HeaderText=”JoiningDate” DataFormatString=”{0:dd MMM yyyy}” HtmlEncode=”False”/ >

Explanation

Two main tags helped me too achieve this:

1. DataFormatString: you can specify format to display string.

dd = Date

MMM = Month

yyyy = year

2. HtmlEncode: the field are HTML Encoded before they shown to bound field.Default true

NOTE: if you don’t do HTMLEncode=”False” .it won’t work. Because default it true.

References:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconstandarddatetimeformatstrings.asp

http://msdn2.microsoft.com/en-us/library/system.datetime.tostring(VS.71).aspx

Hope it will help you too as it helped me..

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