c# - 如何在 asp :DropDownList 中默认选择值

标签 c# .net

有谁知道如何在 asp:DropDownList 中默认选择值?

以下是我的代码:

<tr>
    <th align="right"><strong>Test: </strong></th>
    <td>
      <asp:DropDownList ID="Test" runat="server" DataSourceID="dsTest"
        DataValueField="TestID" DataTextField="TestName" AppendDataBoundItems="true">
        <asp:ListItem></asp:ListItem>
      </asp:DropDownList>  
      <asp:SqlDataSource ID="dsTest" runat="server" ConnectionString="<%$ ConnectionStrings:Test %>"
        SelectCommand="test_select" SelectCommandType="StoredProcedure">
      </asp:SqlDataSource>
   </td>
</tr>

如何选择TestID为1的显示列表?

最佳答案

如果您想默认执行此操作,您有几个选项,第一个选项如下。

<asp:ListItem Selected="True"></asp:ListItem>

或者你可以在代码中做到这一点

Test.selectedIndex = 0;

关于c# - 如何在 asp :DropDownList 中默认选择值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5891023/

相关文章:

C# 杀死一个线程

.net - 正则表达式挑选艺术家姓名和歌曲标题,存在惰性匹配问题

c# - 如何使用与登录用户不同的电子邮件帐户在 C# 中发送电子邮件

c# - 在网格中生成和放置按钮,通过 x,y 坐标引用

c# - 让 WebAPI2 中的 PUT 和 DELETE 动词在 IIS 8.5 上工作

c# - 如何在 Autofac 注册中调用异步方法?

c# - 尝试在我的 WebAPI 上调用特定的 GET 方法,但未找到 HTTP 404

c# - C# 接口(interface)可以要求或规定特定的类吗?

c# - 如何捆绑项目文件夹外部的文件夹

.net - 为什么使用retrieve <entity>后无法访问单个实体的属性?