RSS

Monthly Archives: July 2010

Upload multiple files in asp.net with Javascript


Some times you need to allow user to upload as many files not fixed the number of file.then you need to use this code ,i think this help for you………

the following code use in CSS File or Header of the Aspx page

<style type=”text/css”>
.fileUpload{
width:260px;
font-size:12px;
color:#00000f;
border:solid;
border-width:2px;
border-color:#7f9db9;
height:18px;
}
</style>

Write this JavaScript  code in Header,If you want to restrict user to certain number of files then you can change your java script function as given below.

<script language=”javascript” type=”text/javascript”>
function AddMoreImages() {
if (!document.getElementById && !document.createElement)
return false;
var fileUploadarea = document.getElementById(“fileUploadarea”);
if (!fileUploadarea)
return false;
var newLine = document.createElement(“br”);
fileUploadarea.appendChild(newLine);
var newFile = document.createElement(“input”);
newFile.type = “file”;
newFile.setAttribute(“class”, “fileUpload”);

if (!AddMoreImages.lastAssignedId)
AddMoreImages.lastAssignedId = 100;
newFile.setAttribute(“id”, “FileUpload” + AddMoreImages.lastAssignedId);
newFile.setAttribute(“name”, “FileUpload” + AddMoreImages.lastAssignedId);
var div = document.createElement(“div”);
div.appendChild(newFile);
div.setAttribute(“id”, “div” + AddMoreImages.lastAssignedId);
fileUploadarea.appendChild(div);
AddMoreImages.lastAssignedId++;
}

</script>

Write this code in body tag

<div>
<div id=”fileUploadarea”><asp:FileUpload ID=”fuPuzzleImage” runat=”server” CssClass=”fileUpload” /><br /></div><br />
<div><input style=”display:block;” id=”btnAddMoreFiles” type=”button” value=”Add more images” onclick=”AddMoreImages();” /><br />
<asp:Button ID=”Button1″ runat=”server” onclick=”Button1_Click” Text=”Upload” />
</div>
</div>

Insert the following code in code behind file of page.


protected void Button2_Click(object sender, EventArgs e)
{
try
{
HttpFileCollection hfc = Request.Files;
for (int i = 0; i < hfc.Count; i++)
{
HttpPostedFile hpf = hfc[i];
if (hpf.ContentLength > 0)
{
hpf.SaveAs(Server.MapPath(“~/uploads/”) +System.IO.Path.GetFileName(hpf.FileName));
}
}
lblmsg.Text = “Sucessfully Upload”;
}
catch (Exception)
{

throw;
}
}

Advertisements
 
Leave a comment

Posted by on July 10, 2010 in Java Script

 

Simple Captcha Create with Javascript Code


the Below Code write in Header

<script language=”javascript”>

var a = 49, b = 65;

var c = 100;

var d = 70;

function show()

{

if (a == 57) {

a = 49;

}


var main = document.getElementById(‘txt1’);

var a1 = String.fromCharCode(a);

var b1 = String.fromCharCode(b);

var c1 = String.fromCharCode(c);

var d1 = String.fromCharCode(d);


main.value = a1 + b1 + c1 + d1;

a = a + 1;

b = b + 1;

c = c + 1;

d = d + 1;

}

</script>

write this code in body ………..

<body>

<form id=”form1″ runat=”server”>

<div>

<input type=”text” id=”txt1″ runat=”server”

style=”border-style: none; border-color: inherit; border-width: medium; background-color:black; color:red; font-family: ‘Curlz MT’; font-size: x-large; font-weight: bold; font-variant: normal; letter-spacing: 10pt; width: 120px; background-image: url(‘glitter_background_b4.gif’);”

value=”5AbD” />

<input type=”button” onclick=”show()” value=”Change”  />

</div>

<asp:TextBox ID=”txtverification” runat=”server”></asp:TextBox>

&nbsp;&nbsp;&nbsp;&nbsp;

<asp:Button ID=”Button1″ runat=”server” Text=”Verification”

onclick=”Button1_Click” />

&nbsp;&nbsp;&nbsp;&nbsp;

<asp:Label ID=”lblmsg” runat=”server” Font-Bold=”True” ForeColor=”Red”></asp:Label>

</form>

</body>

check or verification of Random Generate Captcha Code and write this code in Code-Behind .CS File

In the (.cs) page I write the following code for varification:->

protected void Button1_Click(object sender, EventArgs e)

{

if (txtverification.Text == txt1.Value)

{

lblmsg.Text = “Successfull”;

}

else

{

lblmsg.Text = “Failure”;

}

}


 
2 Comments

Posted by on July 10, 2010 in Java Script

 

How to get the value of Textbox, CheckBoxlist, RadioButtonList, ListBox, DropDownList in javascript


Introduction

This Article explains  how to Raise Client Click Events for Server Controls

usage:

1.Attribute ‘OnClientClick’:-The OnClientClick property is used to sets a client side script or Method to be run when the Button control is clicked.

eg:OnClientClick =”return javascriptMethod()”

2.getElementById():-It is used to get the Elements in a from.This method takes an argument to access elements with a unique id attribute.

eg:document.getElementById (“textbox1”);

3)getElementsByTagName():-This method takes in as argument the name of a tag element and returns an array of all matching tags elements found in the document.

eg:document.getElementById (“Label”);

How to get the value of textbox in javascript ?

in .aspx source code

<head runat="server">
    <title>Untitled Page</title>
    <script type ="text/javascript">
    function getTxt()
    {
     var txt=document.getElementById("TextBox1");
     alert(txt.value);
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" Height="16px" Style="z-index: 100; left: 176px;position: absolute; top: 32px" Width="96px"></asp:TextBox>
  <asp:Button ID="Button1" runat="server" Font-Bold="True" Style="z-index:101;left:184px;position:absolute; top:64px" Text="Click" Width="56px"
 OnClientClick ="return  getTxt()"/>
        <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="0.8em" Style="z-index: 103;left: 96px; position: absolute; top: 32px" Text="User Name:"></asp:Label>
    </div>
    </form>
</body>
</html>
















 How to get the text of CheckBoxList  in Javascript?
 


<head runat="server">


    <title>Untitled Page</title>


    <script type="text/javascript">


    function chkboxMeth()


    {


      var chkbox = document.getElementById("CheckBoxList1");


      var inputArr = chkbox.getElementsByTagName('input');


      var labelArr = chkbox.getElementsByTagName('label');


      var sum="";


     for (var i=0; i<labelArr.length; i++)


     {


      if (inputArr[i].checked == true)


{


sum = sum + labelArr[i].innerText;


}


    }


alert(sum);


return false;


  }


    </script>


</head>


<body>


    <form id="form1" runat="server">


    <div>


    <asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True" BackColor="#00CC99" Font-Bold="True" ForeColor="#FF0066">


            <asp:ListItem Value="1">Apple</asp:ListItem>


            <asp:ListItem Value="2">Banana</asp:ListItem>


            <asp:ListItem Value="3">Grapes</asp:ListItem>


        </asp:CheckBoxList>


        <asp:Button ID="Button1" runat="server" Style="position: absolute; z-index: 102; left: 400px; top: 48px;" Text="Click" OnClientClick =" return chkboxMeth()" BackColor="#CCCC66" Font-Bold="True" Width="64px"/>


    </div>


    </form>


</body>


</html>

 

 





How to get the value or text of radioButtonList in javascript?

<head runat="server">
    <title>Untitled Page</title>

    <script type ="text/javascript">             

  function GetRadioButtonSelectedValue()

  {

      var radio = document.getElementById('radiobuttonlist1');

      var inputArr = radio.getElementsByTagName('input');

      var labelArr = radio.getElementsByTagName('label');

     for (var i=0; i<inputArr.length; i++)

     {

        if (inputArr[i].checked )

         {

          alert("Selected Value:"+inputArr[i].value+"  

                Selected Text :"+labelArr[i].innerText);

          inputArr[i].checked =false   

         }

     }

 

  }  

    </script>

 

</head>

<body>

    <form id="form1" runat="server">

    <div>

 

    <asp:RadioButtonList ID="radiobuttonlist1" runat="Server" RepeatLayout="flow" ForeColor="Blue" BackColor="#E0E0E0" Font-Bold="True" Font-Names="Verdana" Font-Size="Small">

          <asp:ListItem Text="ASP.NET" Value="1" ></asp:ListItem>

          <asp:ListItem Text="SilverLight" Value="2"></asp:ListItem>

          <asp:ListItem Text="C#.NET" Value="3"></asp:ListItem>

   </asp:RadioButtonList>

 

   <asp:Button ID="Button1" runat="server" Style="position: static;" Text="Select" 

     OnClientClick =" return GetRadioButtonSelectedValue(); " />

</div>

    </form>

</body>

</html>



How to get the selected value or text  of dropdown in javascript?

    <title>Untitled Page</title>
     <script type ="text/javascript">
      function ddlmeth()
      {
var ddl=document.getElementById("DropDownList1");
     alert("Selected Value: "+ddl.value +"  "+"Selected Text: "+ddl[ddl.selectedIndex].innerText);
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
<asp:Button ID="Button1" runat="server" Style="position: absolute; z-index: 103; left: 200px; top: 120px;" Text="click" OnClientClick ="return ddlmeth()" ForeColor="#FF0066" Width="72px"  />
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" Style="z-index: 102;
            left: 208px; position: absolute; top: 8px">
            <asp:ListItem Value="1">ASP.NET</asp:ListItem>
            <asp:ListItem Value="2">VB.NET</asp:ListItem>
            <asp:ListItem Value="3">C#.NET</asp:ListItem>
        </asp:DropDownList>

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

How to get the text or value of ListBox in JavaScript?


<head runat="server">
    <title>Untitled Page</title>
    <script type ="text/javascript">
    function getListBox()
    {
     var listbox=document.getElementById("ListBox1");

     alert("Selected Value:"+listbox.value+" 
            Selected Text:"+listbox[listbox.selectedIndex].innerText);
    }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ListBox ID="ListBox1" runat="server" BackColor="#FFFFFF" Font-Bold="True" Font-Names="Verdana"ForeColor="#660066" Height="80px" Style="z-index: 100; left: 240px; position: absolute; top: 48px" Width="80px">
            <asp:ListItem Value="1">SilverLight</asp:ListItem>
            <asp:ListItem Value="2">Ajax</asp:ListItem>
            <asp:ListItem Value="3">JQuery</asp:ListItem>
        </asp:ListBox>

        <asp:Button ID="Button1" runat="server" Font-Bold="True" Style="z-index: 102; left: 336px; position: absolute; top: 72px" Text="Click" Width="56px" 
   OnClientClick ="return getListBox();"/>

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














I hope,you  enjoyed it !

 

Thanking you

Sajjad Ahmed

MCS(2004-2006)

The Islamia University Bahawal Pur

Mob:0301-7770198
 
Leave a comment

Posted by on July 9, 2010 in Java Script