如果用户选择“其他”单选按钮并将其保留为空白或在文本框中输入错误数据,则需要显示错误。一旦用户输入正确的数据,错误就消失了。
<asp:RadioButtonList ID="radyears" AutoPostBack="true" runat="server" Height="63px" Width="100px" OnSelectedIndexChanged="radyears_SelectedIndexChanged" Font-Names="Arial" Font-Size="12pt">
<asp:ListItem Selected="True" Value="15">15 Years</asp:ListItem>
<asp:ListItem Value="30">30 Years</asp:ListItem>
<asp:ListItem>Other</asp:ListItem>
</asp:RadioButtonList>
<asp:TextBox ID="txtother" runat="server" AutoPostBack="True" Font-Names="Arial" Font-Size="12pt" Width="150px" MaxLength="10"></asp:TextBox>
<asp:Label ID="lblothererror" runat="server" Font-Names="Arial" Font-Size="11pt" ForeColor="Red"></asp:Label>下面的图片显示无线按钮如何在文本框和文本框旁边显示错误信息标签。这是无线电按钮显示的图像。
这是我的尝试
if (radyears.SelectedValue == "Other")
{
if (String.IsNullOrEmpty(txtother.Text) || (!double.TryParse(txtother.Text, out years)))
{
lblothererror.Text = "Not Valid Input";
return;
}
else
{
lblothererror.Text = "valid number";
return;
}发布于 2016-05-06 04:54:24
尝试以下代码:-
<asp:RadioButtonList ID="radyears" AutoPostBack="true" runat="server" Height="63px" Width="100px" Font-Names="Arial" Font-Size="12pt" OnSelectedIndexChanged="radyears_SelectedIndexChanged">
<asp:ListItem Selected="True" Value="15">15 Years</asp:ListItem>
<asp:ListItem Value="30">30 Years</asp:ListItem>
<asp:ListItem>Other</asp:ListItem>
</asp:RadioButtonList>
<asp:TextBox ID="txtother" runat="server" AutoPostBack="True" Font-Names="Arial" Font-Size="12pt" Width="150px" MaxLength="10" OnTextChanged="txtother_TextChanged"></asp:TextBox>
<asp:Label ID="lblothererror" runat="server" Font-Names="Arial" Font-Size="11pt" ForeColor="Red"></asp:Label>在aspx.cs页面中
protected void radyears_SelectedIndexChanged(object sender, EventArgs e)
{
if (radyears.SelectedValue == "Other")
{
lblothererror.Text = "Enter age in Textbox";
}
else
{
lblothererror.Text = "";
}
}
protected void txtother_TextChanged(object sender, EventArgs e)
{
if (radyears.SelectedValue == "Other")
{
double years = 100;
if (String.IsNullOrEmpty(txtother.Text) || (Convert.ToInt32(txtother.Text) > years))
{
lblothererror.Text = "Invalid";
}
else
{
lblothererror.Text = "valid";
}
}
else
{
lblothererror.Text = "";
}
}发布于 2016-05-06 04:26:10
试试这里是c#代码
protected void radyears_SelectedIndexChanged(object sender, EventArgs e)
{
if (radyears.SelectedValue == "Other")
{
lblothererror.Text = "Enter value in Textbox please";
}
}
protected void txtother_TextChanged(object sender, EventArgs e)
{
if (radyears.SelectedValue == "Other")
{
double years = 123;//Any Value
if (String.IsNullOrEmpty(txtother.Text) || (!double.TryParse(txtother.Text, out years)))
{
lblothererror.Text = "Not Valid Input";
return;
}
else
{
lblothererror.Text = "valid number";
return;
}
}
else
{
lblothererror.Text = "";
return;
}
}它就在这里
<asp:RadioButtonList ID="radyears" AutoPostBack="true" runat="server" Height="63px" Width="100px" OnSelectedIndexChanged="radyears_SelectedIndexChanged" Font-Names="Arial" Font-Size="12pt">
<asp:ListItem Selected="True" Value="15">15 Years</asp:ListItem>
<asp:ListItem Value="30">30 Years</asp:ListItem>
<asp:ListItem>Other</asp:ListItem>
</asp:RadioButtonList>
<asp:TextBox ID="txtother" runat="server" OnTextChanged="txtother_TextChanged" Font-Names="Arial" Font-Size="12pt" Width="150px" MaxLength="10"></asp:TextBox>
<asp:Label ID="lblothererror" runat="server" Font-Names="Arial" Font-Size="11pt" ForeColor="Red"></asp:Label>https://stackoverflow.com/questions/37064011
复制相似问题