Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Determining the control clicked on in datagrid

Posted on 2004-10-08
6
Medium Priority
?
286 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
Independent Software Vendors: 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

618 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