How to catch or reserve the value of the controls that were created by dynamic?
-
Hi,there. I am a new guy of asp.net:-O and hope to learn it better. I hope to use the datagrid to bind data and change its editmode by dynamic. I do this because my datasource is unknow and I won't know how many columns will the datasource have,either. That's why I need to make the datagrid dynamic. My code is below. I write some code at the onitembound event of the datagrid, and catch the edititemindex of the datagrid if it is not -1. Then, I catch the edititem and its controls, and replace it, change its cells columnspan..... But,however :(,I don't know how to reserve the value of the controls that I created:doh:. Because of the cycle of Asp.Net, all of them will dispear before the datagrid.updatecommand events was fired. I am still not very familiar with the cycle of asp.net and its events:confused:. Can someone teach me what should I do in the next step or this is still a wrong way:)? <%@ Page Language="VB" AutoEventWireup="True"%> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> Function CreateDataSource() As ICollection ' Create sample data for the DataGrid control. Dim dt As DataTable = New DataTable() Dim dr As DataRow ' Define the columns of the table. dt.Columns.Add(New DataColumn("IntegerValue", GetType(Integer))) dt.Columns.Add(New DataColumn("StringValue", GetType(String))) dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double))) ' Populate the table with sample values. Dim i As Integer For i = 0 to 8 dr = dt.NewRow() dr(0) = i dr(1) = "Item " & i.ToString() dr(2) = 1.23 * (i + 1) dt.Rows.Add(dr) Next i ' Create a DataView from the DataTable. Dim dv As DataView = New DataView(dt) Return dv End Function sub binddata() ItemsGrid.DataSource = CreateDataSource() ItemsGrid.DataBind() end sub Sub Page_Load(sender As Object, e As EventArgs) ' Load sample data only once when the page is first loaded. If Not IsPostBack Then binddata() End If End Sub 'I catch the edititemindex and change it below. sub itemsgridbound(sender as object, e as DataGridItemEventArgs) if itemsgrid.edititemindex <> -1 then if e.item.itemindex</x-turndown>
-
Hi,there. I am a new guy of asp.net:-O and hope to learn it better. I hope to use the datagrid to bind data and change its editmode by dynamic. I do this because my datasource is unknow and I won't know how many columns will the datasource have,either. That's why I need to make the datagrid dynamic. My code is below. I write some code at the onitembound event of the datagrid, and catch the edititemindex of the datagrid if it is not -1. Then, I catch the edititem and its controls, and replace it, change its cells columnspan..... But,however :(,I don't know how to reserve the value of the controls that I created:doh:. Because of the cycle of Asp.Net, all of them will dispear before the datagrid.updatecommand events was fired. I am still not very familiar with the cycle of asp.net and its events:confused:. Can someone teach me what should I do in the next step or this is still a wrong way:)? <%@ Page Language="VB" AutoEventWireup="True"%> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> Function CreateDataSource() As ICollection ' Create sample data for the DataGrid control. Dim dt As DataTable = New DataTable() Dim dr As DataRow ' Define the columns of the table. dt.Columns.Add(New DataColumn("IntegerValue", GetType(Integer))) dt.Columns.Add(New DataColumn("StringValue", GetType(String))) dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double))) ' Populate the table with sample values. Dim i As Integer For i = 0 to 8 dr = dt.NewRow() dr(0) = i dr(1) = "Item " & i.ToString() dr(2) = 1.23 * (i + 1) dt.Rows.Add(dr) Next i ' Create a DataView from the DataTable. Dim dv As DataView = New DataView(dt) Return dv End Function sub binddata() ItemsGrid.DataSource = CreateDataSource() ItemsGrid.DataBind() end sub Sub Page_Load(sender As Object, e As EventArgs) ' Load sample data only once when the page is first loaded. If Not IsPostBack Then binddata() End If End Sub 'I catch the edititemindex and change it below. sub itemsgridbound(sender as object, e as DataGridItemEventArgs) if itemsgrid.edititemindex <> -1 then if e.item.itemindex</x-turndown>
As you are adding controls dynamically and wants to access them after PostBack, you need to create them again after the PostBack Check http://www.codeproject.com/Purgatory/ViewState\_\_\_Dynamic\_Cntrl.asp Sanjay Sansanwal www.sansanwal.com
-
Hi,there. I am a new guy of asp.net:-O and hope to learn it better. I hope to use the datagrid to bind data and change its editmode by dynamic. I do this because my datasource is unknow and I won't know how many columns will the datasource have,either. That's why I need to make the datagrid dynamic. My code is below. I write some code at the onitembound event of the datagrid, and catch the edititemindex of the datagrid if it is not -1. Then, I catch the edititem and its controls, and replace it, change its cells columnspan..... But,however :(,I don't know how to reserve the value of the controls that I created:doh:. Because of the cycle of Asp.Net, all of them will dispear before the datagrid.updatecommand events was fired. I am still not very familiar with the cycle of asp.net and its events:confused:. Can someone teach me what should I do in the next step or this is still a wrong way:)? <%@ Page Language="VB" AutoEventWireup="True"%> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> Function CreateDataSource() As ICollection ' Create sample data for the DataGrid control. Dim dt As DataTable = New DataTable() Dim dr As DataRow ' Define the columns of the table. dt.Columns.Add(New DataColumn("IntegerValue", GetType(Integer))) dt.Columns.Add(New DataColumn("StringValue", GetType(String))) dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double))) ' Populate the table with sample values. Dim i As Integer For i = 0 to 8 dr = dt.NewRow() dr(0) = i dr(1) = "Item " & i.ToString() dr(2) = 1.23 * (i + 1) dt.Rows.Add(dr) Next i ' Create a DataView from the DataTable. Dim dv As DataView = New DataView(dt) Return dv End Function sub binddata() ItemsGrid.DataSource = CreateDataSource() ItemsGrid.DataBind() end sub Sub Page_Load(sender As Object, e As EventArgs) ' Load sample data only once when the page is first loaded. If Not IsPostBack Then binddata() End If End Sub 'I catch the edititemindex and change it below. sub itemsgridbound(sender as object, e as DataGridItemEventArgs) if itemsgrid.edititemindex <> -1 then if e.item.itemindex</x-turndown>
Or, an alternative answer to S Sansanwal would be to examine the
Request.Form
collection which will contain the values of the controls. You will need to give each of your dynamically created controls a unique ID so that you can pick them out of the collection.
"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!
-
Hi,there. I am a new guy of asp.net:-O and hope to learn it better. I hope to use the datagrid to bind data and change its editmode by dynamic. I do this because my datasource is unknow and I won't know how many columns will the datasource have,either. That's why I need to make the datagrid dynamic. My code is below. I write some code at the onitembound event of the datagrid, and catch the edititemindex of the datagrid if it is not -1. Then, I catch the edititem and its controls, and replace it, change its cells columnspan..... But,however :(,I don't know how to reserve the value of the controls that I created:doh:. Because of the cycle of Asp.Net, all of them will dispear before the datagrid.updatecommand events was fired. I am still not very familiar with the cycle of asp.net and its events:confused:. Can someone teach me what should I do in the next step or this is still a wrong way:)? <%@ Page Language="VB" AutoEventWireup="True"%> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> Function CreateDataSource() As ICollection ' Create sample data for the DataGrid control. Dim dt As DataTable = New DataTable() Dim dr As DataRow ' Define the columns of the table. dt.Columns.Add(New DataColumn("IntegerValue", GetType(Integer))) dt.Columns.Add(New DataColumn("StringValue", GetType(String))) dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double))) ' Populate the table with sample values. Dim i As Integer For i = 0 to 8 dr = dt.NewRow() dr(0) = i dr(1) = "Item " & i.ToString() dr(2) = 1.23 * (i + 1) dt.Rows.Add(dr) Next i ' Create a DataView from the DataTable. Dim dv As DataView = New DataView(dt) Return dv End Function sub binddata() ItemsGrid.DataSource = CreateDataSource() ItemsGrid.DataBind() end sub Sub Page_Load(sender As Object, e As EventArgs) ' Load sample data only once when the page is first loaded. If Not IsPostBack Then binddata() End If End Sub 'I catch the edititemindex and change it below. sub itemsgridbound(sender as object, e as DataGridItemEventArgs) if itemsgrid.edititemindex <> -1 then if e.item.itemindex</x-turndown>
-
Hi,there. I am a new guy of asp.net:-O and hope to learn it better. I hope to use the datagrid to bind data and change its editmode by dynamic. I do this because my datasource is unknow and I won't know how many columns will the datasource have,either. That's why I need to make the datagrid dynamic. My code is below. I write some code at the onitembound event of the datagrid, and catch the edititemindex of the datagrid if it is not -1. Then, I catch the edititem and its controls, and replace it, change its cells columnspan..... But,however :(,I don't know how to reserve the value of the controls that I created:doh:. Because of the cycle of Asp.Net, all of them will dispear before the datagrid.updatecommand events was fired. I am still not very familiar with the cycle of asp.net and its events:confused:. Can someone teach me what should I do in the next step or this is still a wrong way:)? <%@ Page Language="VB" AutoEventWireup="True"%> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> Function CreateDataSource() As ICollection ' Create sample data for the DataGrid control. Dim dt As DataTable = New DataTable() Dim dr As DataRow ' Define the columns of the table. dt.Columns.Add(New DataColumn("IntegerValue", GetType(Integer))) dt.Columns.Add(New DataColumn("StringValue", GetType(String))) dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double))) ' Populate the table with sample values. Dim i As Integer For i = 0 to 8 dr = dt.NewRow() dr(0) = i dr(1) = "Item " & i.ToString() dr(2) = 1.23 * (i + 1) dt.Rows.Add(dr) Next i ' Create a DataView from the DataTable. Dim dv As DataView = New DataView(dt) Return dv End Function sub binddata() ItemsGrid.DataSource = CreateDataSource() ItemsGrid.DataBind() end sub Sub Page_Load(sender As Object, e As EventArgs) ' Load sample data only once when the page is first loaded. If Not IsPostBack Then binddata() End If End Sub 'I catch the edititemindex and change it below. sub itemsgridbound(sender as object, e as DataGridItemEventArgs) if itemsgrid.edititemindex <> -1 then if e.item.itemindex</x-turndown>