How to get the datagrid items on button click event?
-
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) -
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)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.
-
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)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.
-
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.
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)
-
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)
<%@ 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 ArrayListlist.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 tofalse
.