Solved

Determining the control clicked on in datagrid

Posted on 2004-10-08
6
276 Views
Last Modified: 2008-03-10

I want to add a 2 controls to a datagrid one with text = 'Add to Cart", and the other,
a textbox. The user must be able to add text to the textbox, press the 'Add to Cart"
button or label and have the cart updated in the background.

I have a problem determining the control('Add to Cart") clicked on in order to get the
ProductID(databound), and quantity(textbox unbound) on the same row. I need to update the
cart in a stored procedure using the ProductID and quantity, in an event somewhere.

My problem is 2 fold:-
  1. I don't know how to do determine the row clicked on and capture this in an event.
  2. I can't find a comprehensive reference anywhere explaining the events available
     for the various controls, and what I can access when, what is contained in Eventargs
     etc.

Many thanks,
Dion.
0
Comment
Question by:Diono
6 Comments
 
LVL 3

Accepted Solution

by:
skpatra earned 500 total points
ID: 12257081
I am attaching a code for a webform which has a datagrid. The datagrid has a textbox and a link button. On click of the buton, i am readfing the data fronm the corresponding row. Check out the code:

Webform1.aspx:
-------------------------
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="dgTrick.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
      <HEAD>
            <title>WebForm1</title>
            <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
            <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">
                  <asp:DataGrid id="dgAddItem" runat="server" Width="352px" AutoGenerateColumns="false">
                        <Columns>
                              <asp:BoundColumn Visible="False" DataField="ID"></asp:BoundColumn>
                              <asp:BoundColumn DataField="name" HeaderText="Name"></asp:BoundColumn>
                              <asp:TemplateColumn HeaderText="Quantity">
                                    <ItemTemplate>
                                          <asp:TextBox ID="txtQty" Runat="server"></asp:TextBox>
                                    </ItemTemplate>
                              </asp:TemplateColumn>
                              <asp:ButtonColumn Text="Add" HeaderText="add" CommandName="Add"></asp:ButtonColumn>
                        </Columns>
                  </asp:DataGrid>
                  <P>&nbsp;</P>
                  <P>&nbsp;</P>
                  <P>Selected:</P>
                  <asp:TextBox id="txtResult" runat="server" Width="296px" Height="112px" TextMode="MultiLine"></asp:TextBox></P>
            </form>
      </body>
</HTML>

code behind
------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
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 dgTrick
{
      /// <summary>
      /// Summary description for WebForm1.
      /// </summary>
      public class WebForm1 : System.Web.UI.Page
      {
            protected System.Web.UI.WebControls.TextBox txtResult;
            protected System.Web.UI.WebControls.DataGrid dgAddItem;
      
            private void Page_Load(object sender, System.EventArgs e)
            {
                  this.dgAddItem.ItemCommand += new DataGridCommandEventHandler(dgAddItem_ItemCommand);
                  if(!IsPostBack)
                  {
                        DataSet oData = new DataSet();
                        oData.Tables.Add();
                        oData.Tables[0].Columns.Add("ID");
                        oData.Tables[0].Columns.Add("Name");
                        oData.Tables[0].Rows.Add(new object[]{1, "Name1"});
                        oData.Tables[0].Rows.Add(new object[]{2, "Name2"});
                        oData.Tables[0].Rows.Add(new object[]{3, "Name3"});
                        oData.Tables[0].Rows.Add(new object[]{4, "Name4"});
                        
                        dgAddItem.DataSource = oData.Tables[0];
                        dgAddItem.DataBind();
                  }
            }

            private void dgAddItem_ItemCommand(object o, DataGridCommandEventArgs e)
            {
                  string sID = e.Item.Cells[0].Text;
                  string sName = e.Item.Cells[1].Text;
                  string sQty = ((TextBox)(e.Item.Cells[2].FindControl("txtQty"))).Text;
                  txtResult.Text = sID + ";" + sName + ";" + sQty;
            }

            #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
      }
}
0
 
LVL 21

Expert Comment

by:surajguptha
ID: 12257269
When datagrid controls are rendered. They are rendered like dg_ctl#_ctrl#. So now when the button is clicked. We can try parsing the number and finding other controls with the same number in javascript.
0
 

Author Comment

by:Diono
ID: 12257407
Please explain this to me. I am currently using Web Matrix with c#.

          #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
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Diono
ID: 12257500
surajguptha, can you explain using code snippet and c#?
0
 

Author Comment

by:Diono
ID: 12257515
does this.Load refer to this instance of the app?
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12257935
>>   1. I don't know how to do determine the row clicked on and capture this in an event.
        2. I can't find a comprehensive reference anywhere explaining the events available
         for the various controls, and what I can access when, what is contained in Eventargs
         etc.
>>

Lets see... for this kind of problems, use "Select column" for the datagrid, set the text as "Add to cart". Choices are Hyperlink, or Button. Select whatever you want. Now, the datagrid has an event, "ItemCommand" that is invoked when the button in the select column is clicked. There you can get the row index, and the values inside its cells. Visit this link:
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWebUIWebControlsDataGridClassItemCommandTopic.asp

-Baan
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now