Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to get the datagrid items on button click event?

How to get the datagrid items on button click event?

Scheduled Pinned Locked Moved ASP.NET
csharpwpfwcfhelptutorial
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SIJUTHOMASP
    wrote on last edited by
    #1

    Hai, I have a datagrid having datasource from an arraylist.The datagrid is binding in the Page_Load.But when I try to get the items from the datagrid on a button click event,I am not able to get any items.What might be the problem?The code snippet I have used in button onclick event is given below For Each d1 In dataGrid1.Items Dim t1 As TextBox t1 = CType(d1.FindControl("txtColor"), TextBox) Next But I am getting the datagrid items in 'itemcreated' event of datagrid. How ican get the datagrid items in a button click event. I am sure somebody can show me the right way.Thanks much. Thank You, Rahul.P.Menon. SoftwareDeveloper(.NET)

    M 2 Replies Last reply
    0
    • S SIJUTHOMASP

      Hai, I have a datagrid having datasource from an arraylist.The datagrid is binding in the Page_Load.But when I try to get the items from the datagrid on a button click event,I am not able to get any items.What might be the problem?The code snippet I have used in button onclick event is given below For Each d1 In dataGrid1.Items Dim t1 As TextBox t1 = CType(d1.FindControl("txtColor"), TextBox) Next But I am getting the datagrid items in 'itemcreated' event of datagrid. How ican get the datagrid items in a button click event. I am sure somebody can show me the right way.Thanks much. Thank You, Rahul.P.Menon. SoftwareDeveloper(.NET)

      M Offline
      M Offline
      Mike L
      wrote on last edited by
      #2

      Here's what I've got: This handler: this.dataGrid.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgItem_Command); And this code: private void dgItem_Command(object sender, DataGridCommandEventArgs e) { switch(((LinkButton)e.CommandSource).CommandName) { case "Delete": case "Edit": case "Select": DGResponder(((LinkButton)e.CommandSource).CommandName, e); break; default: break; } } void DGResponder(string cmdType, DataGridCommandEventArgs e) { if((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem)) { // Retrieve the text of the Unique value from the DataGridItem // In: Convert.ToInt64(e.Item.Cells[3].Text) or whichever 0-based column(cell) // Get the type of item - delete, select, update, etc. // In: cmdType Response.Redirect("JobDetails.aspx"); } } Hope that helps... Mike L.

      1 Reply Last reply
      0
      • S SIJUTHOMASP

        Hai, I have a datagrid having datasource from an arraylist.The datagrid is binding in the Page_Load.But when I try to get the items from the datagrid on a button click event,I am not able to get any items.What might be the problem?The code snippet I have used in button onclick event is given below For Each d1 In dataGrid1.Items Dim t1 As TextBox t1 = CType(d1.FindControl("txtColor"), TextBox) Next But I am getting the datagrid items in 'itemcreated' event of datagrid. How ican get the datagrid items in a button click event. I am sure somebody can show me the right way.Thanks much. Thank You, Rahul.P.Menon. SoftwareDeveloper(.NET)

        M Offline
        M Offline
        Mike L
        wrote on last edited by
        #3

        Here's what I've got: This handler: this.dataGrid.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgItem_Command); And this code: private void dgItem_Command(object sender, DataGridCommandEventArgs e) { switch(((LinkButton)e.CommandSource).CommandName) { case "Delete": case "Edit": case "Select": DGResponder(((LinkButton)e.CommandSource).CommandName, e); break; default: break; } } void DGResponder(string cmdType, DataGridCommandEventArgs e) { if((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem)) { // Retrieve the text of the Unique value from the DataGridItem // In: Convert.ToInt64(e.Item.Cells[3].Text) or whichever 0-based column(cell) // Get the type of item - delete, select, update, etc. // In: cmdType Response.Redirect(/*Page to display on*/); } } Hope that helps... Mike L.

        S 1 Reply Last reply
        0
        • M Mike L

          Here's what I've got: This handler: this.dataGrid.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgItem_Command); And this code: private void dgItem_Command(object sender, DataGridCommandEventArgs e) { switch(((LinkButton)e.CommandSource).CommandName) { case "Delete": case "Edit": case "Select": DGResponder(((LinkButton)e.CommandSource).CommandName, e); break; default: break; } } void DGResponder(string cmdType, DataGridCommandEventArgs e) { if((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem)) { // Retrieve the text of the Unique value from the DataGridItem // In: Convert.ToInt64(e.Item.Cells[3].Text) or whichever 0-based column(cell) // Get the type of item - delete, select, update, etc. // In: cmdType Response.Redirect(/*Page to display on*/); } } Hope that helps... Mike L.

          S Offline
          S Offline
          SIJUTHOMASP
          wrote on last edited by
          #4

          Hai Mike, Thanks much for your reply.But I need the solution for something different. Let me explain my problem once more. The link button is not inside the datagrid. I want the textbox values on the link button click.But when we click on the link button it is posting back to the page load and go to the click event. If I give the data grid binding inside the condition of 'isnotpostback' again I couldnot get the datagrid items. That is datagrid.items.count=0 Obviously if I give outside the 'isnotpostback' I will get the datagrid items but the value entered in the textboxes cannot get.Because it is rebinded. I really want the textbox values inside the datagrid on linkbutton(outside datagrid) click What I should do? Any solution? Thank you very much. Rahul p Menon. SoftwareDeveloper(.NET)

          M 1 Reply Last reply
          0
          • S SIJUTHOMASP

            Hai Mike, Thanks much for your reply.But I need the solution for something different. Let me explain my problem once more. The link button is not inside the datagrid. I want the textbox values on the link button click.But when we click on the link button it is posting back to the page load and go to the click event. If I give the data grid binding inside the condition of 'isnotpostback' again I couldnot get the datagrid items. That is datagrid.items.count=0 Obviously if I give outside the 'isnotpostback' I will get the datagrid items but the value entered in the textboxes cannot get.Because it is rebinded. I really want the textbox values inside the datagrid on linkbutton(outside datagrid) click What I should do? Any solution? Thank you very much. Rahul p Menon. SoftwareDeveloper(.NET)

            M Offline
            M Offline
            minhpc_bk
            wrote on last edited by
            #5

            <%@ Page Language="vb" %>
            <%@ Import Namespace="System.Collections" %>
            <HTML>
            <HEAD>
            <title>WebForm1</title>
            <script language="VB" runat="server">
            Function CreateDataSource() As ArrayList
            Dim list As ArrayList = New ArrayList

            	list.Add("a")
            	list.Add("b")
            	list.Add("c")
            	
            	Return list
                End Function 'CreateDataSource
                
                Sub Page\_Load(sender As Object, e As EventArgs)
            	
            	If Not IsPostBack Then
            	    ' Need to load this data only once.
            	    DataGrid1.DataSource = CreateDataSource()
            	    DataGrid1.DataBind()
            	End If
                End Sub 'Page\_Load
            	
                Sub Button\_OnClick	(sender As Object, e As EventArgs)
            		Dim d1 as DataGridItem
            		For Each d1 In DataGrid1.Items
            		Dim t1 As TextBox
            		t1 = CType(d1.FindControl("txtColor"), TextBox)
            		Response.Write(t1.Text + "<br>")
            		Next
                End Sub 'Button\_OnClick	
            
            </script>
            </HEAD>
            <body MS\_POSITIONING="GridLayout">
            	<form id="Form1" method="post" runat="server">
            		<asp:DataGrid AutoGenerateColumns="False" id="DataGrid1"  runat="server">
            			<Columns>	
            				<asp:TemplateColumn>
            					<ItemTemplate>
            						<asp:TextBox id="txtColor" Runat="server"></asp:TextBox>
            					</ItemTemplate>
            				</asp:TemplateColumn>
            			</Columns>
            		</asp:DataGrid>
            		<br>
            		<asp:Button OnClick="Button\_OnClick" id="Button1" runat="server" Text="Button"></asp:Button>
            	</form>
            </body>
            

            </HTML>

            Above is a very simple example that demonstrates what you want. If you bind the datasource to the grid every time the page is visited, then you have to set the EnableViewState property of the grid to false.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups