自动将第一个字符用作大写
问题描述:
我想在文本框中自动将第一个字符作为大写字母
i want to take the first character as capital automatically in textbox
答
如果您希望通过javascript使用它,请尝试使用此方法
If you wanted it through javascript, then try this
<script type="text/javascript" language="javascript">
function capFirst(oTextBox) {
oTextBox.value = oTextBox.value[0].toUpperCase() + oTextBox.value.substring(1);
}
</script>
<asp:TextBox runat="server" onChange="javascript:capFirst(this);" ID="Tb1" AutoPostBack="False" />
private void tb1_TextChanged(object sender, EventArgs e)
{
tb1.Text = tb1.Text.Substring(0, 1).ToUpper() + tb1.Text.Substring(1);
}
如果使用winforms
In case if use winforms
private void tb1_Leave(object sender, EventArgs e)
{
tb1.Text = tb1.Text.Substring(0, 1).ToUpper() + tb1.Text.Substring(1);
}
如果是aspnet
Incase of aspnet
private void tb1_TextChanged(object sender, EventArgs e)
{
tb1.Text = tb1.Text.Substring(0, 1).ToUpper() + tb1.Text.Substring(1);
}
不要忘记为aspx中的文本框使用Autopostback = true
Dont forget to use Autopostback=true for the textbox in aspx