C# 如何在asp:DropDownList中设置默认选定值
有人知道如何在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
<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>