Showing posts with label c#. Show all posts
Showing posts with label c#. Show all posts

Wednesday, September 7, 2016

css for file upload control in asp.net

In this article, I am going to explain how to implement css design in file upload control in asp.net.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FileUploadStyle.aspx.cs" Inherits="FileUploadStyle" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style>
 /* { box-sizing: border-box; margin: 0; padding: 0; }
div { margin: 8px; }*/
input[type=file], input[type=file] + input {
    display: inline-block;
    background-color: #CC3;
    border: 1px solid #CC3;
    font-size: 15px; padding: 4px;
       cursor: pointer;
}
input[type=file] + input {
    padding: 13px;
    background-color: #00b7cd;
}
::-webkit-file-upload-button {
    -webkit-appearance: none;
    background-color: #CC3;
    border: 1px solid #CC3;
    font-size: 15px; padding: 8px;
       cursor: pointer;
}
::-ms-browse {
    background-color: #00b7cd;
    border: 1px solid #CC3;
    font-size: 15px; padding: 8px;
}
input[type=file]::-ms-value { border: none;  }
.test-head {
    position: relative
}
 
.test-head p {
font-size: 20px;
color: #353535;
font-weight: bold;
background: #CCCC33;
line-height: 60px;
display: inline-block;
padding-right: 24em;
position: relative;
margin-top: 0px;
}
 
.test-head:before {
    content: "";
    position: absolute;
    width: 50%;
    height: 100%;
    max-width: 260px;
    background: #CCCC33;
    top: 0;
    left: 0;
    z-index:0
}
 
.test-head p:after {
    content: " ";
    display: block;
    background: #CCCC33;
    position: absolute;
    bottom: 0px;
    right: -50px;
    border-top: 30px solid transparent;
    border-bottom: 30px solid transparent;
  
}
  </style>
</head>
<body>
    <form id="form1" runat="server">
    <fieldset>
    <legend>File Upload Control Css Style</legend>
    <asp:FileUpload ID="flbupload" runat="server" />
      </fieldset>
    </form>
</body>
</html>

Output:



Thursday, July 7, 2016

How to use file upload control in asp.net with update panel

In this article, I am going to explain how to use file upload control inside update panel in asp.net c#. Create a folder with name "Upload" for saving your file at solution explorer.

aspx code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UploadFileInsideUpdatePanel.aspx.cs" Inherits="UploadFileInsideUpdatePanel" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="smain" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="upmain" runat="server">
    <ContentTemplate>
    <fieldset>
    <legend>Upload File Inside Update Panel</legend>
    <asp:FileUpload ID="flupload" runat="server" />
    <asp:Button ID="bthUplaod" runat="server" Text="Save" onclick="bthUplaod_Click" />
    <asp:Label ID="lblmsg" runat="server" ForeColor="Green"></asp:Label>
    </fieldset>
    </ContentTemplate>
    <Triggers>
    <asp:PostBackTrigger ControlID="bthUplaod" />
    </Triggers>
    </asp:UpdatePanel>
    </form>
</body>
</html>

aspx.cs code

using System;
using System.IO;
 
public partial class UploadFileInsideUpdatePanel : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void bthUplaod_Click(object sender, EventArgs e)
    {
        try
        {
            lblmsg.Text = string.Empty;
            if (flupload.HasFile)
            {
                string physicalDirPath = Server.MapPath("~/Upload");
                string filedata = Path.GetFileNameWithoutExtension(flupload.FileName) + Guid.NewGuid() + Path.GetExtension(flupload.FileName);
                string filename = physicalDirPath + "\\" + filedata;
                flupload.SaveAs(filename);
                lblmsg.Text = "Saved Successfully";
            }
        }
        catch (Exception ex)
        {
            lblmsg.Text = ex.Message;
        }
    }
}

Output:




Sunday, June 19, 2016

Image width and height in asp.net c#

In this article, I am going to explain how to find out uploaded image dimension in asp. net c#.

Code of aspx page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:FileUpload ID="fupload" runat="server" />
    <asp:Button ID="btnCheckHeightWidth" runat="server" Text="Check"
            onclick="btnCheckHeightWidth_Click" /><asp:Label ID="lblmsg" runat="server" ForeColor="Green"></asp:Label>
    </div>
    </form>
</body>
</html>

Code of cs page

using System;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void btnCheckHeightWidth_Click(object sender, EventArgs e)
    {
        System.IO.Stream stream = fupload.PostedFile.InputStream;
        System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
        lblmsg.Text = "Image size is " + image.Width + "x" + image.Height + " px";
    }
}

Image


Output


Friday, June 17, 2016

update controls outside updatepanel c#

In this article, I am going to explain how to update Control value or any other ASP. net control which reside out side update panel.

Let's take a example, suppose I have a button inside update panel and click on that button I want to assign some value in textbox.

Here the aspx Code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="scmain" runat="server"></asp:ScriptManager>
 
    <asp:UpdatePanel ID="upmain" runat="server">
    <ContentTemplate>
     <fieldset>
    <legend>Button Inside Update Panel</legend>
    <asp:Button ID="btnUpdateValue" runat="server" Text="UpdateValue"
             onclick="btnUpdateValue_Click" />
    </fieldset>
    </ContentTemplate>
    </asp:UpdatePanel>
 
     <asp:UpdatePanel ID="uptextbox" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
    <fieldset>
    <legend>Out Side TextBox of Update Panal</legend>
    <asp:TextBox ID="txtvalue" runat="server"></asp:TextBox>
    </fieldset>
    </ContentTemplate>
    </asp:UpdatePanel>
 
    </form>
</body>
</html>

Here the aspx.cs Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void btnUpdateValue_Click(object sender, EventArgs e)
    {
        txtvalue.Text = "Verified";
        uptextbox.Update();
    }
}

Output




Saturday, February 6, 2016

Bind More than One Dropdownlist from DataBase using SqlDataReader

In this article I am going to explain how to bind multiple drop down list from single sqldatareader.

First I have created two table and one procedure. Run given below script in your Sql Server.


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Sha_Country]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Sha_Country](
                [CountryId] [int] IDENTITY(1,1) NOT NULL,
                [CountryName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]
END
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[Sha_Country] ON
INSERT [dbo].[Sha_Country] ([CountryId], [CountryName]) VALUES (1, N'Afghanistan')
INSERT [dbo].[Sha_Country] ([CountryId], [CountryName]) VALUES (2, N'Brazil')
INSERT [dbo].[Sha_Country] ([CountryId], [CountryName]) VALUES (3, N'Cuba')
INSERT [dbo].[Sha_Country] ([CountryId], [CountryName]) VALUES (4, N'Egypt')
INSERT [dbo].[Sha_Country] ([CountryId], [CountryName]) VALUES (5, N'India')
SET IDENTITY_INSERT [dbo].[Sha_Country] OFF


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Sha_Services]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Sha_Services](
                [ServiceID] [int] IDENTITY(1,1) NOT NULL,
                [ServiceName] [varchar](30) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]
END
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[Sha_Services] ON
INSERT [dbo].[Sha_Services] ([ServiceID], [ServiceName]) VALUES (1, N'Service1')
INSERT [dbo].[Sha_Services] ([ServiceID], [ServiceName]) VALUES (2, N'Service2')
INSERT [dbo].[Sha_Services] ([ServiceID], [ServiceName]) VALUES (3, N'Service3')
INSERT [dbo].[Sha_Services] ([ServiceID], [ServiceName]) VALUES (4, N'Service4')
INSERT [dbo].[Sha_Services] ([ServiceID], [ServiceName]) VALUES (5, N'Service5')
SET IDENTITY_INSERT [dbo].[Sha_Services] OFF

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sha_GettwoList]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
-- =============================================
-- Author:                              Sharad Gupta
-- Create date: 6-Jan-2015
-- Description:      
-- =============================================
CREATE PROCEDURE [dbo].[sha_GettwoList]
AS
BEGIN
                -- SET NOCOUNT ON added to prevent extra result sets from
                -- interfering with SELECT statements.
                SET NOCOUNT ON;
 
                SELECT ServiceID,
                                ServiceName
                FROM sha_services
 
                SELECT CountryId,
                                CountryName
                FROM Sha_Country
END
'
END
GO

Now Here code for c# to bind two dropdown list from single SqlDataReader using NextResult() method.

 C# code

using System;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class DropDown : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetDropDownValues();
        }
    }
    private void GetDropDownValues()
    {
        SqlConnection con = new SqlConnection("data source=.;Database=test;uid=********;pwd=********");
        con.Open();
        SqlCommand cmd = new SqlCommand("sha_GettwoList", con);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows)// it is not necessity to check it returen true or false || true if one or more row otherwise false
        {
            ddlService.DataSource = dr;
            ddlService.DataTextField = "ServiceName";
            ddlService.DataValueField = "ServiceID";
            ddlService.DataBind();
            ddlService.Items.Insert(0, new ListItem("Select Services", "0"));
            dr.NextResult();// for next select statement
            if (dr.HasRows)
            {
                ddlCountry.DataSource = dr;
                ddlCountry.DataTextField = "CountryName";
                ddlCountry.DataValueField = "CountryId";
                ddlCountry.DataBind();
                ddlCountry.Items.Insert(0, new ListItem("Select Country", "0"));
            }
            else {
                ddlCountry.Items.Insert(0, new ListItem("Country Not Found", "0"));
            }
        }
        else{
            ddlService.Items.Insert(0, new ListItem("Services Not Found", "0"));
        }
    }
}

Source Code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DropDown.aspx.cs" Inherits="DropDown" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <fieldSet>
    <legend>Multi DropDown List</legend>
    <table>
    <tr>
    <td>Services:</td>
    <td><asp:DropDownList ID="ddlService" runat="server"></asp:DropDownList> </td>
    </tr>
    <tr>
    <td>Country:</td>
    <td><asp:DropDownList ID="ddlCountry" runat="server"></asp:DropDownList> </td>
    </tr>
    </table>
    </fieldSet>
    </div>
    </form>
</body>
</html>


OutPut: