Monday, 20 May 2013

ASP.Net: Bind Dropdownlist with Images | Neha Sharma



Introduction:

This article explains how to bind images with text in dropdownlist from your table.

Requirement:

1.       A table like below:

Table design:

Table data:


Code.aspx :

I have taken a DropdownList control to bind data from table ddl_image .Before the design you need some files like ddl.css, jquery-1.6.1.min.js and jquery.dd.js.you can download it from here.
Add these code in  aspx page :

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Dropdownlist with Images</title>
<link rel="stylesheet" type="text/css" href="dropdown/ddl.css" />
<script type="text/javascript" src="dropdown/js/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="dropdown/js/jquery.dd.js"></script>
<!-- Script is used to call the JQuery for dropdown -->
<script type="text/javascript" language="javascript">
    $(document).ready(function (e) {
        try {
            $("#ddlprofile").msDropDown();
        } catch (e) {
            alert(e.message);
        }
    });
</script>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td align="right">
<b>Profile:</b>
</td>
<td>
<asp:DropDownList ID="ddlprofile" runat="server" Width="150px" onselectedindexchanged="ddlCountry_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
</td>
</tr>
</table>
</form>
</body>
</html>

Code.cs :

Here I tried to addd a dropdownlist first then bind titles and then call both functions on pageload like below :

1.       Bind dropdownlist :

protected void Bindddl()
    {
        SqlConnection con = new SqlConnection("Data Source=nehashama;Initial Catalog=rp;Integrated Security=true");
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from ddl_image", con);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        ddlprofile.DataTextField = "Name";
        ddlprofile.DataValueField = "Profile_image";
        ddlprofile.DataSource = ds;
        ddlprofile.DataBind();
        con.Close();
    }

2.       Bind Title :

protected void BindTitle()
    {
        if (ddlprofile != null)
        {
            foreach (ListItem li in ddlprofile.Items)
            {
                li.Attributes["title"] = "ddl/" + li.Value; // it ll set the value of items in dropdownlist as tooltip
         
            }
        }
    }

3.       Call on Page Load :

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bindddl();
            BindTitle();
           
        }
    }

Now save all and view the page in browser it ll work fine an DropDownList look like below image:


Thanks.





Sunday, 19 May 2013

ASP.Net: Bind files in Gridview and Download in ZIP folder | Neha Sharma



Introduction:

In this article I explain how to bind files in gridview and how to download selected files in zip format.

Requirement:

1.       Dll Ionic.Zip.dll

2.       Following name space
using System;
using System.Collections.Generic;
using System.IO;
using System.Web.UI.WebControls;
using Ionic.Zip;

add Ionic.Zip.dll in to your bin folder in application then add  above name spaces .

Code .aspx :

I have some files in my application ->files ->file1.txt and file2.doc I bind these file sin gridview .But before binding of gridview I design my page.Write belos code to know what I have done :

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Download in zip format</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<asp:Label ID="lbl_txt" runat="server" Font-Bold="true" ForeColor="Red" />
</div>
<asp:GridView ID="gridview1" CellPadding="5" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chk_Select" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Text" HeaderText="FileName" />
</Columns>
<HeaderStyle BackColor="white" Font-Bold="true" ForeColor="White" />
</asp:GridView>
<asp:Button ID="btn_Download" Text="Download " runat="server" onclick="btnDownload_Click" />
</form>
</body>
</html>

Here you can see I have taken a gridview with checkbox and a button with the text delete.

Code.cs :

Bind files in Gridview:

protected void BindGridview()
    {
        string[] files_Path = Directory.GetFiles(Server.MapPath("~/files/"));
        List<ListItem> files = new List<ListItem>();
        foreach (string path in files_Path)
        {
            files.Add(new ListItem(Path.GetFileName(path)));
        }
        gridview1.DataSource = files;
        gridview1.DataBind();
    }
Call this function on pageload :
if (!IsPostBack)
        {
            BindGridview();
        }


Now, when you save all these and view the page in browser you ll see all files in gridview .
Now write the below code on the Button delete click :

using (ZipFile zip = new ZipFile())
        {
            foreach (GridViewRow gr in gridview1.Rows)
            {
                CheckBox chk = (CheckBox)gr.FindControl("chkSelect");
                if (chk.Checked)
                {
                    string fileName = gr.Cells[1].Text;
                    string filePath = Server.MapPath("~/files/" + fileName);
                    zip.AddFile(filePath, "files");
                }
            }
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment; filename=DownloadedFile.zip");
            Response.ContentType = "application/zip";
            zip.Save(Response.OutputStream);
            Response.End();
        }

When you write this code you have done with the code to download selected files in zipformat.Just save all again and view the page in browser it ll work fine.

Thanks

Which Game Engine you should choose | Neha Sharma

Unity3D and Unreal Engine are only 2 Game engine which executes an indispensable position in Game Industry. The choice of the engine must ...