Archive

Archive for April, 2009

Change Color of Uploaded Image to Black and White

April 24, 2009 1 comment

Hi

try this example to Change Color of Uploaded Image to Black and White

1- Create new website and add new page and New Folder and Name it Upload and inside this folder add another folder

and name it to Original

2- Open your Web page and add FileUpload and Button and Image control

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ChangeImageColor.aspx.cs" Inherits="ChangeImageColor" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</a>">

<html xmlns="<a href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</a>" >
<head runat="server">
    <title>Change Color Image to Black and White</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <p>Please upload your picture:
    <asp:FileUpload id="PictureUploadControl" Width="400" runat="server" />       
    </p>
        <p>
    <asp:Button runat="server" id="AddPictureButton" text="Upload" onclick="AddPictureButton_Click" /><br />
        </p>

    <asp:Image ID="GrayscaledPhoto" runat="server" /><br />

    </div>
    </form>
</body>
</html>

3- In code behind add this code:-

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;  //Bitmap needed this
using System.Drawing.Imaging; //ImageFormat needed this
using System.IO; // .delete needed this
public partial class ChangeImageColor : System.Web.UI.Page
{
    public static Bitmap Grayscale(Bitmap bitmap)
    {
        //Declare myBitmap as a new Bitmap with the same Width & Height
        Bitmap myBitmap = new Bitmap(bitmap.Width, bitmap.Height);
        for (int i = 0; i < bitmap.Width; i++)
        {
            for (int x = 0; x < bitmap.Height; x++)
            {
                //Get the Pixel
                Color BitmapColor = bitmap.GetPixel(i, x);
                //I want to come back here at some point and understand, then change, the constants
                //Declare grayScale as the Grayscale Pixel
                int grayScale = (int)((BitmapColor.R * 0.3) + (BitmapColor.G * 0.59) + (BitmapColor.B * 0.11));
                //Declare myColor as a Grayscale Color
                Color myColor = Color.FromArgb(grayScale, grayScale, grayScale);
                //Set the Grayscale Pixel
                myBitmap.SetPixel(i, x, myColor);
            }
        }
        return myBitmap;
    }

    protected void AddPictureButton_Click(object sender, EventArgs e)
    {
        if (PictureUploadControl.HasFile)
        {
            PictureUploadControl.SaveAs(Server.MapPath("~/Upload/Original") + PictureUploadControl.FileName);
            Bitmap oldBitmap = new Bitmap(Server.MapPath("~/Upload/Original") + PictureUploadControl.FileName, false);
            Bitmap newBitmap = Grayscale(new Bitmap(oldBitmap));
            string name = Guid.NewGuid().ToString();
            newBitmap.Save(Server.MapPath("~/Upload/") + name + ".jpg", ImageFormat.Jpeg);
            oldBitmap.Dispose();
            //we will delete the old
           // File.Delete(Server.MapPath("~/Upload/Original") + PictureUploadControl.FileName);
            GrayscaledPhoto.ImageUrl = "Upload/" + name + ".jpg";
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

Hope this helps

Good Luck

Categories: ASP.Net

Use Required Field Validator for CheckboxList

April 22, 2009 13 comments

Hi

try this example to use required field validator for checkboxList with Client Side

<script language="javascript" type="text/javascript">
    function ValidateChkList(source, arguments)
    {                                                   
        arguments.IsValid = IsCheckBoxChecked() ? true : false;  

    }

    function IsCheckBoxChecked()
    {   
        var isChecked = false;
        var list =document.getElementById('<%= CheckBoxList1.ClientID %>');
        if(list != null)
        {
         for (var i=0; i<list.rows.length; i++)
         {
          for (var j=0; j<list.rows[i].cells.length; j++)
          {
           var listControl = list.rows[i].cells[j].childNodes[0];                      
           if(listControl.checked)
           {                      
            isChecked = true;
           }
          }
         }
         }
         return isChecked;

    }
    </script>

   <div>
        <asp:CheckBoxList ID="CheckBoxList1" runat="server">
            <asp:ListItem Text="C#" ></asp:ListItem>
            <asp:ListItem Text="VB"></asp:ListItem>
        </asp:CheckBoxList>
        <asp:CustomValidator ID="CustomValidator1" ClientValidationFunction="ValidateChkList"
            runat="server" >Required.</asp:CustomValidator>
    </div>
    <div>
        <asp:Button ID="Button2" runat="server" Text="Submit" OnClick="Button2_Click"  />
    </div>

Hope this helps

Good Luck

Categories: ASP.Net, Javascript

Create Popup Panel

April 21, 2009 Leave a comment

Hi

try this example :-

<html>
<head>

<style>

Body
{
 font-family: Arial;
}

.PopupPanel
{
 border: solid 1px black;
 position: absolute;
 left: 50%;
 top: 50%;
 background-color: white;
 z-index: 100;
 height: 200px;
 margin-top: -100px;
 width: 400px;
 margin-left: -200px;
}

.PopupPanelModalArea
{
 left: 0;
 top: 0;
 height: 100%;
 width: 100%;
 position: absolute;
 background-color:silver;
    filter: progid:DXImageTransform.Microsoft.Alpha(opacity=60);
 z-index: 99;
 border: 0;
 -moz-opacity: 0.60;
}

.PopupPanel .TitleBar
{
 margin: 0;
 display: block;
 background-color: #0058ee;
 line-height: 20px;
 color: white;
 font-weight: bold;
 padding: 0 0 0 5px;
}

.PopupPanel .ContentArea
{
 padding: 0 0 0 5px;
}

</style>

<script>
/**********************************
Simply displays or hides the panel
**********************************/
function TogglePopupPanel()
{
 var panelContainer = document.getElementById("PopupPanel");
 
 if (panelContainer.style.display == "none")
 {
  panelContainer.style.display = "";
  document.getElementById('PopupPanelModalArea').focus();
  document.body.onfocus = function() { document.getElementById('PopupPanelModalArea').focus(); };
 }
 else
 {
  panelContainer.style.display = "none";
  document.body.onfocus = function() { return true; };
 }
}

</script>

</head>
<body>

This is the Popup Panel test page.  The idea is that you will not be able<br/>
to alter or interact with the contents of the underlying page while the panel is displaying.<br/>

<br/>
<input type="Button" value="Display Popup Panel" onclick="TogglePopupPanel()" /><br/>
<br/>

<select>
 <option value="0">-- Select --</option>
 <option value="1">One</option>
 <option value="2">Two</option>
 <option value="3">Three</option>
</select>

<input type="Button" value="Test Button"/><br/>

<!-- ****************************** -->
<!-- Start of the PopupPanel HTML   -->
<!-- ****************************** -->

<div id="PopupPanel" style="display:none">
 <iframe class="PopupPanelModalArea" frameborder="0" scrolling="0" id="PopupPanelModalArea"></iframe>
 
 <div class="PopupPanel">
  
  <p class="TitleBar">
   Popup Panel Title Bar
  </p>
  
  <p class="ContentArea">
   Even though the underlying page is disabled, you can however use the controls in this area
   <br/>
   <br/>
   <select>
    <option value="0">-- Select --</option>
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
   </select>
   <br/>
   <br/>
   <input type="Button" value="Close Popup Panel" onclick="TogglePopupPanel()" />
  </p>
 </div>
</div>
<br/>
</body>
</html>

Hope this helps

Good Luck

Categories: ASP.Net, CSS, Javascript

Show and Hide Panel using Javascript

April 19, 2009 4 comments

Hi

try this example:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</a>">
<html xmlns="<a href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</a>">
<head runat="server">
    <title>Toggle Panel</title>

    <script language="javascript" type="text/javascript">
        function toggle()
        { 
            var ctrlID = document.getElementById('<%= pnlToggle.ClientID %>');                        
            if (ctrlID.style.display == 'none')
            {
                ctrlID.style.display = 'block';
                document.getElementById('<%= lbTogglePanel.ClientID %>').innerHTML="Hide Panel";
            }
            else
            {
                ctrlID.style.display = 'none';
                document.getElementById('<%= lbTogglePanel.ClientID %>').innerHTML="Show Panel";
            }       
        }
    </script>

</head>
<body>
    <form id="form1" runat="server">
        <table cellpadding="2" cellspacing="2" border="0" width="50%">
            <tr id="clickEvent" runat="server">
                <td style="background-color: Gray; font-family: Verdana; cursor:hand; font-size: small;" onclick="javascript:toggle();">
                    <asp:Label ID="lbTogglePanel" runat="server" Text="Hide Panel" Font-Underline="true" Font-Bold="true"></asp:Label>
                </td>
            </tr>
            <tr id="togglePanel" runat="server" style="font-family: Verdana;
                font-size: small;">
                <td>
                    <asp:Panel ID="pnlToggle" runat="server">
                        <asp:Label ID="lbl1" runat="server" Text="Content 1">
                        </asp:Label>
                        <br />
                        <asp:Label ID="Label1" runat="server" Text="Content 2">
                        </asp:Label>
                        <br />
                        <asp:Label ID="Label2" runat="server" Text="Content 3">
                        </asp:Label>
                    </asp:Panel>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

Hope this helps

Good Luck

Categories: ASP.Net, Javascript

Disabling mouse Right click Function (on image)

April 15, 2009 Leave a comment

Hi

try this example :

<asp:Image ID=”Image1″ runat=”server” ImageUrl=”~/images/square.gif” oncontextmenu=”return false;” />

Hope this helps

Good Luck

Categories: ASP.Net

Bind Table to Dynamic Data from Database

April 14, 2009 Leave a comment

Hi

try this example to Bind Asp Table to Data from Database and make each row of this table clicable for update to

another Page

in this example i will use Northwind Database

.ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1">http://www.w3.org/TR/xhtml1/DTD/xhtml1</a>-

transitional.dtd">

<html xmlns="<a href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</a>" >
<head runat="server">
    <title>make an asp:Table row highlight and become clickable</title>
<script type="text/javascript">
function highlight(tableRow, active)
{
if (active)
{
tableRow.style.backgroundColor = '#cfc';
}
else
{
tableRow.style.backgroundColor = '#fff';
}
}

function link(Url)
{
document.location.href = Url;
}
</script>
   
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Table ID="Table1" runat="server">
        </asp:Table>
    </div>
    </form>
</body>
</html>

and in code behind :-

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string connstr = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
       
        string query = "SELECT [CustomerID], [CompanyName], [ContactName], [Address], [City]";
        query += " FROM [Customers]";
        SqlConnection conn = new SqlConnection(connstr);
        SqlDataAdapter da = new SqlDataAdapter(query, conn);
        DataSet ds = new DataSet();
        da.Fill(ds, "Employees");
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            TableRow trow = new TableRow();
            foreach (DataColumn dc in ds.Tables[0].Columns)
            {
                TableCell tcell = new TableCell();
                tcell.Controls.Add(new LiteralControl(dr[dc.ColumnName].ToString()));
                trow.Cells.Add(tcell);

                trow.Attributes["onmouseover"] = "highlight(this, true);";
                trow.Attributes["onmouseout"] = "highlight(this, false);";
                trow.Attributes["onclick"] = "link('Edit.aspx?id=" + dr[0].ToString() + "');";
                HttpResponse myHttpResponse = Response;
                HtmlTextWriter myHtmlTextWriter = new HtmlTextWriter(myHttpResponse.Output);
                trow.Attributes.AddAttributes(myHtmlTextWriter);
                Table1.Rows.Add(trow);
            }
        }
        conn.Close();
    }
}

and create another page called Edit.aspx and get the CustomerID from QueryString

and in web.config add this line

<connectionStrings>
  <add name="NorthwindConnectionString" connectionString="Data Source=.;Initial Catalog=Northwind;Integrated Security=True" providerName="System.Data.SqlClient"/>
 </connectionStrings>

Hope this helps

Good Luck

Categories: ASP.Net

Get User Name Under Windows Authentication

April 13, 2009 Leave a comment

Getting the authenticated user’s name when using Windows Authentication with ASP.NET is very simple.  I just keep having to go back to Google or dig out an old project to remember it, so here it is:

string username = User.Identity.Name.ToString();

Be sure to add the Security reference:

using System.Web.Security;

Hope this helps

Good Luck

Categories: ASP.Net