Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • Last Modified:

Determining the control clicked on in datagrid


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
Diono
Asked:
Diono
1 Solution
 
skpatraCommented:
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
 
surajgupthaCommented:
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
 
DionoAuthor Commented:
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
Technology Partners: 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!

 
DionoAuthor Commented:
surajguptha, can you explain using code snippet and c#?
0
 
DionoAuthor Commented:
does this.Load refer to this instance of the app?
0
 
DotNetLover_BaanCommented:
>>   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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now