Solved

Determining the control clicked on in datagrid

Posted on 2004-10-08
6
284 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 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

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 …
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

717 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