Which control should I use when binding a multidimensional array
Use a DataList as follows. .aspx <asp:datalist id=’DataList1′ runat=’server’> <ItemTemplate> <asp:repeater datasource='<%#Container.DataItem%>’ runat=server ID=’Repeater1′ > <ItemTemplate> <%# Container.DataItem %> </ItemTemplate> </asp:repeater> </ItemTemplate> </asp:datalist> VB.NET Private Sub Page_Load(sender As Object, e As System.EventArgs) ’ Put user code to initialize the page here ’ Create and populate a multi-dimensional array Dim MyArray(4) As Integer() Dim i As Integer For i = 0 To 3 MyArray(i) = New Integer(5) {} Dim x As Integer For x = 0 To 4 MyArray(i)(x) = x + 5 * i Next Next DataList1.DataSource = MyArray DataList1.DataBind() End Sub ’Page_Load C# private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here // Create and populate a multi-dimensional array int [][] MyArray = new int[4][]; for (int i=0; i<4; i++) { MyArray[i] = new int[5]; for (int x=0; x<5; x++) { MyArray[i][x] = x+(5*i); } } DataList1.DataSource = MyArray; DataList1.DataBind(); }
How to create a File Picker in ASP.NET
Use namespace System.IO <asp:label id=’lblHeader’ runat=’server’></asp:label><br> <b>Get Information on Directory:</b><br> <p><asp:textbox id=’txtPath’ runat=’server’></asp:textbox> <p></p> <asp:button id=’btnSubmit’ runat=’server’ text=’Go!’ type=’Submit’></asp:button> <p></p> <div style=’width:100%; height:200; overflow:auto;’> <asp:datalist id=’DataList1′ runat=’server’ DataKeyField=’FullName’ OnSelectedIndexChanged=’SelectedIndexChanged’> <ItemTemplate> <li> <%# DataBinder.Eval(Container.DataItem, ‘Name’) %> <br> <font size=’-1′>[ <asp:linkbutton Text=’View Contents’ CommandName=’Select’ runat=’server’ ID=’Linkbutton1′ />] | [ <%# DataBinder.Eval(Container.DataItem, ‘Length’) %> bytes] </font> <p> </ItemTemplate> </asp:datalist> </div> <p> <hr> </p><asp:label id=’lblFileContents’ runat=’server’></asp:label> 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 Request(‘txtPath’) <> ” Then Dim strDir As String = Request(‘txtPath’) lblHeader.Text = ‘File Listing for ‘ & strDir & ” Dim dirInfo As New DirectoryInfo(strDir) ’ Get the files for the directory strDir Dim fInfos As FileInfo() = dirInfo.GetFiles(‘*.*’) DataList1.DataSource = fInfos DataList1.DataBind() End If End Sub Protected Sub SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataList1.SelectedIndexChanged Dim strFilePath As String = DataList1.DataKeys(DataList1.SelectedItem.ItemIndex).ToString() Dim fInfo As FileInfo = New FileInfo(strFilePath) Dim objStream As StreamReader = fInfo.OpenText() Dim strContents As String = objStream.ReadToEnd() objStream.Close() lblFileContents.Text = ‘Contents of ‘ & fInfo.Name & ‘:’ & _ ” & vbCrLf & strContents & vbCrLf & ” End Sub C# private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here if (Request[‘txtPath’]!= null) { string strDir = Request[‘txtPath’]; lblHeader.Text = ‘File Listing for ‘ + strDir + ”; DirectoryInfo dirInfo =new DirectoryInfo(strDir); // Get the files for the directory strDir FileInfo[] fInfos = dirInfo.GetFiles(‘*.*’); DataList1.DataSource = fInfos; DataList1.DataBind(); } } protected void SelectedIndexChanged(Object sender , System.EventArgs e ) { string strFilePath = DataList1.DataKeys[(int)DataList1.SelectedItem.ItemIndex].ToString(); FileInfo fInfo = new FileInfo(strFilePath); StreamReader objStream = fInfo.OpenText(); string strContents = objStream.ReadToEnd(); objStream.Close(); lblFileContents.Text = ‘Contents of ‘ + fInfo.Name + ‘:’ + ” + ‘ ‘ + strContents + ‘ ‘ + ”; }
How can I get a list of all the environment variables
VB.NET <asp:DataList id=’DataList1′ runat=’server’> <HeaderTemplate> <table> </HeaderTemplate> <ItemTemplate> <tr> <td> <%# CType(Container.DataItem, DictionaryEntry).Key %> </td> <td> <%# CType(Container.DataItem, DictionaryEntry).Value%> </td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:DataList> 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 DataList1.DataSource = System.Environment.GetEnvironmentVariables() DataList1.DataBind() End Sub C# <asp:DataList id=’DataList1′ runat=’server’> <HeaderTemplate> <table> </HeaderTemplate> <ItemTemplate> <tr> <td> <%# ((DictionaryEntry)Container.DataItem).Key %> </td> <td> <%# ((DictionaryEntry)Container.DataItem).Value %> </td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:DataList> private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here DataList1.DataSource = System.Environment.GetEnvironmentVariables(); DataList1.DataBind(); }
How to hide and show data in the DataList
<asp:Datalist runat=’server’ OnItemCommand =’ItemCmd’ id=’Datalist1′ Font-Size=’10pt’ Font-Name=’Verdana’> <ItemTemplate> <asp:LinkButton style=’text-decoration:none’ runat=’server’ id=’btnDetails’ Text=’+’ CommandName=’Show’ Font-Name=’Verdana’ /> <b> <%# DataBinder.Eval(Container.DataItem, ’employeeid’) %> </b> <br /> <asp:label id=’lblEID’ Visible=’False’ runat=’Server’ Text=’<%# DataBinder.Eval(Container.DataItem, ‘Employeeid’) %>’ /> <asp:DataGrid runat=’server’ id=’Datagrid1′ Font-Name=’Verdana’ Font-Size=’10pt’ HorizontalAlign=’Center’ Visible=’False’ Width=’85%’> </ItemTemplate> </asp:Datalist> VB.NET Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then ’Bind data to DataList End If End Sub Private Sub Datalist1_ItemCommand(ByVal source As Object, _ ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) If e.CommandName = ‘Show’ Then Dim EmpIDlabel As Label = e.Item.FindControl(‘lblEID’) Dim strEmpID As String = EmpIDlabel.Text CType(e.Item.FindControl(‘Datagrid1’), DataGrid).DataSource = GetEmpDetails(strEmpID) CType(e.Item.FindControl(‘Datagrid1’), DataGrid).DataBind() CType(e.Item.FindControl(‘Datagrid1’), DataGrid).Visible = True CType(e.Item.FindControl(‘btnDetails’), LinkButton).Text = ‘-‘ CType(e.Item.FindControl(‘btnDetails’), LinkButton).CommandName = ‘Hide’ End If If e.CommandName = ‘Hide’ Then CType(e.Item.FindControl(‘Datagrid1’), DataGrid).Visible = False CType(e.Item.FindControl(‘btnDetails’), LinkButton).Text = ‘+’ CType(e.Item.FindControl(‘btnDetails’), LinkButton).CommandName = ‘Show’ End If End Sub Function GetEmpDetails(ByVal Employeeid As String) As SqlDataReader Const strConnString As String = ‘server=localhost;uid=sa;pwd=;database=northwind’ Dim objConn As New SqlConnection(strConnString) Dim strSQL As String strSQL = ‘SELECT FirstName , LastName ,Title, Address FROM Employees ‘ & _ ‘WHERE Employeeid = @Employeeid’ Dim objCmd As New SqlCommand(strSQL, objConn) Dim paramEmployeeid As SqlParameter paramEmployeeid = New SqlParameter(‘@Employeeid’, SqlDbType.VarChar, 10) paramEmployeeid.Value = Employeeid objCmd.Parameters.Add(paramEmployeeid) objConn.Open() ’Open the connection Return objCmd.ExecuteReader(CommandBehavior.CloseConnection) End Function C# private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here if(!Page.IsPostBack ) { //Bind data to Datalist1 } } protected void ItemCmd(object source , System.Web.UI.WebControls.DataListCommandEventArgs e ) { if( e.CommandName == ‘Show’ ) { Label EmpIDlabel =(Label) e.Item.FindControl(‘lblEID’); string strEmpID = EmpIDlabel.Text; ((DataGrid)e.Item.FindControl(‘Datagrid1’)).DataSource = GetEmpDetails(strEmpID); ((DataGrid)e.Item.FindControl(‘Datagrid1’)).DataBind(); ((DataGrid)e.Item.FindControl(‘Datagrid1’)).Visible = true; ((LinkButton)e.Item.FindControl(‘btnDetails’)).Text = ‘-‘; ((LinkButton)e.Item.FindControl(‘btnDetails’)).CommandName = ‘Hide’; } if( e.CommandName == ‘Hide’ ) { ((DataGrid)e.Item.FindControl(‘Datagrid1’)).Visible = false; ((LinkButton )e.Item.FindControl(‘btnDetails’)).Text = ‘+’; ((LinkButton)e.Item.FindControl(‘btnDetails’)).CommandName = ‘Show’; } } protected SqlDataReader GetEmpDetails(string Employeeid ) { string strConnString = ‘server=localhost;uid=sa;pwd=;database=northwind’; SqlConnection objConn = new SqlConnection(strConnString); string strSQL ; strSQL = ‘SELECT FirstName , LastName ,Title, Address FROM Employees WhERE Employeeid = @Employeeid’; SqlCommand objCmd = new SqlCommand(strSQL, objConn); SqlParameter paramEmployeeid ; paramEmployeeid = new SqlParameter(‘@Employeeid’, SqlDbType.VarChar, 10); paramEmployeeid.Value = Employeeid; objCmd.Parameters.Add(paramEmployeeid); objConn.Open() ;// Open the connection; return objCmd.ExecuteReader(CommandBehavior.CloseConnection); }
How to add a Templatecolumn dynamically to a DataList
<asp:DataList id=’DataList1′ runat=’server’></asp:DataList> VB.NET In class Public Class DatalistLabelColumn Implements ITemplate Public Sub New() End Sub ’New 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 DataListItem = CType(lbl.NamingContainer, DataListItem) Dim strVals As [String] = Convert.ToString(DataBinder.Eval(CType(container, DataListItem).DataItem, ‘LastName’)) + ‘, ‘ + Convert.ToString(DataBinder.Eval(CType(container, DataListItem).DataItem, ‘FirstName’)) lbl.Text = strVals End Sub ’BindLabelColumn End Class ’DatalistLabelColumn Dim ds As DataSet = ’Fill the dataset DataList1.ItemTemplate = New DatalistLabelColumn DataList1.DataSource = ds DataList1.DataBind() C# In class public class DatalistLabelColumn : ITemplate { public DatalistLabelColumn() { //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; DataListItem container = (DataListItem)lbl.NamingContainer ; String strVals =Convert.ToString(DataBinder.Eval(((DataListItem)container).DataItem, ‘LastName’)) + ‘, ‘ + Convert.ToString(DataBinder.Eval(((DataListItem)container).DataItem, ‘FirstName’)) ; lbl.Text = strVals; } } DataSet ds = //Assign appropriate value; DataList1.ItemTemplate = new DatalistLabelColumn(); DataList1.DataSource =ds; DataList1.DataBind();