我有一个要求,我必须根据开始日期和结束日期删除下拉项目。 我已经足够搜索并尝试了许多简单的方法,例如我可以删除下拉列表中的所选项目。 这里的问题是,它抛出错误,输入字符串格式不正确以及如何从下拉列表中跟踪和删除项目。 请注意,下拉值从日历列表开始绑定,并且已经显示在自定义列表中作为下拉列表(希望很清楚)。
以下是我的代码:
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;
}
}
请帮忙。谢谢。
答案 0 :(得分:0)
根据您的评论:
如果日期大于StartDate且小于EndDate,则如何从下拉列表中删除该项目
听起来你应该使用Convert.ToDateTime或DateTime.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);
}
如果这不符合您的要求,那么您需要重新审视您的要求。