?
Solved

Determining the control clicked on in datagrid

Posted on 2004-10-08
6
Medium Priority
?
285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 3

Accepted Solution

by:
skpatra earned 2000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

765 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