Why can’t I put <%@ Page Language=’C ‘ %> where at the top of an ASPX file and write my server-side scripts in C ?
The parsers ASP.NET uses to extract code from ASPX files understand C#, Visual Basic.NET, and JScript.NET. You can write server-side scripts in any language supported by a .NET compiler.
How to comment out ASP.NET Tags
<%–<asp:Label id=’Label1′ style=’Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 48px’ runat=’server’>Label</asp:Label>–%>
How to do Paging in DataList
<asp:datalist id=’DataList1′ runat=’server’> <ItemTemplate> <%#DataBinder.Eval(Container.DataItem, ‘ProductID’).ToString()%> <%#DataBinder.Eval(Container.DataItem, ‘ProductName’).ToString()%> </ItemTemplate> </asp:datalist> <table width=’50%’ border=’0′> <tr> <td><asp:LinkButton id=’lnkPrevious’ runat=’server’><<</asp:LinkButton> <td><asp:LinkButton id=’lnkNext’ runat=’server’>>></asp:LinkButton> </tr> </table> VB.NET Dim intStart As Integer Dim intpageSize As Integer Dim cn As SqlConnection Dim da As SqlDataAdapter Dim ds As DataSet 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 ViewState(‘Start’) = 0 bindList() End If End Sub Sub bindList() cn = New SqlConnection(‘server=localhost;uid=sa;pwd=;database=northwind’) da = New SqlDataAdapter(‘Select * from Products ‘, cn) ds = New DataSet intStart = ViewState(‘Start’) ViewState(‘pageSize’) = 14 da.Fill(ds, intStart, ViewState(‘pageSize’), ‘Table’) DataList1.DataSource = ds DataList1.DataBind() End Sub Private Sub lnkPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkPrevious.Click intStart = ViewState(‘Start’)- ViewState(‘pageSize’) ViewState(‘Start’) = intStart If intStart <= 0 Then ViewState(‘Start’) = 0 End If bindList() End Sub Private Sub lnkNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkNext.Click Dim dlistcount As Integer = DataList1.Items.Count intStart = ViewState(‘Start’) + ViewState(‘pageSize’) ViewState(‘Start’) = intStart If dlistcount < ViewState(‘pageSize’) Then ViewState(‘Start’) = ViewState(‘Start’) – ViewState(‘pageSize’) End If bindList() End Sub Private Sub lnkFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) If intStart <= 0 Then ViewState(‘Start’) = 0 End If bindList() End Sub C# int intStart ; SqlConnection cn ; SqlDataAdapter da ; DataSet ds ; private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here if(!Page.IsPostBack ) { ViewState[‘Start’] = 0; bindList(); } } void bindList() { cn = new SqlConnection(‘server=localhost;uid=sa;pwd=;database=northwind’); da = new SqlDataAdapter(‘Select * from Products ‘, cn); ds = new DataSet(); intStart = (int)ViewState[‘Start’]; ViewState[‘pageSize’] = 14; da.Fill(ds, intStart,(int) ViewState[‘pageSize’], ‘Table’); DataList1.DataSource = ds; DataList1.DataBind(); } private void lnkPrevious_Click(object sender, System.EventArgs e) { intStart = (int) ViewState[‘Start’] -(int) ViewState[‘pageSize’]; ViewState[‘Start’] = intStart; if (intStart <= 0 ) { ViewState[‘Start’] = 0; } bindList(); } private void lnkNext_Click(object sender, System.EventArgs e) { int dlistcount = DataList1.Items.Count; intStart = (int) ViewState[‘Start’]+(int) ViewState[‘pageSize’]; ViewState[‘Start’] = intStart; if ( dlistcount < (int)ViewState[‘pageSize’] ) { ViewState[‘Start’] = (int)ViewState[‘Start’] – (int)ViewState[‘pageSize’]; } bindList(); }
How to use a Hyperlink in a DataList
<asp:DataList id=’DataList1′ runat=’server’> <ItemTemplate> <asp:HyperLink Runat =server NavigateUrl =’<%#’webform1.aspx?id=’ + DataBinder.Eval(Container.DataItem, ‘productid’).ToString()%>’ ID=’Hyperlink1′> <%#DataBinder.Eval(Container.DataItem, ‘ProductName’)%> </asp:HyperLink> </ItemTemplate> </asp:DataList>
How to display checkbox in a DataList and highlight a row if the value of checkbox is true
<asp:datalist id=’DataList1′ OnItemDataBound =’ItemDB’ runat=’server’> <ItemTemplate > <asp:CheckBox id=’chkDiscontinued’ Runat =server checked=<%#Convert.ToBoolean(DataBinder.Eval(Container.DataItem , ‘Discontinued’).ToString())%>> </asp:CheckBox> <asp:label ID=’lblName’ runat=server text=<%#DataBinder.Eval(Container.DataItem , ‘ProductName’).ToString() %>></asp:label> </ItemTemplate> </asp:datalist> VB.NET ’Populate the DataList in the Page_Load Event Protected Sub ItemDB(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then If CType(e.Item.FindControl(‘chkDiscontinued’), CheckBox).Checked Then e.Item.BackColor = Color.CadetBlue End If End If End Sub C# //Bind the DataList in the Page_Load Event protected void ItemDB(object sender , System.Web.UI.WebControls.DataListItemEventArgs e ) { if(( e.Item.ItemType == ListItemType.Item)||( e.Item.ItemType == ListItemType.AlternatingItem) ) { if( ((CheckBox)(e.Item.FindControl(‘chkDiscontinued’))).Checked ) { e.Item.BackColor = Color.CadetBlue; } } }