【问题标题】:Input string was not in correct format and remove drop down items from drop down输入字符串的格式不正确并从下拉列表中删除下拉项目
【发布时间】:2012-02-28 06:11:42
【问题描述】:

我有一个要求,我必须根据开始日期和结束日期删除下拉项目。 我已经用谷歌搜索了足够多的方法并尝试了许多简单的方法,例如我可以在下拉列表中删除选定的项目。 这里的问题是,它会引发错误,即输入字符串的格式不正确以及如何从下拉列表中跟踪和删除该项目。 请注意,下拉值是从日历列表开始绑定的,并且已在自定义列表中显示为下拉列表(希望清楚)。

下面是我的代码:

protected void BtnRegister_Click(object sender, EventArgs e)
{
    try
    {
        SPSite oSPSiteCollection = SPContext.Current.Site;
        SPWeb oSPWeb = SPContext.Current.Web;
        SPList oSPList = oSPWeb.Lists["Registered"];
        SPListItemCollection oSPListItemCollection = oSPList.Items;
        SPListItem oSPListItem = oSPListItemCollection.Add();
        DataTable oDataTable = new DataTable();
        oSPListItem["Employee Name"] = oSPWeb.CurrentUser.Email.ToString();
        string[] UsersSeperated = peopleEditorManager.CommaSeparatedAccounts.Split(',');
        SPFieldUserValueCollection UserCollection = new SPFieldUserValueCollection();
        SPFieldUserValue UserName = new SPFieldUserValue();
        foreach (string UserSeperated in UsersSeperated)
        {
            oSPWeb.EnsureUser(UserSeperated);
            SPUser User = oSPWeb.SiteUsers[UserSeperated];
            UserName = new SPFieldUserValue(oSPWeb, User.ID, User.LoginName);
            UserCollection.Add(UserName);
        }
        oSPListItem["peopleEditorManager"] = UserCollection;
        oSPListItem["Practice Name"] = TxtPracticeName.Text;
        oSPListItem["Course Name"] = ddlDrop.SelectedItem;
        oSPListItem["Prerequisite"] = TxtPrerequisite1.Text;
        oSPListItem["Beg Date"] = TxtStartDate.Text;
        oSPListItem["Finish Date"] = TxtEndDate.Text;
        string registeredCourse = oSPListItem["Course Name"].ToString();
        SPList oSPListCourse = oSPWeb.Lists["Scheduled Courses"];
        SPListItemCollection oSPListItemCollectionCourse = oSPListCourse.Items;
        foreach (SPListItem oSPListItemCourse in oSPListItemCollectionCourse)
        {
            string begginingDate = oSPListItemCourse["Start Date"].ToString();
            string finishDate = oSPListItemCourse["End Date"].ToString();
            //input string not in correct format
            if (( Convert.ToInt32(begginingDate) >= Convert.ToInt32(TxtStartDate.Text) ) 
                || (Convert.ToInt32(finishDate) <= Convert.ToInt32(TxtEndDate.Text))) 
            {
                //how to remove the item from drop down if their date is greater than StartDate and less than EndDate
                ddlDrop.Items.Remove(ddlDrop.SelectedItem);
            }
        }  
        oSPListItem.Update();
        LblMessage.Text = "Registeres";
        String fromID = oSPWeb.CurrentUser.Email.ToString();
        String mailTo = UserName.User.Email.ToString();
        string titleName = ddlDrop.SelectedItem.ToString();
        SendEmail_WU(fromID, mailTo, titleName);
    }
    catch (Exception ex)
    {
        LblMessage.Text = ex.Message;
    }
}

请帮忙。谢谢。

【问题讨论】:

    标签: list sharepoint-2010 drop-down-menu


    【解决方案1】:

    根据您的评论:

    如果日期大于 StartDate 且小于 EndDate,如何从下拉列表中删除项目

    听起来您应该使用 Convert.ToDateTimeDateTime.Parse 而不是 Convert.ToInt32。

    此外,您的评论说“和”,但代码中的 || 说“或”。并且你的条件的左右两边需要翻转。

    以下代码应实现您的评论所表达的内容:

    //input string not in correct format
    if ((Convert.ToDateTime(TxtStartDate.Text) > Convert.ToDateTime(begginingDate)) 
        && (Convert.ToDateTime(TxtEndDate.Text) < Convert.ToDateTime(finishDate))) 
    {
        // how to remove the item from drop down if their date is 
        // greater than StartDate and less than EndDate
        ddlDrop.Items.Remove(ddlDrop.SelectedItem);
    }
    

    如果这不能满足您的要求,那么您需要重新审视您的要求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多