Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在asp:DropDownList中设置默认选定值_C#_.net - Fatal编程技术网

C# 如何在asp:DropDownList中设置默认选定值

C# 如何在asp:DropDownList中设置默认选定值,c#,.net,C#,.net,有人知道如何在asp:DropDownList中默认选择值吗 以下是我的代码: <tr> <th align="right"><strong>Test: </strong></th> <td> <asp:DropDownList ID="Test" runat="server" DataSourceID="dsTest" DataValueField="TestID" Dat

有人知道如何在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?

您可以在从代码后面绑定数据后添加它吗

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

阅读最后一句话后,听起来您可能希望列表项的值为1。请使用以下代码执行此操作:

Test.Items.FindByValue("1").Selected = true;

希望其中一个对您有用。

您能在从代码后面绑定数据后添加它吗

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

阅读最后一句话后,听起来您可能希望列表项的值为1。请使用以下代码执行此操作:

Test.Items.FindByValue("1").Selected = true;

希望其中一个对你有用。

你有两个选择,如果你想在默认情况下这样做,第一个选项在下面

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

您有两个选项,第一个选项在下面,如果您想在默认情况下这样做

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