How to change server control backcolor from a variable

<asp:TextBox id=’TextBox1′ BackColor =<%#colCon.ConvertFromString(bgcolor)%> runat=’server’> </asp:TextBox> VB.NET Protected colCon As New System.Drawing.ColorConverter Protected bgcolor As String = ‘#556600’ ’In Page_Load Page.DataBind() C# protected System.Drawing.ColorConverter colCon =new System.Drawing.ColorConverter(); protected string bgcolor = ‘#556600’; //In Page_Load Page.Databind();

How to display data in Textboxes using DataSet

Product ID : <asp:TextBox id=’TextBox1′ style=’Z-INDEX: 101; LEFT: 80px; POSITION: absolute; TOP: 64px’ runat=’server’></asp:TextBox> Product Name:<asp:TextBox id=’TextBox2′ style=’Z-INDEX: 102; LEFT: 80px; POSITION: absolute; TOP: 112px’ runat=’server’></asp:TextBox> VB.NET ’Populate the DataSet ’… ’Display in TextBoxes using Column Name TextBox1.Text = ds.Tables (0).Rows(0)(‘ProductId’).ToString (); TextBox2.Text =ds.Tables (0).Rows(0)(‘ProductName’).ToString (); ’Display in TextBoxes using Column Index TextBox1.Text = ds.Tables (0).Rows(0)(0).ToString (); TextBox2.Text =ds.Tables (0).Rows(0)(1).ToString (); C# //Populate the DataSet //… //Display in TextBoxes using Column Name TextBox1.Text = ds.Tables [0].Rows[0][‘ProductId’].ToString (); TextBox2.Text =ds.Tables [0].Rows[0][‘ProductName’].ToString (); //Display in TextBoxes using Column Index TextBox1.Text = ds.Tables [0].Rows[0][0].ToString (); TextBox2.Text =ds.Tables [0].Rows[0][1].ToString ();

How to display data in Textboxes using DataReader

Id : <asp:TextBox id=’TextBox1′ style=’Z-INDEX: 102; LEFT: 128px; POSITION: absolute; TOP: 32px’ runat=’server’></asp:TextBox> Name : <asp:TextBox id=’TextBox2′ style=’Z-INDEX: 103; LEFT: 128px; POSITION: absolute; TOP: 72px’ runat=’server’></asp:TextBox> VB.NET Dim cn As SqlConnection Dim cmd As SqlCommand Dim rdr As SqlDataReader 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 Try cn = New SqlConnection(‘server=localhost;uid=sa;pwd=;database=northwind’) cmd = New SqlCommand(‘select * from Products where productid =1’, cn) cn.Open() rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection) rdr.Read() TextBox1.Text = rdr(‘ProductId’).ToString() TextBox2.Text = rdr(‘ProductName’).ToString() Catch ex As Exception Response.Write(ex.Message.ToString()) Finally rdr.Close() cn.Close() End Try End Sub C# SqlConnection cn ; SqlCommand cmd ; SqlDataReader rdr ; private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here try { cn = new SqlConnection(‘server=localhost;uid=sa;pwd=;database=northwind’); cmd = new SqlCommand( ‘select * from Products where productid=1 ‘, cn); cn.Open(); rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection ); rdr.Read (); TextBox1.Text =rdr[‘ProductId’].ToString (); TextBox2.Text =rdr[‘ProductName’].ToString (); } catch (Exception ex) { Response.Write (ex.Message.ToString ()); } finally { rdr.Close(); cn.Close(); } }

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();