How To Open GridView in Div

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewandDiv.aspx.cs" Inherits="GridViewandDiv" %>

<!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 id="Head1" runat="server">
   <title>Untitled Page</title>
</head>
<body id="pgBody" runat="server">
   <form id="form1" runat="server">
       <div>
           <table runat="server" id="table1" class="bar">
               <tr>
                   <td>
                       <asp:Label ID="LabelProjects" Text="Projects:" runat="server"></asp:Label>
                   </td>
                   <td>
                       <asp:TextBox ID="TextBoxProjects" runat="server"></asp:TextBox>
                       <asp:Button ID="btnOpen" runat="Server" CausesValidation="False" Text="Open Grid" />
                   </td>
               </tr>
               <tr>
                   <td colspan="1">
                   </td>
                   <td>
                       <div id="splat" name="splat" runat="server" style="display: block">
                           <asp:GridView Width="151px" ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False">
                               <Columns>
                                   <asp:TemplateField HeaderText="Select All">
                                       <HeaderTemplate>
                                           <input id="CheckboxSelectAll" onclick="SelectAllCheckboxes(this)" type="checkbox" />
                                           Select All
                                       </HeaderTemplate>
                                       <ItemTemplate>
                                           <asp:CheckBox ID="Chk" runat="server" Text='<%# Eval("NAME") %>' AutoPostBack="true"
                                               OnCheckedChanged="Chk_CheckedChanged" />
                                       </ItemTemplate>
                                   </asp:TemplateField>
                               </Columns>
                           </asp:GridView>
                       </div>
                   </td>
               </tr>
           </table>
       </div>
   </form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class GridViewandDiv : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            GridView1.DataSource = GetDataTable();
            GridView1.DataBind();
        }
        pgBody.Attributes.Add("onclick", ClientScript.GetPostBackEventReference(Page, "CustomPostBack"));

        if (Request["__EVENTARGUMENT"] == "CustomPostBack" && splat.Style["display"] == "block")
        {
            splat.Style.Remove("display");
            splat.Style.Add("display", "none");
        }
        else
        {
            if (Request["__EVENTARGUMENT"] == "CustomPostBack")
            {
                splat.Style.Remove("display");
                splat.Style.Add("display", "block");
            }
        }
    }
    private DataTable GetDataTable()
    {
        //create table
        DataTable dt = new DataTable("Project");
        dt.Columns.Add("ID", Type.GetType("System.Int32"));
        dt.Columns.Add("NAME", Type.GetType("System.String"));

        //create fields
        DataColumn[] pk = new DataColumn[1];
        pk[0] = dt.Columns["ID"];
        dt.PrimaryKey = pk;
        dt.Columns["ID"].AutoIncrement = true;
        dt.Columns["ID"].AutoIncrementSeed = 1;
        dt.Columns["ID"].ReadOnly = true;

        //fill rows
        DataRow dr;
        for (int x = 1; x <= 10; x++)
        {
            //make every other one different
            if (Math.IEEERemainder(x, 2) == 0)
            {
                dr = dt.NewRow();
                dr["Name"] = "Riss1";

                dt.Rows.Add(dr);
            }
            else
            {
                dr = dt.NewRow();
                dr["Name"] = "Riss2";

                dt.Rows.Add(dr);

            }
        }

        return dt;
    }
   
    protected void Chk_CheckedChanged(object sender, EventArgs e)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        foreach(GridViewRow  gv in GridView1.Rows)
        {
            CheckBox chk = gv.FindControl("chk") as CheckBox;
            if (chk.Checked)
            {

                sb.Append(chk.Text);
                sb.Append(";");

            }


        }
        TextBoxProjects.Text = sb.ToString();
    }
}

No comments:

Post a Comment