Archive

Archive for January, 2009

Add Text To Uploaded Image

January 31, 2009 Leave a comment

Hi

try this example:

First :- create a web form and add Fileupload and Button controls

Secod: in code behind

private Bitmap yaz(System.Drawing.Image resim, int width, int hieght, string name, float font)
    {
        Bitmap resmim = new Bitmap(resim, width, hieght);
        System.Drawing.Graphics graf = System.Drawing.Graphics.FromImage(resmim);
        System.Drawing.SolidBrush firca = new SolidBrush(System.Drawing.Color.Red);
        System.Drawing.Font fnt = new Font("calibri", font);//font type
        System.Drawing.SizeF size = new SizeF(0, 0);
        System.Drawing.PointF coor = new PointF(0, 0);
        System.Drawing.RectangleF kutu = new RectangleF(coor, size);
        StringFormat sf = new StringFormat();
        sf.FormatFlags = StringFormatFlags.DirectionVertical;
        graf.DrawString(name, fnt, firca, kutu, sf);
        return resmim;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        System.Drawing.Image i = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
        Bitmap b = yaz(i, i.Width, i.Height, "Yasser Zaid", 16); //
        b.Save(Server.MapPath("~/Ads/" + FileUpload1.FileName));
    }

Hope this helps

Good Luck

Advertisements
Categories: ASP.Net

Regular Expression for email address validation

January 31, 2009 Leave a comment

Hi

use this regular expression for email address validation

^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$

Hope this helps

Good Luck

Categories: ASP.Net

Change TextBox Color if Empty

January 29, 2009 Leave a comment

This is example of how to create Textbox user control that when textbox is empty
will change it’s background to red else change background to white

Create MyTextBox  control:  MyTextBox.ascx

 <%@ Control Language="C#" AutoEventWireup="true" CodeFile="MyTextBox.ascx.cs" Inherits="WebUserControl" %>
<asp:TextBox ID="txtText" runat="server"/>
<asp:CustomValidator id="valCustom" runat="server"
    ControlToValidate="txtText"
    ClientValidationFunction="MyTextBox_ClientValidate" Display="None" ></asp:CustomValidator>

and in code behind :

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

public partial class WebUserControl : System.Web.UI.UserControl
{

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        StringBuilder script = new StringBuilder();
        script.AppendLine("<script type='text/javascript' language='javascript'>");
        script.AppendLine("   function MyTextBox_ClientValidate(source, arguments){");
        script.AppendLine("   var textBox = document.getElementById(source.controltovalidate);");
        script.AppendLine("   if (arguments.Value != \"\"){");
        script.AppendLine("      arguments.IsValid = true;");
        script.AppendLine("      textBox.style.background = \"white\";");
        script.AppendLine("   }else{");
        script.AppendLine("      arguments.IsValid = false;");
        script.AppendLine("      textBox.style.background = \"red\";");
        script.AppendLine("   }");
        script.AppendLine("}\n");

        // OVERRIDE THE NORMAL VALIDATOR FRAMEWORK
        script.AppendLine("function CustomValidatorEvaluateIsValid(val){");
        script.AppendLine("   var value = \"\";");
        script.AppendLine("   if (typeof(val.controltovalidate) == \"string\") {");
        script.AppendLine("      value = ValidatorGetValue(val.controltovalidate);");
        script.AppendLine("   }");
        script.AppendLine("   var args = { Value:value, IsValid:true };");
        script.AppendLine("   if (typeof(val.clientvalidationfunction) == \"string\") {");
        script.AppendLine("      eval(val.clientvalidationfunction + \"(val, args)\");");
        script.AppendLine("   }");
        script.AppendLine("   return args.IsValid;");
        script.AppendLine("}");
        script.AppendLine("</script>");
        this.Page.RegisterStartupScript("TextClientValidate", script.ToString());
    }   
}

Hope this helps

Good Luck

Categories: ASP.Net

Remooving a scrolbar from a Textbox

January 29, 2009 Leave a comment

Hi

try this example:

<asp:TextBox ID="BigTextBox"  runat="server" Style="white-space: pre; overflow: hidden;" Height="401px" TextMode="MultiLine" Width="484px" />

//——or this

<style>.text{overflow: hidden;}</style>

<asp:TextBox ID="BigTextBox" runat="server" Style="white-space: pre;" Height="401px" TextMode="MultiLine" Width="484px"  CssClass="text"/>

Good Luck

Categories: ASP.Net, CSS

Validate at Least one of two TextBoxes

January 29, 2009 Leave a comment

Hi

try this example:

** using Client Side:

<script type="text/javascript">
function AtLeastOneContact_ClientValidate(source, args)
{
if (document.getElementById("<%= Phone.ClientID %>").value =="" &&
document.getElementById("<%= Email.ClientID %>").value == "" )
{
args.IsValid = false;
}
else
{
args.IsValid = true;   
}
}

</script>
<b>Phone: </b><asp:TextBox id="Phone" runat="server"></asp:TextBox><br />
<b>Email: </b><asp:TextBox id="Email" runat="server"></asp:TextBox>
<asp:Button id="Submit" Text="Submit" runat="server" /><br />

<%-- AtLeastOneContact Custom Validator --%>
<asp:CustomValidator id="AtLeastOneContact" runat="server" 
  ErrorMessage="Phone or Email Required"
  Display="Dynamic"
  OnServerValidate="AtLeastOneContact_ServerValidate"
  ClientValidationFunction="AtLeastOneContact_ClientValidate" />

in code behind :

** using server side

protected void AtLeastOneContact_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (Phone.Text!="" && Email.Text != "")
            args.IsValid = true;
        else
        {
            args.IsValid = false;
        }
    }

Good Luck

Categories: ASP.Net Tags:

Make TextBox Accept only Decimal

January 29, 2009 Leave a comment

Hi

try this example:

Javascript function

// function to make the textboxes accept only decimals
    function decimalOnly(evt)
    {
        evt = (evt) ? evt : event;
        var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
        if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode!= 46)
        {
            return false;
        }
        return true;
    }

and in code behind in Page Load:

TextBox1.Attributes.Add("onkeypress", "return decimalOnly(event)");

Good Luck

Categories: ASP.Net

Make TextBox accept Number only

January 29, 2009 Leave a comment

Hi

try thes example to Make TextBox accept Number only

Example 1:

Use this Javascript function

//-- allow textbox to accept numbers only
    function numeralsOnly(evt)
    {
        evt = (evt) ? evt : event;
        var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
        if (charCode > 31 && (charCode < 48 || charCode > 57))
        {
            //alert("Enter numerals only in this field.");
            return false;
        }
        return true;
    }

and in code behind in Page Load:

TextBox1.Attributes.Add("onkeypress", "return numeralsOnly(event)");

Example 2:

using  RegularExpressionValidator control

<asp:RegularExpressionValidator ValidationGroup="reg" ID="RegularExpressionValidator2" ControlToValidate="txtMobile"
runat="server" ErrorMessage="Invalid Mobile No" ValidationExpression="^[0-9]*$">*</asp:RegularExpressionValidator>

Good Luck

Categories: ASP.Net