RE: datagrid event handler

Discussion in 'ASP .Net' started by Bassel Tabbara [MSFT], Jun 27, 2003.

  1. Hello Manan,
    I am including below a sample on this:

    WebForm1.aspx
    <%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
    AutoEventWireup="false"
    Inherits="ChunyeeJohnLeungSample.WebForm1" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>WebForm1</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema"
    content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body>
    <form id="Form1" method="post" runat="server">
    <P>
    <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
    <Columns>
    <asp:TemplateColumn HeaderText="Include Product">
    <ItemTemplate>
    <asp:CheckBox id=CheckBox1 runat="server" Text='<%#
    DataBinder.Eval(Container.DataItem, "title_id") %>'>
    </asp:CheckBox>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Book Title">
    <ItemTemplate>
    <asp:Literal id=Literal1 runat="server" Text='<%#
    DataBinder.Eval(Container.DataItem, "title") %>'>
    </asp:Literal>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid></P>
    <P>
    <asp:TextBox id="TextBox1" runat="server" Height="251px" Width="881px"
    TextMode="MultiLine"></asp:TextBox></P>
    <P>
    <asp:Button id="Button1" runat="server" Text="Button"></asp:Button></P>
    <P>&nbsp;</P>
    <P>&nbsp;</P>
    </form>
    </body>
    </HTML>

    WebForm1.aspx.cs
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    namespace ChunyeeJohnLeungSample
    {
    /// <summary>
    /// Summary description for WebForm1.
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.TextBox TextBox1;
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Web.UI.WebControls.DataGrid DataGrid1;
    public System.Web.UI.WebControls.DataGrid mygrid;
    private void Page_Load(object sender, System.EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    SqlConnection conn = new SqlConnection("server=(local); database=pubs;
    Integrated Security=SSPI");
    SqlDataAdapter da = new SqlDataAdapter("select * from titles", conn);
    DataSet ds = new DataSet();
    da.Fill(ds, "titles");
    DataGrid1.DataSource = ds;
    DataGrid1.DataBind();
    }
    else
    {
    GetSelectedData();
    }
    // Put user code to initialize the page here
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    public void GetSelectedData()
    {
    CheckBox cb;
    Literal holder;

    foreach(DataGridItem dgi in this.DataGrid1.Items)
    {
    cb = (CheckBox) dgi.Cells[0].Controls[1];
    if(cb.Checked)
    {
    //yes, then write the text from that field out
    //get a reference to the control in the datagrid
    holder = (Literal) dgi.Cells[1].Controls[1];
    //write the text of the referenced control to the textbox
    TextBox1.Text = TextBox1.Text + holder.Text + "\n";
    }
    }
    }

    }
    }

    For more information refers to the following Kb article:

    306227 HOW TO: Use a CheckBox Web Control in a DataGrid in Visual Studio
    .NET
    http://support.microsoft.com/?id=306227

    320707 HOW TO: Loop Through and Examine CheckBox Control Values in a
    DataGrid
    http://support.microsoft.com/?id=320707

    Thanks,
    Bassel Tabbara
    Microsoft, ASP.NET

    This posting is provided "AS IS", with no warranties, and confers no rights.


    --------------------
    | Content-Class: urn:content-classes:message
    | From: "Manan" <>
    | Sender: "Manan" <>
    | Subject: datagrid event handler
    | Date: Thu, 26 Jun 2003 07:17:33 -0700
    | Lines: 29
    | Message-ID: <09f901c33bed$af778740$>
    | MIME-Version: 1.0
    | Content-Type: text/plain;
    | charset="iso-8859-1"
    | Content-Transfer-Encoding: 7bit
    | X-Newsreader: Microsoft CDO for Windows 2000
    | Thread-Index: AcM77a918YkunpZ9RLmNqxRWwq7lQg==
    | X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
    | Newsgroups: microsoft.public.dotnet.framework.aspnet
    | Path: cpmsftngxa06.phx.gbl
    | Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:155110
    | NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    |
    | I have created the checkbox dyanamically in the datagrid
    | using the ItemBound property whenever user click the
    | checkbox i want to do post back and call the event
    | handler. Could you please guide me to the right direction.
    | If any examples that help is appreciated.
    |
    | Thanks
    | Manan
    |
    | ============
    | page.aspx.cs
    | ============
    | public void ItemBound(object sender, DataGridItemEventArgs
    | e)
    | {
    | if ((e.Item.ItemType == ListItemType.Item) ||
    | (e.Item.ItemType == ListItemType.AlternatingItem))
    | {
    | DataRowView drv = (DataRowView) e.Item.DataItem;
    | mycheckbox = new CheckBox();
    | mycheckbox.AutoPostBack = true;
    | mycheckbox.AutoPostBack = true;
    | mycheckbox.EnableViewState = true;
    | mycheckbox.ID = e.Item.ItemIndex.ToString();
    | e.Item.Cells[0].Controls.Add(mycheckbox);
    | }
    | }
    |
    |
    |
     
    Bassel Tabbara [MSFT], Jun 27, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sonic
    Replies:
    1
    Views:
    553
    John Saunders
    Jan 7, 2005
  2. Replies:
    1
    Views:
    756
    Damien
    Feb 22, 2007
  3. RC

    How to recall add event from an Event handler??

    RC, Jan 6, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    296
    John Saunders
    Jan 6, 2005
  4. Adi
    Replies:
    2
    Views:
    206
  5. Replies:
    0
    Views:
    165
Loading...

Share This Page