How to add an extra item to the DropDownList filled with data from a database?
VB.NET ’DropDownList1.Items.Insert(0, ‘Please Select’) DropDownList1.Items.Insert(0, new ListItem(‘<text>’, ‘<value>’)) C# //DropDownList1.Items.Insert(0, ‘Please Select’); DropDownList1.Items.Insert(0, new ListItem(‘<text>’, ‘<value>’)); This should be done after .DataBind of dropdownlist
How to get the textbox value at the client side ?
<script lang=’javascript’> function CheckFunction() { if (document.getElementById(’<%=textbox2.ClientID%>’).value == ”) { alert(‘Please enter a value’); return; } } </script> <asp:textbox id=’textbox2′ runat=’Server’></asp:textbox> <input type=button id=’btn1′ onclick=’javascript:CheckFunction();’ value=’Click’>
Does session state have a locking mechanism that serialize the access to state?
Session state implements a reader/writer locking mechanism: A page (or frame) that has session state write access (e.g. <%@ Page EnableSessionState=’True’ %>) will hold a writer lock on the session until the request finishes. A page (or frame) that has session state read access (e.g. <%@ Page EnableSessionState=’ReadOnly’ %>) will hold a reader lock on the session until the request finishes. Reader lock will block a writer lock; Reader lock will NOT block reader lock; Writer lock will block all reader and writer lock. That’s why if two frames both have session state write access, one frame has to wait for the other to finish first
How to filter xml data and display data in the DataGrid
<?xml version=’1.0′ encoding=’utf-8′ ?> <products> <product prodId=’product1-00′ param1=’11’ /> <product prodId=’product1-00′ param1=’12’ /> <product prodId=’product1-01′ param1=’13’ /> <product prodId=’product1-02′ param1=’14’ /> <product prodId=’product2-00′ param1=’21’ param2=’22’ /> <product prodId=’product2-00′ param1=’31’ param2=’32’ /> <product prodId=’product2-01′ param1=’41’ param2=’42’ /> </products> VB.NET Dim ds As New DataSet ds.ReadXml(Server.MapPath(‘data1.xml’)) Dim dv As New DataView dv = ds.Tables(0).DefaultView dv.RowFilter = ‘prodId=’product2-00’’ Me.DataGrid1.DataSource = dv Me.DataBind() C# DataSet ds = new DataSet(); ds.ReadXml(Server.MapPath(‘data1.xml’)); DataView dv = new DataView(); dv = ds.Tables[0].DefaultView; dv.RowFilter = ‘prodId=’product2-00’’; this.DataGrid1.DataSource = dv; this.DataBind();
How to View one record per page in ASP.NET
<asp:label id=’Label2′ style=’Z-INDEX: 106; LEFT: 111px; POSITION: absolute; TOP: 83px’ runat=’server’>Product ID</asp:label> <asp:label id=’Label1′ style=’Z-INDEX: 105; LEFT: 110px; POSITION: absolute; TOP: 43px’ runat=’server’>Product Name</asp:label> <asp:textbox id=’txtProductName’ style=’Z-INDEX: 104; LEFT: 206px; POSITION: absolute; TOP: 83px’ runat=’server’ OnDataBinding=’txtDataBind’></asp:textbox> <asp:textbox id=’txtProductid’ style=’Z-INDEX: 103; LEFT: 204px; POSITION: absolute; TOP: 43px’ runat=’server’> <asp:button id=’btnPrevious’ style=’Z-INDEX: 102; LEFT: 137px; POSITION: absolute; TOP: 126px’ runat=’server’ Text=’Previous’ OnClick =’PrevBtn’></asp:button> <asp:button id=’btnNext’ style=’Z-INDEX: 101; LEFT: 243px; POSITION: absolute; TOP: 126px’ runat=’server’ Text=’Next’ OnClick =’NextBtn’></asp:button> 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 Try ’Fill the DataSet If Not Page.IsPostBack Then ViewState(‘CurrentPos’) = 0 Me.DataBind() End If Catch ex As Exception Response.Write(ex.Message & ex.StackTrace) End Try End Sub protected Sub NextBtn(ByVal sender As System.Object, ByVal e As System.EventArgs) Try Dim CurrentPos As Integer = CType(ViewState(‘CurrentPos’), Integer) CurrentPos += 1 If CurrentPos > ds.Tables(0).Rows.Count Then CurrentPos -= 1 End If ViewState(‘CurrentPos’) = CurrentPos Me.DataBind() Catch ex As Exception Response.Write(ex.Message) End Try End Sub protected Sub PrevBtn(ByVal sender As System.Object, ByVal e As System.EventArgs) Try Dim CurrentPos As Integer = CType(ViewState(‘CurrentPos’), Integer) If CurrentPos > 0 Then CurrentPos -= 1 End If ViewState(‘CurrentPos’) = CurrentPos Me.DataBind() Catch ex As Exception Response.Write(ex.Message) End Try End Sub protected Sub txtDataBind(ByVal sender As Object, ByVal e As System.EventArgs) Try Dim CurrentPos As Integer = CType(ViewState(‘CurrentPos’), Integer) ViewState(‘CurrentPos’) = (CurrentPos) txtProductid.Text = ds.Tables(0).Rows(CurrentPos).Item(‘productid’) txtProductName.Text = ds.Tables(0).Rows(CurrentPos).Item(‘productname’) Catch ex As Exception Response.Write(ex.Message) End Try End Sub C# DataSet ds; private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here //Fill the DataSet if (!Page.IsPostBack ) { ViewState[‘CurrentPos’] = 0 ; this.DataBind() ; } } protected void PrevBtn(object sender, System.EventArgs e) { try { int CurrentPos = (int)ViewState[‘CurrentPos’] ; if (CurrentPos > 0 ) { CurrentPos -= 1 ; } ViewState[‘CurrentPos’] = CurrentPos ; this.DataBind() ; } catch (Exception ex) { Response.Write(ex.Message) ; } } protected void NextBtn(object sender, System.EventArgs e) { try { int CurrentPos = (int)ViewState[‘CurrentPos’] ; CurrentPos += 1 ; if( CurrentPos > ds.Tables[0].Rows.Count) { CurrentPos -= 1 ; } ViewState[‘CurrentPos’] = CurrentPos ; this.DataBind() ; } catch (Exception ex) { Response.Write(ex.Message) ; } } protected void txtDataBind(Object sender , System.EventArgs e ) { try { int CurrentPos = (int) ViewState[‘CurrentPos’]; ViewState[‘CurrentPos’] = CurrentPos ; txtProductid.Text = ds.Tables[0].Rows[CurrentPos][‘productid’].ToString(); txtProductName.Text = ds.Tables[0].Rows[CurrentPos][‘productname’].ToString (); } catch (Exception ex) { Response.Write(ex.Message) ; } }