从几个下拉列表中获取值

时间:2011-10-05 13:51:41

标签: asp.net c#-4.0 drop-down-menu checkboxlist

在我的Page_Init方法中,我创建了1个复选框列表(填充了项目)和几个下拉列表等于checkboxlist中的项目数量(6checkboxlistitems - > 6下拉列表)。

两个项目下拉列表都与数据有关(项目代表一周中的每一天,下拉列表中包含一天的4个时间帧)。我为每个项目创建一个下拉列表,并给它id = item.text。

Page_Init方法的代码:

        foreach (ListItem item in chkListContact.Items)
        {
            ddlTimePeriod = new DropDownList();
            ddlTimePeriod.ID = item.Text;
            ddlTimePeriod.CssClass = "dropdownlistContact";
            ddlTimePeriod.DataSource = datasrcTimePeriod;
            ddlTimePeriod.DataTextField = "TimePeriodName";
            ddlTimePeriod.DataValueField = "TimeToCallID";
            ddlTimePeriod.DataBind();
            dllPanel.Controls.Add(ddlTimePeriod);
            dllPanel.ID = "dllPanel";
        }

来自BUTTON_CLICK的代码:

        foreach (ListItem item in chkListContact.Items)
        {
            //
            if (item.Selected)
            {
                //Here I want to get the values from the selected items and corresponding dropdownlist value and send it to database
                cmdTimeToCall.Parameters.Clear();
                cmdTimeToCall.Parameters.Add("PersonId", personid);
                cmdTimeToCall.Parameters.Add("DayOfWeekId", Convert.ToInt32(item.Value));
                cmdTimeToCall.Parameters.Add("TimeToCallId", --VALUE FROM DROPDOWNLIST OF CORRESPONDING ITEM --);

            }
        }

如果选择相应的项目,如何从每个下拉列表中获取值?

1 个答案:

答案 0 :(得分:1)

if (item.Selected)
{
    DropDownList ddlTimePeriod = (DropDownList) dllPanel.FindControl(item.Text);

    // now use ddlTimePeriod.SelectedItem.Text, ddlTimePeriod.SelectedItem.Value

    cmdTimeToCall.Parameters.Clear();
    cmdTimeToCall.Parameters.Add("PersonId", personid);
    cmdTimeToCall.Parameters.Add("DayOfWeekId", Convert.ToInt32(item.Value));
    cmdTimeToCall.Parameters.Add("TimeToCallId", --VALUE FROM DROPDOWNLIST OF CORRESPONDING ITEM --);

}