How to create an array of Web Controls

<asp:PlaceHolder id=’PlaceHolder1′ runat=’server’></asp:PlaceHolder> VB.NET Dim textboxes(5) As TextBox Dim i As Integer For i = 0 To 4 textboxes(i) = New TextBox() textboxes(i).ID = ‘TextBox’ + i textboxes(i).AutoPostBack = True PlaceHolder1.Controls.Add(textboxes(i)) Next C# TextBox[] textboxes = new TextBox[5]; for (int i=0; i<5; i++) { textboxes[i] = new TextBox(); textboxes[i].ID = ‘TextBox’ + i; textboxes[i].AutoPostBack = true; PlaceHolder1.Controls.Add(textboxes[i]); }

How to add a TemplateColumn dynamically to the datagrid

<asp:DataGrid id=’DataGrid1′ AutoGenerateColumns=False runat=’server’></asp:DataGrid> VB.NET Create class Public Class newLabelColumn Implements ITemplate Public Sub New() End Sub ’New ’Add constructor stuff here Public Sub InstantiateIn(ByVal container As Control) Implements System.Web.UI.ITemplate.InstantiateIn Dim label1 As New Label AddHandler label1.DataBinding, AddressOf Me.BindLabelColumn container.Controls.Add(label1) End Sub ’InstantiateIn Public Sub BindLabelColumn(ByVal sender As Object, ByVal e As EventArgs) Dim lbl As Label = CType(sender, Label) Dim container As DataGridItem = CType(lbl.NamingContainer, DataGridItem) Dim strVals As [String] = Convert.ToString(DataBinder.Eval(CType(container, DataGridItem).DataItem, ‘LastName’)) + ‘, ‘ + Convert.ToString(DataBinder.Eval(CType(container, DataGridItem).DataItem, ‘FirstName’)) lbl.Text = strVals End Sub ’BindLabelColumn End Class ’newLabelColumn ’Fill the Dataset Dim objtc As New TemplateColumn objtc.HeaderText = ‘Full Name’ objtc.ItemTemplate = New newLabelColumn DataGrid1.Columns.Add(objtc) DataGrid1.DataSource = ds DataGrid1.DataBind() C# Create class public class newLabelColumn : ITemplate { public newLabelColumn() { //Add constructor stuff here } public void InstantiateIn(Control container) { Label label1 = new Label(); label1.DataBinding += new EventHandler(this.BindLabelColumn); container.Controls.Add(label1); } public void BindLabelColumn(object sender, EventArgs e) { Label lbl = (Label)sender; DataGridItem container = (DataGridItem)lbl.NamingContainer ; String strVals = Convert.ToString(DataBinder.Eval(((DataGridItem)container).DataItem, ‘LastName’)) + ‘, ‘ + Convert.ToString(DataBinder.Eval(((DataGridItem)container).DataItem, ‘FirstName’)) ; lbl.Text = strVals; } } //Fill the DataSet TemplateColumn objtc = new TemplateColumn(); objtc.HeaderText = ‘Full Name’; objtc.ItemTemplate = new newLabelColumn(); DataGrid1.Columns.Add(objtc); DataGrid1.DataSource =ds; DataGrid1.DataBind();

How to have mutually exclusive radiobutton controls in a datagrid.

<asp:DataGrid id=’DataGrid1′ AutoGenerateColumns=’False’ runat=’server’> <Columns> <asp:TemplateColumn HeaderText=’FirstName’> <ItemTemplate> <input type=’radio’ name=’optSelectedName’ value=<%#DataBinder.Eval(Container.DataItem, ‘FirstName’) %> onclick=’javascript:txtFname.value = this.value;’><%#DataBinder.Eval(Container.DataItem, ‘FirstName’) %> <br> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:DataGrid> <asp:Button id=’Button1′ style=’Z-INDEX: 101; LEFT: 154px; POSITION: absolute; TOP: 103px’ runat=’server’ Text=’Button’></asp:Button> <div style=’VISIBILITY: hidden’> <asp:TextBox ID=’txtFname’ Runat=’server’ /> </div> VB.NET Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ’Bind the Datagrid with dataSet End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Response.Write(‘You have selected :’ & txtFname.Text) End Sub C# private void Page_Load(object sender, System.EventArgs e) { //Bind the datagrid to dataset } private void Button1_Click(object sender, System.EventArgs e) { Response.Write(‘You have selected :’ + txtFname.Text); }

How to do Bidirectional Sorting in the DataGrid

<asp:DataGrid id=’DataGrid1′ OnSortCommand =’SortDataGrid’ runat=’server’ AllowSorting=’True’ EnableViewState=’False’></asp:DataGrid> VB.NET Protected SQLStmt As String = ‘Select * from Products ‘ Dim myconnection As SqlConnection Dim myda As SqlDataAdapter Dim ds As DataSet Dim strConn As String Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ’Put user code to initialize the page here binddata() End Sub Sub binddata() strConn = ‘Server=localhost;uid=sa;password=;database=northwind;’ myconnection = New SqlConnection(strConn) myda = New SqlDataAdapter(SQLStmt, myconnection) ds = New DataSet myda.Fill(ds, ‘AllTables’) DataGrid1.DataSource = ds DataGrid1.DataBind() End Sub Protected Sub SortDataGrid(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) If ViewState(‘SortOrder’) Is Nothing Then ViewState(‘SortOrder’) = ‘ ASC’ ElseIf ViewState(‘SortOrder’) = ‘ ASC’ Then ViewState(‘SortOrder’) = ‘ DESC’ Else ViewState(‘SortOrder’) = ‘ ASC’ End If SQLStmt = SQLStmt & ‘ ORDER BY ‘ & e.SortExpression & ‘ ‘ & ViewState(‘SortOrder’) binddata() End Sub C# SqlConnection myconnection ; SqlDataAdapter myda ; DataSet ds ; String strConn ; string SQLStmt= ‘Select * from Products ‘; private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here binddata(); } void binddata() { strConn = ‘Server=localhost;uid=sa;password=;database=northwind;’; myconnection =new SqlConnection(strConn); myda = new SqlDataAdapter(SQLStmt, myconnection); ds = new DataSet(); myda.Fill(ds, ‘AllTables’); DataGrid1.DataSource = ds; DataGrid1.DataBind(); } protected void SortDataGrid(object source , System.Web.UI.WebControls.DataGridSortCommandEventArgs e ) { if (ViewState[‘SortOrder’] ==null) { ViewState[‘SortOrder’] = ‘ ASC’; } else if (ViewState[‘SortOrder’].ToString () == ‘ ASC’ ) { ViewState[‘SortOrder’] = ‘ DESC’; } else { ViewState[‘SortOrder’] = ‘ ASC’; } SQLStmt = SQLStmt + ‘ ORDER BY ‘ + e.SortExpression.ToString () + ‘ ‘ + ViewState[‘SortOrder’]; binddata(); }

How can I have an onclick event in the DataGrid for any Column

<asp:DataGrid id=’DataGrid1′ OnItemDataBound =’ItemDB’ DataKeyField =’ProductId’ runat=’server’></asp:DataGrid> VB.NET Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ’Put user code to initialize the page here If Not Page.IsPostBack Then ’Bind the dataGrid to DataView End If End Sub Protected Sub ItemDB(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Dim dv As DataView = DataGrid1.DataSource Dim dcCol As DataColumn Dim dc As DataColumnCollection = dv.Table.Columns Dim strID As String For Each dcCol In dv.Table.Columns If e.Item.ItemType = ListItemType.AlternatingItem Or _ e.Item.ItemType = ListItemType.Item Then strID = DataGrid1.DataKeys(e.Item.ItemIndex) e.Item.Cells(dc.IndexOf(dc(dcCol.ColumnName))).Attributes.Add(‘style’, ‘cursor:hand’) e.Item.Cells(dc.IndexOf(dc(dcCol.ColumnName))).Attributes.Add(‘onclick’, _ ‘javascript:window.open(’details.aspx?id=’ & strID & ‘’,’ _ & ‘’MyPage’,’height=300,width=300’)’) End If Next End Sub C# private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here if(!Page.IsPostBack ) { //Bind Datagrid to DataView } } protected void ItemDB(object sender , System.Web.UI.WebControls.DataGridItemEventArgs e ) { DataView dv = (DataView)DataGrid1.DataSource; DataColumnCollection dc = dv.Table.Columns ; string strID; foreach (DataColumn dcCol in dv.Table.Columns) { if ((e.Item.ItemType == ListItemType.AlternatingItem )||(e.Item.ItemType == ListItemType.Item )) { strID = DataGrid1.DataKeys[e.Item.ItemIndex].ToString (); e.Item.Cells[dc.IndexOf(dc[dcCol.ColumnName])].Attributes.Add(‘style’, ‘cursor:hand’); e.Item.Cells[dc.IndexOf(dc[dcCol.ColumnName])].Attributes.Add(‘onclick’, ‘javascript:window.open(’details.aspx?id=’ + strID + ‘’,’ + ‘’MyPage’,’height=300,width=300’)’); } } }