Showing posts with label asp.net controls. Show all posts
Showing posts with label asp.net controls. Show all posts

Format Date Time using BoundColumn / ButtonColumn in DataGrid

A very common desire is to set a column of a DataGrid/GridView to display just the month, day and year (or other custom formats) for a dateTime field.

Below is an example for the same,

The first column with header 'DateTime 1' is a BoundColumn which uses DataFormatString="{0:yyyy.MM.dd}". This will format a date time string of 28-10-2011 12:52:00 to 2011.10.28.

The same can be used with ButtonColumn, as shown here the date time string of 28-10-2011 12:52:00 is converted to a format of 2011.10.28 12:52

<asp:DataGrid ID="abc" CssClass="default" DataKeyField="Id"  
OnItemCommand="myCommand" AutoGenerateColumns="false" Runat="Server" > 

<Columns> 

<asp:BoundColumn HeaderText="DateTime 1" DataField="dt1"  
DataFormatString="{0:yyyy.MM.dd}"  /> 

<asp:ButtonColumn HeaderText="DateTime 2" 
 DataTextField="dt2" DataTextFormatString="{0:yyyy.MM.dd hh:mm}"  
ButtonType="LinkButton" CommandName="Select" runat="server"/> 

</Columns> 

</asp:DataGrid>

Using of update panel (UpdatePanel) of ajax in asp.net

In  this tutorial i will explain how can we use UpdatPanel control which you can find in the section Ajax Extensions in the Toolbox...
UpdatPanel is used if you want to do partial post back i.e. if we want to post back only some part of the page & not whole page.
For example take a example of taking two numbers in the text box & showing it in the third text box on button click. As it is very easy you can create this of your own. create this all controls in the UpdatPanel.
Remember to put ScriptManager Control before the UpdatPanelas it is required to run any java script of Ajax.
Now take one more text box out side the UpdatPanel just to ensure that partial post back is occuring.
Your code will look something like this....


<div style="background-color:Gray;width:400px;padding:10px 0px 10px 10px;">
  <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
      <ContentTemplate>
       First Number : <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
       <br />
       Second Number : <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
       <br />
       <asp:Button ID="Button1" runat="server" Text="Add" onclick="Button1_Click" />
       <br />
       Addition :<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
      </ContentTemplate>
    </asp:UpdatePanel>
</div>
<div style="margin-top:50px;background-color:Gray;width:400px;padding:10px 0px 10px 10px;">
  <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</div>
 
 
& button click code would be something like this ... 
 
    protected void Button1_Click(object sender, EventArgs e)
    {
        int a = Convert.ToInt32(TextBox1.Text);
        int b = Convert.ToInt32(TextBox2.Text);
        TextBox3.Text = (a + b).ToString();
    }
 
 
Now run your application.....
When page opens ...write something in the textbox which is outside the update panel ....
 
& then put some numbers in the first & second text box & click on add .....
Post back will happen only for that portion & addition will come on the third textbox .... 
But you can see that text written in the last textbox is as it is as only controls inside had a post back & not whole page...
 
UpdatePanel has two properties UpdateMode &  ChildrenAsTriggers .
 
UpdateMode:
It has two values
1)Always: It indicates that content of the each control in side that UpdatePanel should be updated on each post back which originates from the page.
2)Conditional:  It indicates that control should be updated only on the post back of specific controls.

ChildrenAsTriggers:
 When this property is set to true then any updates triggered by the nested UpdatePanel will also trigger an update to the parent UpdatePanel.

How to show multiple columns in the single dropdown list using sqlreader?

Here i will show you how can u bind the multiple columns ti the single dropdown list..

Steps:
Add dropdown list from toolbox.
Now bind the data at the page load event of the page.

protected void Page_Load(object sender, EventArgs e)
    {
       string connectionString = 
"Data Source=VRAKSH-VAIBHAVS;Initial Catalog=AdventureWorks;Persist Security Info=True;User ID=sa;Password=data#123";
       SqlConnection con = new SqlConnection(connectionString);
       SqlCommand cmd = new SqlCommand 
("select City + ' ' + PostalCode as PCity from Person.Address", con);
       con.Open();
       SqlDataReader rd;
       rd = cmd.ExecuteReader();
        DataSet ds = new DataSet();
        DropDownList1.DataSource = rd;
        DropDownList1.DataValueField = "PCity";
       DropDownList1.DataBind();
        con.Close();
    }
 
 
 Remember to bind the data source to the dropdown list .....
& also bind the column to the drop down list like i did i.e.
DropDownList1.DataValueField = "PCity"; 
 
Like this you can bind multiple columns to the dropdown list .... 
 

Converting normal textbox into password textbox on clicking on the textbox

In this tutorial i will show how can u show the text in the textbox which is having TextMode=password but when clicked on that textbox it converts it to the password textbox.
Steps:
Take one textbox & set its TextMode to password.
now go to source mode of visual studio write this in the textbox

onfocus="if(this.value =='PASSWORD')this.value='';" onblur="if(this.value==''){this.value='PASSWORD';this.TextMode='SingleLine';}"
After adding this what happens is .....
It will initially show text "PASSWORD".
After that when you click on it ..it will be converted password textbox which will show text as "*"

your textbox markup should look something like this....

 < asp:TextBox ID="Password" runat="server" TextMode="Password"
onfocus
="if(this.value =='PASSWORD')this.value='';" onblur="if(this.value==''){this.value='PASSWORD';this.TextMode='SingleLine';}">PASSWORD< /asp:TextBox>

How to show large image on mouse click on the small image using JavaScript in asp.net ?

In this tutorial I will show you how we can use java script to display the large image by clicking on the small image.

Steps:

Create new website.

Now add one more div tag in the body ....

<div id="bigDiv" style="position: absolute; z-index: 2; border: solid 1px black;

width: auto; height: auto; left: 250px;">

Drag the image control in the body of the form (also set the attribute ImageUrl to some image in the project )& place it after the above div tag.

Now add the following javascript in the head of the page.

<script type="text/javascript" language="javascript">
 
        function showZoomImage(obj) {
            document.getElementById("bigDiv").style.visibility = "visible";
            document.getElementById("bigDiv").innerHTML = "";
            document.getElementById("bigDiv").style.left = event.clientX;
            document.getElementById("bigDiv").style.top = event.clientY;
           document.getElementById("bigDiv").style.zIndex = "0";
 
        }
        function ShowDefaultImage(obj) {
            document.getElementById("bigDiv").innerHTML = "";
            document.getElementById("bigDiv").style.visibility = "hidden";
        }
 
 
   script>
 
 

This will look something like this.....

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 
Inherits="_Default"EnableViewState="false" ViewStateMode="Disabled" %>
 
<! 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>
    <script type="text/javascript" language="javascript">
 
        function showZoomImage(obj) {
            document.getElementById("bigDiv").style.visibility = "visible";
            document.getElementById("bigDiv").innerHTML = "";
            document.getElementById("bigDiv").style.left = event.clientX;
            document.getElementById("bigDiv").style.top = event.clientY;
            document.getElementById("bigDiv").style.zIndex = "0";
 
        }
        function ShowDefaultImage(obj) {
            document.getElementById("bigDiv").innerHTML = "";
            document.getElementById("bigDiv").style.visibility = "hidden";
        }
 
    script>
head>
<body>
    <form id="form1" runat="server" enableviewstate="false" viewstatemode="Disabled">
    <div>
        <div id="bigDiv" style="position: absolute; z-index: 2; border: solid 1px black;
            width: auto; height: auto; left: 250px;">
        div>
<asp:Image ID="Image2" runat="server" ImageUrl="~/Images/Desert.jpg" Width="100"
                    Height="100" onclick="showZoomImage(this);" />
        div>
    form>
body>
html>
 

Just keep in mind that id of the empty div & id in the script should be the same (i have indicated in the Green colour in the above mark-up)

Like this we can make much more things ....I have made this.....

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"
    EnableViewState="false" ViewStateMode="Disabled" %>
 
<!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>
    <script type="text/javascript" language="javascript">
 
        function showZoomImage(obj) {
            document.getElementById("bigDiv").style.visibility = "visible";
            document.getElementById("bigDiv").innerHTML = "";
            document.getElementById("bigDiv").style.left = event.clientX;
            document.getElementById("bigDiv").style.top = event.clientY;
            document.getElementById("bigDiv").style.zIndex = "0";
 
        }
        function ShowDefaultImage(obj) {
            document.getElementById("bigDiv").innerHTML = "";
            document.getElementById("bigDiv").style.visibility = "hidden";
        }
 
 
    script>
    <style>
        .oneImage
        {
            padding: 5px 5px 5px 5px;
        }
        .allImage
        {
            padding: 0px 10px 0px 10px;
            width: 200px;
            height: 605px;
            overflow: scroll;
        }
    style>
head>
<body>
    <form id="form1" runat="server" enableviewstate="false" viewstatemode="Disabled">
    <div>
        <div id="bigDiv" style="position: absolute; z-index: 2; border: solid 1px black;
            width: auto; height: auto; left: 250px;">
        div>
        <div class="allImage">
            <div class="oneImage">
                <asp:Image ID="Image2" runat="server" ImageUrl="~/Images/Desert.jpg" Width="100"
                    Height="100" onclick="showZoomImage(this);" />div>
            <div class="oneImage">
                <asp:Image ID="Image1" runat="server" ImageUrl="~/Images/Chrysanthemum.jpg" Width="100"
                    Height="100" onclick="showZoomImage(this);" />div>
            <div class="oneImage">
                <asp:Image ID="Image3" runat="server" ImageUrl="~/Images/Hydrangeas.jpg" Width="100"
                    Height="100" onclick="showZoomImage(this);" />div>
            <div class="oneImage">
                <asp:Image ID="Image4" runat="server" ImageUrl="~/Images/Jellyfish.jpg" Width="100"
                    Height="100" onclick="showZoomImage(this);" />div>
            <div class="oneImage">
                <asp:Image ID="Image5" runat="server" ImageUrl="~/Images/Lighthouse.jpg" Width="100"
                    Height="100" onclick="showZoomImage(this);" />div>
            <div class="oneImage">
                <asp:Image ID="Image6" runat="server" ImageUrl="~/Images/Desert.jpg" Width="100"
                    Height="100" onclick="showZoomImage(this);" />div>
            <div class="oneImage">
                <asp:Image ID="Image7" runat="server" ImageUrl="~/Images/Chrysanthemum.jpg" Width="100"
                    Height="100" onclick="showZoomImage(this);" />div>
            <div class="oneImage">
                <asp:Image ID="Image8" runat="server" ImageUrl="~/Images/Hydrangeas.jpg" Width="100"
                    Height="100" onclick="showZoomImage(this);" />div>
            <div class="oneImage">
                <asp:Image ID="Image9" runat="server" ImageUrl="~/Images/Jellyfish.jpg" Width="100"
                    Height="100" onclick="showZoomImage(this);" />div>
            <div class="oneImage">
                <asp:Image ID="Image10" runat="server" ImageUrl="~/Images/Lighthouse.jpg" Width="100"
                    Height="100" onclick="showZoomImage(this);" />div>
        div>
    form>
body>
html>