Monday, 28 April 2014

Popup Message in asp.net

public static void DisplayAJAXMessage(Control page, string msg)
    {
        string myScript = String.Format("alert('{0}');", msg);
        ScriptManager.RegisterStartupScript(page, page.GetType(), "MyScript", myScript, true);
    }



 DisplayAJAXMessage(btnsave, "Please Do Not Enter Diplicate Department Name ");

How to implement ajax Auto Complete in asp.net


////////////////////////////////////////////////////////
<script type="text/javascript">
        function DepartmentsSelected(sender, e) {
            var values = e.get_value().split(":");
            var LeaveRecommendAuthirty = values[1];
            var LeaveSanctionAuthority = values[2];
            if (LeaveRecommendAuthirty != '') {
                $('#dropleaverecommend').val(LeaveRecommendAuthirty);
            }
            else {
                $('#dropleaverecommend').val('0');
            }
            if (LeaveSanctionAuthority != '') {
                $('#dropsanctionauthority').val(LeaveSanctionAuthority);
            }
            else {
                $('#dropsanctionauthority').val('0');
            }
            return false;
        }


///////////////////////////////////////////////////

<td>Department Name
            <br />
            <asp:TextBox ID="txtdepartment" CssClass="text" runat="server" ClientIDMode="Static"></asp:TextBox>
            <cc1:AutoCompleteExtender ID="txtdepartment_AutoCompleteExtender" runat="server" DelimiterCharacters="" Enabled="True"
             MinimumPrefixLength="2" OnClientItemSelected="DepartmentsSelected"
             ServiceMethod="DisplayDepartments" TargetControlID="txtdepartment" UseContextKey="True">
             </cc1:AutoCompleteExtender>
             </td>



//////////////////////////////////////////////////



 [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
     public static List<String> DisplayDepartments(string prefixText)
     {
         Department ob = new Department();
         DataSet ds = ob.DisplayDepartments(prefixText);
         List<String> Departments = new List<String>();
         Int32 i = -1;
         foreach (DataRow row in ds.Tables[0].Rows)
         {
             String item = AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(row["Name"].ToString(), row["ID"].ToString() + ":" + row["LeaveRecommendAuthirty"].ToString() + ":" + row["LeaveSanctionAuthority"].ToString());
             Departments.Add(item);
         }
         return Departments;
     }

///////////////////////////////////////////////////

ALTER PROC [dbo].[pDisplayDepartment]
    @Department VARCHAR(50)
AS
    BEGIN
   
        SELECT  Name,Id,ISNULL(LeaveRecommendAuthirty,'') AS LeaveRecommendAuthirty,ISNULL(LeaveSanctionAuthority,'') AS LeaveSanctionAuthority
        FROM    dbo.Department
        WHERE   Name LIKE '%' + @Department + '%' AND isActive=1
    END


///////////////////////////////////////////////////

Wednesday, 5 March 2014

Delete multiple Rows at the same time in Gridview in asp.net

  // On button click of Delete


protected void btndelete_Click(object sender, EventArgs e)
    {
        string[] str = new string[5];
        int i=0;
        foreach (GridViewRow gr in gvDetails.Rows)
        {
            CheckBox cb = (CheckBox)gr.Cells[0].FindControl("chk");


            if (cb.Checked)
            {
               
                str[i] = Convert.ToString( cb.Text);
                i++;
              

            }


       
        }
        foreach (string s in str)
        {
           
            SqlCommand cmdd = new SqlCommand("Delete from UserInformation where UserId='"+s+"' ", con);
            con.Open();
           cmdd.ExecuteNonQuery();
            con.Close();
        }
        BindRolesDetails();
    }
   

Thursday, 27 February 2014

Create a small Server........

//  Create  a folder in main root name with files
 //And Download DLL file
// Ionic.Zip.dll
<asp:GridView ID="gridview1" CellPadding="5" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="chk" Checked="false" 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="btn_Download_Click" />

************************ cs file********************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Ionic.Zip;
public partial class zipfolder : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
        BindGridview();
        }
    }
    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();
    }



    protected void btn_Download_Click(object sender, EventArgs e)
    {
        using (ZipFile zip = new ZipFile())
        {
            foreach (GridViewRow gr in gridview1.Rows)
            {
                CheckBox chk = (CheckBox)gr.FindControl("chk");
                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();
        }


    }
}

//Result