我遇到的问题是我有两组单选按钮。 当我在其中任何一个中单击“全部”时,必须禁用SharePoint DatePicker控件并显示自定义日期(如图所示)。但是当你点击第二组radiobuttons时,你看到第一个被启用了。 如果你帮我解决这个问题,我将不胜感激。
感谢。
以下是两组单选按钮:
//第一组
<asp:RadioButton ID="Rd4Month" Text="All" AutoPostBack="True" GroupName="GrpDuration" runat="server" OnCheckedChanged="rdgetDateforMonths" />
<asp:RadioButton ID="Rd1Month" Text="1 month" AutoPostBack="True" GroupName="GrpDuration" runat="server" OnCheckedChanged="rdgetDateforMonths" />
<asp:RadioButton ID="Rd2Month" Text="3 month" AutoPostBack="True" GroupName="GrpDuration" runat="server" OnCheckedChanged="rdgetDateforMonths" />
<asp:RadioButton ID="Rd3Month" Text="6 month" AutoPostBack="True" GroupName="GrpDuration" runat="server" OnCheckedChanged="rdgetDateforMonths" />
<asp:RadioButton ID="Rd5Month" Text="Other" AutoPostBack="True" GroupName="GrpDuration" runat="server" OnCheckedChanged="rdgetDateforMonths" />
//第二组
<asp:RadioButton ID="Rd4BuildMonth" AutoPostBack="True" Text="All" GroupName="GrpBuildDuration" runat="server" OnCheckedChanged="rdgetBuildDateforMonths" />
<asp:RadioButton ID="Rd1BuildMonth" AutoPostBack="True" Text="1 month" GroupName="GrpBuildDuration" runat="server" OnCheckedChanged="rdgetBuildDateforMonths" />
<asp:RadioButton ID="Rd2BuildMonth" AutoPostBack="True" Text="3 month" GroupName="GrpBuildDuration" runat="server" OnCheckedChanged="rdgetBuildDateforMonths" />
<asp:RadioButton ID="Rd3BuildMonth" AutoPostBack="True" Text="6 month" GroupName="GrpBuildDuration" runat="server" OnCheckedChanged="rdgetBuildDateforMonths" />
<asp:RadioButton ID="Rd5BuildMonth" AutoPostBack="True" Text="Other" GroupName="GrpBuildDuration" runat="server" OnCheckedChanged="rdgetBuildDateforMonths" />
// SharePoint DateTimeControl
<table>
<tr>
<td style="vertical-align: middle;">
<asp:Label ID="Label4" runat="server" Text="Start:"></asp:Label>
</td>
<td style="vertical-align: middle;">
<SharePoint:DateTimeControl ID="dtcStartDate" runat="server" DateOnly="True" />
</td>
<td style="vertical-align: middle;">
<asp:Label ID="Label5" runat="server" Text="End:"></asp:Label>
</td>
<td style="vertical-align: middle;">
<SharePoint:DateTimeControl ID="dtcEndDate" runat="server" DateOnly="True" />
</td>
</tr>
</table>
代码隐藏:
protected void rdgetDateforMonths(object sender, EventArgs e)
{
//dtcStartDate.Enabled = true;
// dtcStartDate.ClearSelection();
// ViewState["rd4Month"] = "false";
if (Rd1Month.Checked)
{
dtcStartDate.ClearSelection();
dtcStartDate.Enabled = true;
dtcStartDate.SelectedDate = DateTime.Now.AddMonths(-1);
// Label1.Text = DateTime.Now.AddMonths(-1).ToString();
}
if (Rd2Month.Checked)
{
dtcStartDate.ClearSelection();
dtcStartDate.Enabled = true;
dtcStartDate.SelectedDate = DateTime.Now.AddMonths(-3);
}
if (Rd3Month.Checked)
{
dtcStartDate.ClearSelection();
dtcStartDate.Enabled = true;
dtcStartDate.SelectedDate = DateTime.Now.AddMonths(-6);
}
if (Rd4Month.Checked)
{
dtcStartDate.ClearSelection();
DateTime value = new DateTime(2012, 04, 01);
dtcStartDate.SelectedDate = value;
dtcStartDate.Enabled = false;
// ViewState["rd4Month"] = "true";
}
if (Rd5Month.Checked)
{
dtcStartDate.Enabled = true;
dtcStartDate.ClearSelection();
}
}
protected void rdgetBuildDateforMonths(object sender, EventArgs e)
{
// dtcBuildStartDate.ClearSelection();
// dtcBuildStartDate.Enabled = true;
// ViewState["rd4BuildMonth"] = "false";
if (Rd1BuildMonth.Checked)
{
dtcBuildStartDate.Enabled = true;
dtcBuildStartDate.ClearSelection();
dtcBuildStartDate.SelectedDate = DateTime.Now.AddMonths(-1);
}
if (Rd2BuildMonth.Checked)
{
dtcBuildStartDate.ClearSelection();
dtcBuildStartDate.Enabled = true;
dtcBuildStartDate.SelectedDate = DateTime.Now.AddMonths(-3);
}
if (Rd3BuildMonth.Checked)
{
dtcBuildStartDate.Enabled = true;
dtcBuildStartDate.ClearSelection();
dtcBuildStartDate.SelectedDate = DateTime.Now.AddMonths(-6);
}
if (Rd4BuildMonth.Checked)
{
dtcBuildStartDate.Enabled = false;
dtcBuildStartDate.ClearSelection();
DateTime value = new DateTime(2012, 04, 01);
dtcBuildStartDate.SelectedDate = value;
dtcBuildStartDate.Enabled = false;
// ViewState["rd4BuildMonth"] = "true";
}
if (Rd5BuildMonth.Checked)
{
dtcBuildStartDate.Enabled = true;
dtcBuildStartDate.ClearSelection();
}
}
protected void Page_Load(object sender, EventArgs e)
{
//dtcStartDate.MaxDate = System.DateTime.Today;
dtcBuildStartDate.MaxDate = DateTime.Now.AddMonths(-1);
dtcStartDate.MaxDate = DateTime.Now.AddMonths(-1);
dtcEndDate.MaxDate = System.DateTime.Today;
dtcBuildEndDate.MaxDate = System.DateTime.Today; ;
lblErrorMsg.Text = "";
// When the page loads 1st time
if (!Page.IsPostBack)
{
try
{
Rd1Month.Checked = true;
Rd1BuildMonth.Checked = true;
dtcEndDate.SelectedDate = System.DateTime.Today; // set end date calendar to today's date
dtcBuildEndDate.SelectedDate = System.DateTime.Today;
dtcStartDate.SelectedDate = DateTime.Now.AddMonths(-1);
dtcBuildStartDate.SelectedDate = DateTime.Now.AddMonths(-1);
}
catch (Exception ex)
{
lblErrorMsg.Text += ex.Message;
}
}
}
我将DateTimeControl的Enable修改为False
<SharePoint:DateTimeControl ID="dtcBuildStartDate" runat="server" DateOnly="True" Enabled="False" />
答案 0 :(得分:1)
查看刚刚添加的Page_Load方法并将评论读到我之前的帖子。看起来问题在于共享点日期时间控制的行为方式。您可以尝试添加EnableViewState =“true”。
<SharePoint:DateTimeControl ID="dtcBuildStartDate" runat="server" DateOnly="True" Enabled="False" EnableViewState="true" />
如果未打开viewstate,则每次页面回发时都会重新加载aspx页面定义中的默认值。这也会影响控件的值在回发后不被维护等等。如果enableviewstate不起作用,那么你可以尝试在ViewState中自己添加状态然后在每次回发时你必须管理这个值,但它可能会处理变更等问题变得复杂。
您还可以在页面加载时尝试以下操作:
protected void Page_Load(object sender, EventArgs e)
{
//dtcStartDate.MaxDate = System.DateTime.Today;
dtcBuildStartDate.MaxDate = DateTime.Now.AddMonths(-1);
dtcStartDate.MaxDate = DateTime.Now.AddMonths(-1);
dtcEndDate.MaxDate = System.DateTime.Today;
dtcBuildEndDate.MaxDate = System.DateTime.Today; ;
lblErrorMsg.Text = "";
// When the page loads 1st time
if (!Page.IsPostBack)
{
try
{
Rd1Month.Checked = true;
Rd1BuildMonth.Checked = true;
dtcEndDate.SelectedDate = System.DateTime.Today; // set end date calendar to today's date
dtcBuildEndDate.SelectedDate = System.DateTime.Today;
dtcStartDate.SelectedDate = DateTime.Now.AddMonths(-1);
dtcBuildStartDate.SelectedDate = DateTime.Now.AddMonths(-1);
}
catch (Exception ex)
{
lblErrorMsg.Text += ex.Message;
}
}
SetDateBoxEnabled();
}
private void SetDateboxEnabled()
{
if (Rd4BuildMonth.Checked)
{
dtcBuildStartDate.Enabled = false;
}
else
{
dtcBuildStartDate.Enabled = true;
}
if (Rd4Month.Checked)
{
dtcStartDate.Enabled = false;
}
else
{
dtcStartDate.Enabled = true;
}
}