【问题标题】:Text box display based on drop down list基于下拉列表的文本框显示
【发布时间】:2013-02-25 15:44:14
【问题描述】:

我有一个下拉列表,其中显示了所有当前的工作编号。我需要一个文本框来显示这个数字的“名称”等价物。下拉列表基于对同一数据库中单独表的查询。下拉列表是:

<asp:DropDownList ID="dlRef" runat="server" DataSourceID="DataRef" DataTextField="Ref" 
     DataValueField="Ref" style="z-index: 1; left: 155px; top: 68px; position: absolute; 
     width: 115px; height: 29px; bottom: 254px;" AutoPostBack="True">
</asp:DropDownList>

我需要做的是让文本框 tbADName 与使用查询的数据源相关联,例如 select Name from ActiveJobs where Ref = dlRef.DataValueField。

这可能吗,还是我需要使用其他构造来显示此信息?

【问题讨论】:

标签: asp.net


【解决方案1】:

您可以使用以下功能

onchange="showText(this.options[this.selectedIndex].text);"

添加到您的下拉列表中

<asp:DropDownList ID="dlRef" runat="server" DataSourceID="DataRef" DataTextField="Ref" 
     DataValueField="Ref" style="z-index: 1; left: 155px; top: 68px; position: absolute; 
     width: 115px; height: 29px; bottom: 254px;" AutoPostBack="True"
      onchange="showText(this.options[this.selectedIndex].text);">
</asp:DropDownList>

在 java-script 中创建一个函数

function showText(value)
{
   document.getElementById("textboxid").value=value
}

编辑 1

<head>
    <title>DropDown</title>
    <script type="text/javascript">
    function chkind(){
    var dropdown1 = document.getElementById('dlRef');
    var textbox = document.getElementById('textbox');
    var a = dropdown1.options[dropdown1.selectedIndex].text;
    textbox.value = a;

    }
    }
    </script>
</head>

<body>
<asp:DropDownList ID="dlRef" runat="server" DataSourceID="DataRef" DataTextField="Ref" 
 DataValueField="Ref" style="z-index: 1; left: 155px; top: 68px; position: absolute; 
 width: 115px; height: 29px; bottom: 254px;" AutoPostBack="True"
 onchange="chkind()">
    <option>Hi</option>
    <option>Bye</option>
</select><br />
<asp:textbox id="textbox" type="text" runat="server" />
</body>

【讨论】:

  • 两件事。我知道将 onchange=functionName() 放在哪里。我不明白将实际功能放在哪里。 Shekhar 的代码对我来说没有意义,因为它没有显示如何将数字和名称之间的关系放入文本框中。
  • Shekhar,如果我理解这会将 HI 或 BYE 放入文本框中。我需要它从使用 dlRef 下拉列表中的“Ref”编号的数据源中获取名称。
  • 也许这会更容易。我有一个数据源,它根据 dlRef 选择从表中选择“名称”。我现在的问题是将此结果附加到文本框或标签或任何最简单的地方。
  • 您想在下拉菜单中的文本框或标签中添加内容吗?
【解决方案2】:

我会使用 javascript 来执行此操作。在您的asp:DropDownList 中添加onchange=functionName() 并处理javascript 中的onchange 以将文本框文本更改为下拉列表中的值。

编辑:

你可以这样调用 onchange 事件:

<asp:DropDownList ID="dlRef" runat="server" DataSourceID="DataRef" DataTextField="Ref" 
     DataValueField="Ref" style="z-index: 1; left: 155px; top: 68px; position: absolute; 
     width: 115px; height: 29px; bottom: 254px;" AutoPostBack="True" onchange="changeTextBox(this)">
</asp:DropDownList>

你的脚本是这样的:

<script>
    function changeTextBox(data) {
        document.getElementById("yourTextBoxId").value = data.value;
    }
</script>

如果你想显示文本,我猜你应该使用 text 属性。

注意:从 html5 开始,脚本标签可以在没有 type 属性的情况下使用。

【讨论】:

  • 我理解这个概念。我很想知道是否有一些示例代码可以让我朝着正确的方向开始?
  • 在我的回答中添加了更多信息
  • 抱歉,回来晚了。我发现我可以将formview控件绑定到下拉列表。选中下拉列表上的回发复选框非常重要,否则您不会刷新表单视图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-21
相关资源
最近更新 更多