Sunday, October 26, 2008

Detect DropDownList SelectedIndexChanged on Page Load

In ASP.NET, when you define an asp:DropDownList with AutoPostBack="true", say if you are having two ListItems of the same value, then you select the first one, and now when you select the second one, the SelectedIndexChanged event of the DropDownList will not fire since the
SelectedValue of the DropDownList has not changed. You could use Request.Form("__EVENTTARGET") in the Page_Load to solve this problem.

Here is the example:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="TestDropDown._Default" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" >


<head runat="server">


<title></title>


</head>


<body>


<form id="form1" runat="server">


<div>



<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">


<asp:ListItem Text="---" Value="---"></asp:ListItem>


<asp:ListItem Text="aa" Value="aa"></asp:ListItem>


<asp:ListItem Text="bb" Value="aa"></asp:ListItem>


<asp:ListItem Text="cc" Value="cc"></asp:ListItem>


</asp:DropDownList>


</div>


</form>


</body>


</html>


Partial Public Class _Default


Inherits System.Web.UI.Page


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


If Me.Request.Form("__EVENTTARGET") = DropDownList1.UniqueID Then


Response.Write(DropDownList1.SelectedValue)


End If


End Sub


Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged


Response.Write(DateTime.Now.ToString())


End Sub


End Class

IE8 running in IE7 mode

You could add the following as the first meta tag on the page-level to force the page to render in IE7 mode when you are using IE8:

<meta http-equiv="X-UA-Compatible" content="IE=7" />


Saturday, October 18, 2008

ReadOnly TextBox in ASP.NET

If you set the value of a readonly textbox via javascript, you cannot get the text property of the textbox from code behind. The solution is to use the Request object.

Here is how:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="TestTime._Default" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" >


<head runat="server">


<title>Untitled Page</title>


<script language="javascript">


Function SetValue()


{


document.getElementById("txtTest").value = "Test";


}


</script>


</head>


<body>


<form id="form1" runat="server">


<div>


<asp:TextBox ID="txtTest" runat="server" ReadOnly="true"></asp:TextBox>


<asp:Button ID="Button1" runat="server" Text="PostBack" OnClientClick="SetValue();"/>


<asp:Label ID="lblTest" runat="server"></asp:Label>


</div>


</form>


</body>


</html>


Partial Public Class _Default


Inherits System.Web.UI.Page


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


End Sub


Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click


lblTest.Text = txtTest.Text 'txtTest.Text = ""


lblTest.Text = Request(txtTest.ClientID) 'Request(txtTest.ClientID)= "Test"


End Sub


End Class


Optionally, you can use the following line to set the readonly attribute:

txtMyTextBox.Attributes.Add("readonly", "readonly")

Reference:

ReadOnly Textbox ViewState in .NET 2.0

Friday, October 17, 2008

SQL 2008 Select All Rows

If you want to select all rows from a table using SQL Server 2008 Management Studio, here is how:
1. Go to Tools > Options > SQL Server Object Explorer
2. Change both the 'Value for Edit Top n Rows command' and 'Value for Select Top n Rows command' to zero.

3. You can now view or edit all rows of a table.

Reference:
SQL 2008 - Change ‘Edit Top 200 Rows"