简体   繁体   English

如何动态填充使用数据表嵌套在表单视图中的下拉列表?

[英]How do I populate a droplist dynamically that is nested in a Form View using a datatable?

I am trying to fill a asp:DropDownList with data from SQL and with the use of a datatable. 我试图用来自SQL数据和使用数据表填充asp:DropDownList However I get the error 'CreatedByDropList' has a SelectedValue which is invalid because it does not exist in the list of items. 但是我收到错误消息“ CreatedByDropList”,它具有一个SelectedValue,这是无效的,因为它不存在于项目列表中。 Parameter name: value 参数名称:值

The asp:DropDownList is inside of a formview whos select is populated by a gridview. asp:DropDownList位于formview内部,其选择由gridview填充。

here is the HTML for the DropList control in Edit and Insert template of the Formview: 这是Formview的Edit and Insert模板中DropList控件的HTML:

<asp:DropDownList ID="CreatedByDropList" AppendDataBoundItems="true" SelectedValue='<%# Bind("CreatedBy") %>' runat="server" />

Here is the Code Behind: 下面是代码:

protected void FormView1_DataBound(object sender, EventArgs e)
{
    if (FormView1.CurrentMode == FormViewMode.Insert)
    {
        DropDownList ddlCreatedBy = FormView1.FindControl("CreatedByDropList") as DropDownList; //Placed BreakPoint Here

        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MySqlServer"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand(@"SELECT RLS.RoleName [RoleName], URS.UserID [UserID], USRS.UserName[UserName], USRS.FirstName[FirstName], USRS.LastName[LastName]
                                                FROM [Roles] RLS Inner JOIN [Users] USRS LEFT JOIN [UserRoles] URS 
                                                ON USRS.[UserID] = URS.[UserID] ON RLS.[RoleID] = URS.[RoleID] 
                                                WHERE RLS.[RoleName] = 'Blog Editors'",conn);
            conn.Open();
            using (SqlDataReader reader1 = cmd.ExecuteReader())
            {
                while (reader1.Read())
                {
                    int numUserID = reader1.GetInt32(1);
                    string strFirstName = reader1.GetString(3);
                    string strLastName = reader1.GetString(4);
                    string newUserName = strFirstName + " " + strLastName;
                }

                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    ddlCreatedBy.DataSource = dt;
                    ddlCreatedBy.DataTextField = "newUserName";//Placed BreakPoint Here
                    ddlCreatedBy.DataValueField = "numUserID";//Placed BreakPoint Here
                    ddlCreatedBy.DataBind();
            }



        }

}

I placed Breakpoints throughout for some troubleshooting but the debug window stays blank(meaning all debug windows are blank and useless) the entire time. 我在整个断点处进行了一些故障排除,但是调试窗口始终保持空白(这意味着所有调试窗口均是空白且无用)。

reader 1 isn't doing anything. 读者1没做任何事情。 It's just iterating over all of the rows returned from the command, assigning them to variables, then not doing anything with them, as far as this snippet shows. 只是迭代从命令返回的所有行,将它们分配给变量,然后对它们进行任何处理,就如该片段所示。

Also, you don't have any columns in your DataTable named "newUserName" or "numUserId". 另外,您的数据表中没有名为“ newUserName”或“ numUserId”的任何列。 Change the last 4 lines to this: 将最后4行更改为:

foreach(DataRow row in dt.Rows)
{
    ddlCreatedBy.Items.Add(new ListItem(row["FirstName"] + " " + row["LastName"], row["UserId"]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM