AJAX HoverMenuExtender for Gridview Edit,Update,Delete


Code In ASPX page:

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head id="Head1" runat="server">
<title></title>
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.style1
{
width: 272px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></asp:ToolkitScriptManager>
<table>
<tr><td class="style1"></td><td><asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onrowediting="GridView1_RowEditing" CellPadding="4" ForeColor="#333333"
GridLines="None" onrowcancelingedit="GridView1_RowCancelingEdit"
onrowupdating="GridView1_RowUpdating" onrowdeleting="GridView1_RowDeleting">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="HoverMenuExample">
<ItemTemplate>
<asp:Panel CssClass="popupMenu" ID="PopupMenu" runat="server"
style="display:none">
<div style="border:1px outset white;padding:2px;">
<div><asp:LinkButton ID="LinkButton1" runat="server"
CommandName="Edit" Text="Edit" /></div>
<div><asp:LinkButton ID="LinkButton2" runat="server"
CommandName="Delete" Text="Delete" /></div>
</div>
</asp:Panel>
<asp:Panel ID="Panel9" runat="server">
<table width="100%">
<tr>
<td width="25%">
<asp:Label Font-Bold="true" ID="Label1" runat="server"
Text='<%#Eval("EmpNo") %>' /></td>
<td width="50%"><asp:Label ID="Label2" runat="server"
Text='<%# Eval("EmpName") %>' /></td>
<td width="25%"><asp:Label ID="Label3" runat="server"
Text='<%# Eval("EmpJob") %>' /></td>
<td width="25%"><asp:Label ID="Label4" runat="server"
Text='<%# Eval("EmpSal") %>' /></td>
</tr>
</table>
</asp:Panel>
<asp:HoverMenuExtender ID="hme2" runat="Server"
HoverCssClass="popupHover"
PopupControlID="PopupMenu"
PopupPosition="Right"
TargetControlID="Panel9"
PopDelay="25" />
</ItemTemplate>
<EditItemTemplate>
<asp:Panel ID="Panel9" runat="server" Width="100%">
<table width="100%">
<tr>
<td width="55%">EmpNo:<br /><asp:TextBox Font-Bold="true" ID="TextBox1" runat="server"
Text='<%# Eval("EmpNo") %>' Width="150" /></td>
<td width="55%">EmpName:<br /><asp:TextBox ID="TextBox2" runat="server"
Text='<%# Eval("EmpName") %>' Width="150" /></td>
<td width="55%">EmpJob:<br /><asp:TextBox ID="TextBox3" runat="server"
Text='<%# Eval("EmpJob") %>' Width="150" /></td>
<td width="55%">EmpSal:<br /><asp:TextBox ID="TextBox4" runat="server"
Text='<%# Eval("EmpSal") %>' Width="150" /></td>
</tr>
</table>
</asp:Panel>
<asp:HoverMenuExtender ID="hme1" runat="Server"
TargetControlID="Panel9"
PopupControlID="PopupMenu"
HoverCssClass="popupHover"
PopupPosition="Left" />
<asp:Panel ID="PopupMenu" runat="server" CssClass="popupMenu" Width="80">
<div style="border:1px outset white">
<asp:LinkButton ID="LinkButton1" runat="server"
CommandName="Update" Text="Update" />
<br />
<asp:LinkButton ID="LinkButton2" runat="server"
CommandName="Cancel" Text="Cancel" />
</div>
</asp:Panel>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel></td></tr>
</table>
</div>
</form>
</body>
</html>

Code Behind:

DatabaseModel.DatabaseEntities obj = new DatabaseModel.DatabaseEntities();
    Class1 obj3 = new Class1();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Getdata();
        }
    }
    public void Getdata()
    {
        DatabaseModel.DatabaseEntities obj = new DatabaseModel.DatabaseEntities();
        var x = (from n in obj.Emps select n);
        GridView1.DataSource = x;
        GridView1.DataBind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        Getdata();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox Id = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox1"));
        TextBox tbname = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox2"));
        TextBox tbjob = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox3"));
        TextBox tbsal = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox4"));
        DatabaseModel.Emp obj1 = new DatabaseModel.Emp();
        obj1.EmpNo = int.Parse(Id.Text);
        obj1.EmpName = tbname.Text;
        obj1.EmpJob = tbjob.Text;
        obj1.EmpSal = Convert.ToDecimal(tbsal.Text);
        obj3.Update(obj1);
        GridView1.EditIndex = -1;
        Getdata();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        Getdata();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Label Id = (Label)(GridView1.Rows[e.RowIndex].FindControl("Label1"));
        DatabaseModel.Emp obj2 = new DatabaseModel.Emp();
        obj2.EmpNo = int.Parse(Id.Text);
        obj3.Delete(obj2);
        Getdata();
        GridView1.EditIndex = -1;
    }

Code in Class1.cs:

    DatabaseModel.DatabaseEntities obj = new DatabaseModel.DatabaseEntities();
public Class1()
{
//
// TODO: Add constructor logic here
//
}

public void Update(DatabaseModel.Emp empdetails)
{
var x = (from n in obj.Emps
where n.EmpNo == empdetails.EmpNo
select n).FirstOrDefault();
if (x != null)
{
x.EmpName = empdetails.EmpName;
x.EmpJob = empdetails.EmpJob;
x.EmpSal = empdetails.EmpSal;
obj.SaveChanges();
}
}
public void Delete(DatabaseModel.Emp empid)
{
var x = (from n in obj.Emps
where n.EmpNo == empid.EmpNo
select n).FirstOrDefault();
if (x != null)
{
obj.DeleteObject(x);
obj.SaveChanges();
}
}

DownloadSamplecode

AJAX Modal Popup Extender for Gridview Edit Update Delete


Code in ASPX page:

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head id="Head1" runat="server">
<title></title>
<style type="text/css">
.modalBackground {
background-color:Gray;
filter:alpha(opacity=70);
opacity:0.7;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<div>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None">
<Columns>
<asp:BoundField DataField="EmpNo" ReadOnly="true"
HeaderText="EmpId"/>
<asp:BoundField DataField="EmpName" HeaderText="Empname" />
<asp:BoundField DataField="EmpJob" HeaderText="EmpJob" />
<asp:BoundField DataField="EmpSal" HeaderText="EmpSal" />
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server"
OnClick="ShowPopup">Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="White" />
<FooterStyle BackColor="#990000" Font-Bold="True"
ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True"
ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333"
HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True"
ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
<br />
<asp:Button ID="Button1" runat="server" Text="Button"
style="display:none" />
<asp:ModalPopupExtender ID="Button1_ModalPopupExtender"
runat="server" DynamicServicePath="" Enabled="True"
PopupControlID="Panel1" TargetControlID="Button1"
CancelControlID="Button3" BackgroundCssClass="modalBackground">
</asp:ModalPopupExtender>
<asp:Panel ID="Panel1" runat="server"
BorderColor="#990033" BorderWidth="2px">
<table class="style1" bgcolor="#FFFFCC">
<tr>
<td align="center" bgcolor="#FFFF99" colspan="2"
style="color: #FFFFFF; font-weight: 700;
font-size: large; background-color: #990033;">
Edit Details</td>
</tr>
<tr>
<td class="style5">
EmpId:</td>
<td class="style5">
<asp:Label ID="Id" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
EmpName:</td>
<td>
<asp:TextBox ID="TextBox2" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
EmpJob:</td>
<td>
<asp:TextBox ID="TextBox3" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
EmpSal:</td>
<td>
<asp:TextBox ID="TextBox4" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
&nbsp;</td>
<td>
<asp:Button ID="Button2" runat="server"
onclick="Button2_Click" Text="Update" />
<asp:Button ID="Button3" runat="server" Text="Cancel" />
</td>
</tr>
<tr>
<td colspan="2" style="background-color: #990033">
&nbsp;</td>
</tr>
</table>
</asp:Panel>
</div>
</form>
</body>
</html>

Code Behind:

DatabaseModel.DatabaseEntities obj = new DatabaseModel.DatabaseEntities();
    Class1 obj3 = new Class1();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetData();
        }
    }
    public void GetData()
    {
        var x = (from n in obj.Emps
                 select n);
        GridView1.DataSource = x;
        GridView1.DataBind();
    }
    public void ShowPopup(object sender, EventArgs e)
    {
        LinkButton lnk = sender as LinkButton;
        GridViewRow Row = (GridViewRow)lnk.NamingContainer;
        Id.Text = Row.Cells[0].Text;
        TextBox2.Text = Row.Cells[1].Text;
        TextBox3.Text = Row.Cells[2].Text;
        TextBox4.Text = Row.Cells[3].Text;
        this.Button1_ModalPopupExtender.Show();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        DatabaseModel.Emp emp = new DatabaseModel.Emp();
        int id = Convert.ToInt32(Id.Text);
        DatabaseModel.DatabaseEntities Model = new DatabaseModel.DatabaseEntities();
        var x = (from n in Model.Emps
                 where n.EmpNo == id
                 select n).FirstOrDefault();
        if (x != null)
        {
            x.EmpName = TextBox2.Text;
            x.EmpJob = TextBox3.Text;
            x.EmpSal = Convert.ToDecimal(TextBox4.Text);
            Model.SaveChanges();
            GetData();
        }
    }

DownloadSampleCode

if you are getting the following error:

A potentially dangerous Request.Path value was detected from the client(&).

Please click on following link

https://chikkanti.wordpress.com/2011/11/22/a-potentially-dangerous-request-path-value-was-detected-from-the-client/

FileUpload Validation for Images using RegularExpression


Code in ASPX page:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="FileUpload1"
ErrorMessage=".jpeg or .JPEG or .gif or .GIF or .png or .PNG Formats only"
ValidationExpression=
"(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*\.(?:jpe?g|gif|png))(?:\?([^#]*))?(?:#(.*))?">
</asp:RegularExpressionValidator>
</div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
style="font-weight: 700; color: #0000CC; background-color: #FFFF99"
Text="Upload" />
</form>
</body>
</html>

Code behind:

protected void Button1_Click(object sender, EventArgs e)
{
 if (FileUpload1.HasFile)
 {
     string str = FileUpload1.FileName;
     FileUpload1.PostedFile.SaveAs(MapPath("~/Uploads/" + str));
 }
}

DownloadSampleCode

Watermark in textbox using javascript


we are seen in so many websites like when we place our mouce pointer in textbox

it will show empty textbox… and when we take out mouse pointer it will show

textbox with text like “UserName” or “Searchhere”…

to make your textbox likethat…

write the following code

 

 

<asp:TextBox ID="TextBox1"
 runat="server"
 onblur="if(this.value == ''){ this.value = 'UserName';}" 
 onfocus="if(this.value == 'UserName'){this.value = '';}"
 Text="UserName"></asp:TextBox>

AJAX SlideShow Control


In this post we can learn how to do slideshow using ajax
Code in ASPX page:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        .style3
        {
            width: 353px;
        }
    </style>
</head>
<body>
<form id="form1" runat="server">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table>
<tr><td class="style3"></td>
<td align="center">
<asp:Label ID="Label1" runat="server"
Text="Label" style="font-size: x-large;
color: #FF3300; font-family: Arial, Helvetica, sans-serif;
font-weight: 700; background-color: #FFFFCC">
</asp:Label><br />
<asp:Image ID="Image1" runat="server" Height="299px"
Width="350px" />
<asp:SlideShowExtender ID="Image1_SlideShowExtender"
 runat="server" AutoPlay="True" Enabled="True"
 Loop="True" SlideShowServiceMethod="GetSlides"
SlideShowServicePath="" TargetControlID="Image1"
UseContextKey="True" ImageDescriptionLabelID="Label2"
ImageTitleLabelID="Label1" NextButtonID="Button3"
PlayButtonID="Button2" PlayButtonText="Play"
PlayInterval="1500" PreviousButtonID="Button1"
StopButtonText="Stop"></asp:SlideShowExtender>
<br /><asp:Label ID="Label2" runat="server" Text="Label"
style="font-size: x-large; color: #0000FF; font-family:
Verdana; background-color: #CCFFFF"></asp:Label><br />
<asp:Button ID="Button1" runat="server" Text="Previous" />
<asp:Button ID="Button2" runat="server" Text="Stop" />
<asp:Button ID="Button3" runat="server" Text="Next" />
</ContentTemplate>
</asp:UpdatePanel></td>
</tr>
</table>
</div>
</form>
</body>
</html>

Code behind:
[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static AjaxControlToolkit.Slide[] GetSlides(string contextKey)
    {
        AjaxControlToolkit.Slide[] slides = new AjaxControlToolkit.Slide[10];
        slides[0] = new AjaxControlToolkit.Slide("Images/0.jpg", "baby Image", "this is hd image from flikar");
        slides[1] = new AjaxControlToolkit.Slide("Images/1.jpg", "cutebaby image", "this is hd image from google");
        slides[2] = new AjaxControlToolkit.Slide("Images/2.jpg", "nicebaby", "this image from bing");
        slides[3] = new AjaxControlToolkit.Slide("Images/3.jpg", "goodbaby", "this image from asp.net info blog");
        slides[4] = new AjaxControlToolkit.Slide("Images/4.jpg", "nicebaby", "this also...");
        slides[5] = new AjaxControlToolkit.Slide("Images/5.jpg", "cutebaby", "this too...");
        slides[6] = new AjaxControlToolkit.Slide("Images/6.jpg", "smartbaby", "this one also...");
        //return default(AjaxControlToolkit.Slide[]);
        return slides;
    }

Download Sample Code

AJAX Autocomplete Extender using EDM


Code in ASPX page:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:TextBox ID="TextBox1" runat="server" Height="22px"
Width="378px"></asp:TextBox>
<cc1:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender"
 runat="server" DelimiterCharacters=""
  Enabled="True" MinimumPrefixLength="1"
ServiceMethod="GetCompletionList" ServicePath=""
 TargetControlID="TextBox1"
UseContextKey="True" CompletionInterval="10">
</cc1:AutoCompleteExtender>
<asp:Button ID="btnSearch" runat="server"
onclick="btnSearch_Click" Text="Search" />
<asp:GridView ID="GridView1" runat="server"
CellPadding="4" ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True"
ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True"
ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White"
HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True"
ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
</div>
</form>
</body>
</html>

Code behind:
[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static string[] GetCompletionList(string prefixText, int count, string contextKey)
    {
        DatabaseModel.DatabaseEntities obj = new DatabaseModel.DatabaseEntities();
        var x = (from n in obj.Items
                 where n.Description.ToUpper().Contains(prefixText.ToUpper())
                 select n.Description);
        return x.ToArray<string>();

    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        DatabaseModel.DatabaseEntities obj = new DatabaseModel.DatabaseEntities();
        var x = (from n in obj.Items
                 where n.Description.ToUpper().Contains(TextBox1.Text.ToUpper())
                 select n);
       GridView1.DataSource = x;
      GridView1.DataBind();
    }

Download Sample Code