Tuesday, August 27, 2013

GridView OnCommad Event and Javascript window

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Gv3.aspx.cs" Inherits="DUMMY_Gv3" %>
<%--<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>--%>
<!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>
    <script type="text/javascript">
        //        function ShowEditForms(id, rowIndex) {
        //            window.radopen("EditUserDetail.aspx?UserID=" + id, "UserListDialog");
        //            return false;
        //        }
        function popupwindow(url, title, w, h) {
            var left = (screen.width / 2) - (w / 2);
            var top = (screen.height / 2) - (h / 2);
            return window.open(url, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
        } 
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="Sc1" runat="server">
    </asp:ScriptManager>
    <div>
        <asp:GridView ID="GridView1" runat="server" DataKeyNames="AnnouncementID" AutoGenerateColumns="false"
            OnRowDataBound="GridView1_OnRowDataBound" OnRowCommand="GridView1_OnRowCommand"
            OnPageIndexChanging="OnPaging" AllowPaging="true" PageSize="10">
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#EFF3FB" />
            <EditRowStyle BackColor="#2ff1BF" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label ID="lblItemIndex" runat="server" Text='<%#Container.DataItemIndex + 1 %>'>
                        </asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="ID">
                    <ItemTemplate>
                        <asp:Label ID="lblAnnouncementID" runat="server" Text='<%#Eval("AnnouncementID") %>'
                            Visible="false"></asp:Label>
                        <asp:LinkButton ID="lnkAnnouncementID" runat="server" Text="Edit" CommandName="Editing"
                            CommandArgument='<%#Eval("AnnouncementID") %>'></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Type">
                    <ItemTemplate>
                        <asp:Label ID="lblAnnouncementType" runat="server"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Title">
                    <ItemTemplate>
                        <asp:Label ID="lblTitle" runat="server"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Message">
                    <ItemTemplate>
                        <asp:Label ID="lblMessage" runat="server"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:HyperLink ID="EditLinks" runat="server" Text="Edit"></asp:HyperLink>
                        <asp:LinkButton ID="LinkButton1" runat="server" Text="Edits"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <EmptyDataTemplate>
                There are no Datas Available...
            </EmptyDataTemplate>
        </asp:GridView>
        
    </div>
    </form>
</body>
</html>


 



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class DUMMY_Gv3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }
    private void BindGrid()
    {
        List<Announcement> objAnnouncement = AnnouncementProvider.GetAll();
        GridView1.DataSource = objAnnouncement;
        GridView1.DataBind();
    }
    protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Announcement rowData = (Announcement)e.Row.DataItem;
            LinkButton lnkAnnouncementID = (LinkButton)e.Row.FindControl("lnkAnnouncementID");
            Label lblAnnouncementType = (Label)e.Row.FindControl("lblAnnouncementType");
            Label lblTitle = (Label)e.Row.FindControl("lblTitle");
            Label lblMessage = (Label)e.Row.FindControl("lblMessage");
            lnkAnnouncementID.Text = Convert.ToString(rowData.AnnouncementId);
            lblAnnouncementType.Text = Convert.ToString(rowData.AnnouncementType);
            lblTitle.Text = Convert.ToString(rowData.Title);
            lblMessage.Text = Convert.ToString(rowData.Message);
            //HyperLink editLinks = (HyperLink)e.Row.FindControl("EditLinks");
            //editLinks.Attributes["href"] = "javascript:void(0);";
            //editLinks.Attributes.Add("onclick", String.Format("return ShowEditForms('{0}','{1}');", rowData.AnnouncementId, e.Row.RowIndex));
            //LinkButton LinkButton1 = (LinkButton)e.Row.FindControl("LinkButton1");
            //LinkButton1.Attributes.Add("onclick", String.Format("return ShowEditForms('{0}','{1}');", rowData.AnnouncementId, e.Row.RowIndex));
            HyperLink editLinks = (HyperLink)e.Row.FindControl("EditLinks");
            editLinks.Attributes["href"] = "javascript:void(0);";
            editLinks.Attributes.Add("onclick", String.Format("return popupwindow('{0}','{1}','{2}','{3}');", "EditUserDetail.aspx", "Edit", "500", "500"));
            LinkButton LinkButton1 = (LinkButton)e.Row.FindControl("LinkButton1");
            LinkButton1.Attributes.Add("onclick", String.Format("return popupwindow('{0}','{1}','{2}','{3}');", "EditUserDetail.aspx", "Edit", "500", "500"));
        }
    }
    protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
    {
        string CommandName = e.CommandName;
        if (CommandName != "Page") //because for page page index will handle it. 
        {
            Guid ID = new Guid(Convert.ToString(e.CommandArgument));
            switch (CommandName)
            {
                case "Editing":
                     // do stuff here... (window open,page redirection,or editing)
                    break;
            }
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        BindGrid();
    }
    protected void OnPaging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        //GridView1.DataBind();
        BindGrid();
    }
}

0 comments:

Post a Comment