[VS2005] Textbox Auto Expand

Here is an example of auto expand a textbox.

1. Control declaration:

<asp:TextBox ID=”txtMsg” runat=”server”  TextMode=”MultiLine” style=”overflow:hidden” onkeyup=”AutoExpand(this, event)” Rows=”2″ />

2. JavaScript function:

function AutoExpand(txtBox, event)
{
if (event.keyCode == “13” || event.keyCode == “8”) {
var therows = 0
var thetext = document.getElementById(txtBox.id).value;
var newtext = thetext.split(“\n”);
therows += newtext.length

document.getElementById(txtBox.id).rows = therows;
return false;
}
}

[JS] Display Textbox When RadioButton Is Clicked

This source code is using the javascript to display the textbox when a radiobutton is clicked. This applies when a user clicks the other specify radio button and the javascript is called to display the textbox. Then, the textbox will be validated if empty.

//Javascript
function InterfaceControl1(id)
{
var radioQ1 = document.getElementsByName(id);
for (var ii = 0; ii < (radioQ1ii++)
{
if (radioQ1[ii].checked){
if (radioQ1[ii].value == “O”){
document.getElementById(“txtQ1”).style.display = ‘inline’; //to show
}
else
{
document.getElementById(“txtQ1”).style.display = ‘none’; //to hide
}
}
}
}

The design code is as below:

<asp:RadioButtonList ID=”rbtnQ1″ runat=”server” RepeatColumns=”2″ RepeatDirection=”Horizontal” CssClass=”chkBoxList” OnClick=”javascript:InterfaceControl1(‘rbtnQ1’);”>
<asp:ListItem Text=”SteelSeries” Value=”SteelSeries”></asp:ListItem>
<asp:ListItem Text=”Razer” Value=”Razer”></asp:ListItem>
<asp:ListItem Text=”Logitech” Value=”Logitech”></asp:ListItem>
<asp:ListItem Text=”Microsoft” Value=”Microsoft”></asp:ListItem>
<asp:ListItem Text=”Others (Please specify)” Value=”O”></asp:ListItem>
</asp:RadioButtonList>

<asp:TextBox ID=”txtQ1″ runat=”server” style=”display:none;” MaxLength=”50″ ValidationGroup=”vdgSubmit”></asp:TextBox>

<asp:RequiredFieldValidator ID=”rfvQ1″ runat=”server” ControlToValidate=”txtQ1″ ErrorMessage=”Please fill in” ForeColor=”Red” ></asp:RequiredFieldValidator>

<br /><br />