C# 使用相关组合框配置文本字段-ASP.NET
下午好 我在ASP.NET工作 我有一个文本框,需要在其中手动插入一些数据,但这些数据取决于组合框上选择的内容 为了便于理解,我举了一个例子 组合框选定值“A”-->只能在文本框中插入值“1,2,3,4,5” 组合框选择值“B”-->只能在文本框中插入值“6,7,8,9,10” 最好的方法是什么 谢谢。编辑 下面是更新后的ASP代码。需要注意的是:C# 使用相关组合框配置文本字段-ASP.NET,c#,asp.net,combobox,textbox,C#,Asp.net,Combobox,Textbox,下午好 我在ASP.NET工作 我有一个文本框,需要在其中手动插入一些数据,但这些数据取决于组合框上选择的内容 为了便于理解,我举了一个例子 组合框选定值“A”-->只能在文本框中插入值“1,2,3,4,5” 组合框选择值“B”-->只能在文本框中插入值“6,7,8,9,10” 最好的方法是什么 谢谢。编辑 下面是更新后的ASP代码。需要注意的是: 我使用VS Empty模板(因此是contentplaceholder),但前提是相同的 您需要替换响应中控件的名称。请写入s,使其与您的控件匹配
- 我使用VS Empty模板(因此是contentplaceholder),但前提是相同的
- 您需要替换
响应中控件的名称。请写入
s,使其与您的控件匹配
- 如果不需要错误消息,可以在javascript代码中删除
及其所有引用(
…)$(“#text valid”)
- 这将使用计时器来验证信息。我认为这是最好的方法,因为如果您选择选项b并尝试键入“10”,它将在1上失败。我将延迟设置为1秒(1000ms),但您可以在
中更改此设置设置超时(…)
- 为了简单起见,我使用jQuery。这并不是一件坏事,只是让它更易读和跨浏览器兼容
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TestingGround.WebForm1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
// Grab the two fields we want to work with (don't forget to
// change "combo" and "text" below to the name of your controls
var fldCombo = '#<% Response.Write(combo.ClientID); %>';
var fldText = '#<% Response.Write(text.ClientID); %>';
// validation function to see if the current value is one of the values
// specified in the combobox
var isValidValue = function () {
$('#text-valid').text('');
var val = $(fldText).val();
var range = $('option:selected', fldCombo).val().split(',');
for (var r = 0; r < range.length; r++)
if (val == range[r])
return true;
$('#text-valid').text('Invalid, must fall within: ' + range.join(', '));
return false;
};
// does the actual validation of the field, or resets it to empty
// and sets focus back in to it
var validateField = function () {
if (!isValidValue())
$(fldText).val('').focus();
};
// bind to the option (se we can validate the current value that's in
// the textbox between changes
$(fldCombo).change(function () {
validateField();
});
// delay timer for validation on keypress
var delayedValidate = null;
// validate when the field changes (mostly on tabs or lose focus)
$(fldText).change(function () {
clearTimeout(delayedValidate);
validateField();
// also bind on a keypress (as they enter a value)
}).bind('keypress', function () {
delayedValidate = setTimeout(validateField, 1000); // allow you time to type in a valid number
});
});
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>TextBox content based on ComboBox</h2>
<table>
<tr>
<td>Combo Box:</td>
<td><asp:DropDownList runat="server" ID="combo">
<asp:ListItem Text="Option A" Value="1,2,3,4,5" Selected="True" />
<asp:ListItem Text="Option B" Value="6,7,8,9,10" />
</asp:DropDownList></td>
</tr>
<tr>
<td>Text Box Value:</td>
<td><asp:TextBox runat="server" ID="text" /><span id="text-valid" style="text-indent:5px;color:red;"></span>
</td>
</tr>
</table>
</asp:Content>
$(文档).ready(函数(){
//抓住我们想要使用的两个字段(别忘了
//将下面的“组合”和“文本”更改为控件的名称
var fldCombo='#';
变量fldText='#';
//验证函数,以查看当前值是否为其中一个值
//在组合框中指定
var isValidValue=函数(){
$(“#文本有效”)。文本(“”);
var val=$(fldText.val();
var range=$('option:selected',fldCombo.val().split(',');
对于(var r=0;r
更新 这是一个能够指定范围的版本。还添加了一个更健壮的检查(在比较之前先将值解析为整数):
$(文档).ready(函数(){
var fldCombo='#';
变量fldText='#';
var isValidValue=函数(){
var val=parseInt($(fldText.val());
如果(val==null | | isNaN(val))
返回false;
$(“#文本有效”)。文本(“”);
var range=$('option:selected',fldCombo.val().split(',');
对于(var r=0;r 如果(val>=low&&val您可以使用MS AJAX扩展。文本框控件有一个扩展(FilteredTextBox Extender)这只允许输入某些字符。您可以使用它,并且当组合框的选定值更改时,您可以修改代码隐藏扩展允许的数字。我认为OP的意思是,如果选择选项A
,则仅允许值1、2、3、4、5(单独)应被接受为文本框中的输入。感谢您的帮助,但文本框中的值将从该值范围手动插入。是的,过早地点击提交(胖指它)。我仍然会发布一个解决方案,以防其他人需要它,但看起来你已经控制了它。我仍然需要一个解决方案,所以如果你能帮助我,我将不胜感激。@FelipeCosta:New version接受范围(请参阅底部代码块)。;-)
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Page.aspx.cs" Inherits="TestingGround.WebForm1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var fldCombo = '#<% Response.Write(combo.ClientID); %>';
var fldText = '#<% Response.Write(text.ClientID); %>';
var isValidValue = function () {
var val = parseInt($(fldText).val());
if (val == null || isNaN(val))
return false;
$('#text-valid').text('');
var range = $('option:selected', fldCombo).val().split(',');
for (var r = 0; r < range.length; r++) {
// is range[r] a numeric range
if (range[r].indexOf('-') != -1) {
var lowHigh = range[r].split('-');
var low = parseInt(lowHigh[0]), high = parseInt(lowHigh[1]);
if (val >= low && val <= high)
return true;
}
// range[r] is just a straight number
var match = parseInt(range[r]);
if (val == match)
return true;
}
$('#text-valid').text('Invalid, must fall within: ' + range.join(', '));
return false;
};
var validateField = function () {
if (!isValidValue())
$(fldText).val('').focus();
};
$(fldCombo).change(function () {
validateField();
});
var delayedValidate = null;
$(fldText).change(function () {
clearTimeout(delayedValidate);
validateField();
}).bind('keypress', function () {
delayedValidate = setTimeout(validateField, 1000); // allow you time to type in a valid number
});
});
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>TextBox content based on ComboBox</h2>
<table>
<tr>
<td>Combo Box:</td>
<td><asp:DropDownList runat="server" ID="combo">
<asp:ListItem Text="Option A" Value="1-3,4,5,11-99" Selected="True" />
<asp:ListItem Text="Option B" Value="6,7-9,10" />
</asp:DropDownList></td>
</tr>
<tr>
<td>Text Box Value:</td>
<td><asp:TextBox runat="server" ID="text" /><span id="text-valid" style="text-indent:5px;color:red;"></span>
</td>
</tr>
</table>
</asp:Content>