我知道这与关于S.O.的许多其他问题类似。但它们似乎都没有适用于这种情况,也没有解决问题。问题是我有一个模态弹出控件,我根据需要传递其他控件。因此,当用户点击我的UI上的一个按钮时,他可以使用一组内容获取该模式,然后当单击另一个按钮时,他将获得不同的内容。一切都工作正常,直到我试图从添加到模态弹出窗口的内容中触发事件。说我已经尽力解决这个问题并不完全准确,但我已经尝试了很多。我越来越相信这不能做到,或者我有一些错误的设置。
这是更新面板的代码
<input id="dummy" type="button" style="display: none" runat="server" />
<asp:ModalPopupExtender CancelControlID="Close" runat="server" ID="mpeThePopup" TargetControlID="dummy" PopupControlID="pnlModalPopUpPanel" BackgroundCssClass="modalBackground" PopupDragHandleControlID="Title" />
<asp:Panel ID="pnlModalPopUpPanel" runat="server" CssClass="modalPopup" Width="400px" Height="600px">
<asp:UpdatePanel ID="udpInnerUpdatePanel" runat="Server">
<ContentTemplate>
<table id="ContentTableTag" runat="server" cellpadding="0" cellspacing="0" style="width: 100%;
height: 100%;">
<tr>
<td id="Title" runat="server" style="background-color: rgb(79,82,90); text-align: left;
height: 28px; width: 90%; color: White;" nowrap="nowrap">
<h4 style="margin: 0px 0px 0px 5px;">
<asp:Label ID="LblSectionTitle" runat="server" Text="Modal"></asp:Label>
</h4>
</td>
<td id="Close" runat="server" style="background-color: rgb(79,82,90); text-align: right;
height: 28px; width: 10%" nowrap="nowrap">
<asp:ImageButton ID="ibClose" runat="server" Style="margin-right: 5px;" ImageUrl="~/WLImages/MLS/button_close.png"
ToolTip="Close" />
</td>
</tr>
<tr>
<td id="MainContentHolder" colspan="2" align="left" style="top: 0px; bottom: 100%;
vertical-align: top; width: 100%; height: 100%" />
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
这是我添加到模态弹出窗口的用户控件的代码,在其后台是两个事件触发器,一个用于日历更改其日期,另一个用于检查复选框项目的时间。
<table id="mainContent" runat="server" visible="false" width="350" height="300">
<tr>
<td>
<asp:label ForeColor="White" runat="server" Text="Exam Date:" />
</td>
<td>
<asp:TextBox ID="txtDate" runat="server" Width="127" ForeColor="Black"
readonly="true" ontextchanged="txtDate_TextChanged"/>
<asp:CalendarExtender ID="CalendarExtender" runat="server"
PopupButtonID="ibtnCalendar" OnClientDateSelectionChanged="checkDate"
TargetControlID="txtDate" onload="LoadCalendar" />
</td>
<td>
<asp:ImageButton ID="ibtnCalendar" ImageUrl="../imgs/btn_calendar.png" Width="20px" runat="server" />
</td>
</tr>
<tr>
<td style="vertical-align: top; padding-top:5px">
<asp:label ForeColor="White" runat="server" Text="Study Days:" />
</td>
<td colspan="2" style="vertical-align: top; padding-top:5px">
<asp:CheckBoxList ID="WeekCheckBox" runat="server" ForeColor="White"
onselectedindexchanged="WeekCheckBox_SelectedIndexChanged">
<asp:ListItem Text="Monday" Value="1" />
<asp:ListItem Text="Tuesday" Value="2" />
<asp:ListItem Text="Wednesday" Value="3" />
<asp:ListItem Text="Thursday" Value="4" />
<asp:ListItem Text="Friday" Value="5" />
<asp:ListItem Text="Saturday" Value="6" />
<asp:ListItem Text="Sunday" Value="7" />
</asp:CheckBoxList>
</td>
</tr>
<tr>
<td>
<asp:label ForeColor="White" runat="server" Text="Study hours per day:" />
</td>
<td colspan="2">
<asp:TextBox ForeColor="Black" ID="tbStudyHours" runat="server" Width="127px" ReadOnly="true" />
</td>
</tr>
</table>
最后,我将控件附加到模态。
private void GenerateCalendarPopup(ExamDateSelector eds)
{
pnlModalPopUpPanel.BackColor = GUI.Instance.GUIColorElement(GUIElements.color_main);
LblSectionTitle.Text = "Exam Date";
pnlModalPopUpPanel.Height = eds.ControlHeight + 40;
pnlModalPopUpPanel.Width = eds.ControlWidth + 25;
eds.ShowControl = true;
MainContentHolder.Controls.Add(eds);
}
感谢您的帮助。
答案 0 :(得分:2)
不知道你的其余代码。我在这里说话,并说:当然它不起作用:P。
执行回发时,需要重新创建page_init事件中占位符中的控件。这样,您就可以在分配viewstate和控制处理程序之前重新创建控件集合。但是,如果不知道其余的代码,那只是猜测。
那说:你为什么要这样做?为什么不简单地使用2个不同的ModalPopupExtenders?即使没有诉诸这种诡计,它本身就是一只野兽。答案 1 :(得分:0)
虽然这可能不是处理我为自己创建的情况的“正确”方法,试图复制我们的Windows产品的流程和风格,我确实找到了解决事件问题的方法在模态弹出窗口中显示时不从此控件触发;虽然以迂回的方式。 首先我在javascript中编写了一个函数,该函数完成了我将放在控件事件中的大部分数学运算;基本上,它考虑到用户选择作为学习日的一周中的几天,计算从现在到特定日期之间剩余的学习时间。然后,我通过使用web方法将我保存的信息传递给数据库。
function calculateStudyTime() {
//do some math
…
PageMethods.SaveModalExamDateChangesToDB(datesString, selectedDate);
}
我将脚本附加到我想在代码隐藏中触发计算的每个控件。在我的情况下,我希望重新计算学习时间,其中一个代表一周中某一天的复选框被切换。所以我将脚本附加到ListItem控件。我是这样做的,因为ListItem控件实际上没有可以通过设计器访问的onclick属性,但可以通过这种方式添加。
protected void LoadCalendar(object sender, EventArgs e)
{
…
foreach (ListItem item in WeekCheckBox.Items)
{
item.Attributes.Add("onclick", "calculateStudyTime()");
}
…
}
最后在调用所有模态弹出框的基页中,我创建了对该基页的静态引用,以及从javascript文件调用的装饰函数。需要对自身的静态引用,因此我可以从静态Web方法访问基页的成员(在本例中是我的上下文)。
Static Dashboard thisPage;
protected void Page_Load(object sender, EventArgs e)
{
…
thisref = this;
…
}
[WebMethod]
[ScriptMethod]
public static void SaveModalExamDateChangesToDB(string days, string date)
{
thisPage.UserSession.UserCourse.ExamDate = DateTime.Parse(date);
thisPage.UserSession.UserCourse.StudyDays = days;
thisPage.UserSession.UserCourse.Save();
}
就像我说它不理想,但它有效。