DropDownList数据源asp.net vb

时间:2016-01-06 08:27:43

标签: asp.net vb.net gridview formview

我遇到连接到数据源的DropDownList控件的问题。我希望在编辑完整的formview时显示所选的索引,但每次我尝试编辑时都会得到表的第一个索引...而所选的索引是不同的...我尝试使用selectparameters,但它也行不通! 这是我的代码。

<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="DDListDataSource" DataTextField="GradeGroupName" DataValueField="GradeGroupName" Height="18px" Width="164px" >
</asp:DropDownList>
<asp:SqlDataSource ID="DDListDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:Dev201601ConnectionString %>"
     InsertCommand="INSERT INTO GradeGroups(GradeGroup) VALUES (@GradeGroup)"
     SelectCommand="SELECT GradeGroupName FROM GradeGroups"
     UpdateCommand="UPDATE GradeGroups SET GradeGroupName = @GradeGroup, GradeGroup = @GradeGroup WHERE (GradeGroup = @Original_GradeGroup)">
    <InsertParameters>
         <asp:Parameter Name="GradeGroup"></asp:Parameter>
    </InsertParameters>
    <SelectParameters>
          <asp:ControlParameter ControlID="DropDownList1" Name="GradeGroupName" PropertyName="SelectedValue" Type="String" />
    </SelectParameters>
    <InsertParameters>
        <asp:Parameter Name="GradeGroupName" />
        <asp:Parameter Name="GradeGroup" />
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="GradeGroup" />
        <asp:Parameter Name="Original_GradeGroup" />
    </UpdateParameters>
</asp:SqlDataSource>

1 个答案:

答案 0 :(得分:1)

我喜欢在代码隐藏中生成下拉列表:

设计页面:

<asp:DropDownList ID="ddlItems" runat="server" />

C#中的代码隐藏:

SqlConnection con = new SqlConnection(conStr);
SqlCommand cmd = new SqlCommand("select * from table_name", con);

try
{
    con.Open();
    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    sda.Fill(ds);
    ddlItems.DataSource = ds;
    ddlItems.DataTextField = "Name";
    ddlItems.DataValueField = "ID";
    ddlItems.DataBind();
}
catch (Exception ex)
{
    //...
}
finally
{
    con.Close();
}

VB.NET中的代码隐藏:

Dim con As New SqlClient.SqlConnection(conStr)
Dim cmd As New SqlClient.SqlCommand("SELECT * FROM Table_Name", con)

Try
   con.Open()
   Dim sda As New SqlDataAdapter(cmd)
   Dim ds As New DataSet()
   sda.Fill(ds)
   ddlItems.DataSource = ds
   ddlItems.DataTextField = "Name"
   ddlItems.DataValueField = "ID"
   ddlItems.DataBind()
   con.Close()
Catch ex As Exception
   '....
End Try