我有两个问题Checkbox_OnCheckedChanged
,Dropdownlist_OnSelectedIndexChanged
事件没有触发我后面的代码中的相关函数。
两个控件都绑定到 a 中的值GridView
。在从 UI 引发的第一个OnCheckedChanged
或OnSelectedIndexChanged
事件中,后面的代码中的函数不会出错,但是如果我刷新我UpdatePanel
的 ,例如通过更改内部数据的日期范围GridView
并因此加载或多或少的记录OnCheckedChanged
,OnSelectedIndexChanged
则更长的调用,但是PostBack
发生了。
我相信这些函数没有被调用,而是调用了重新绑定的函数GridView
:
private void report_DateChanged(object sender, EventArgs e)
{
// Bind GridView here..
}
但是我无法弄清楚为什么会发生这种情况。
我有一个 MasterPage:
namespace ReportingSystemV2.Reporting
{
public partial class Reporting : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
// Attach to DateChanged Event on DateChange UserControl to Function
DateRangeSelect.dateChanged += new DateRangeSelect.dateChangedEventHandler(dtRgSel_dateChanged);
}
// Declare an event for the content pages
public event EventHandler reportDateChanged;
// Called on a date change event
protected void dtRgSel_dateChanged(object sender, DateChangedEventArgs e)
{
// Raise the event for content pages
if (reportDateChanged != null)
reportDateChanged(this, EventArgs.Empty);
}
}
这MasterPage
有一个Control
用于选择我的日期范围..
我的控制ascx
代码的主要代码部分是:
<%--UpdatePanel to trigger datechange--%>
<asp:UpdatePanel runat="server" ID="UpdatePanelDateChanged" OnLoad="UpdatePanelDateChanged_Load" >
</asp:UpdatePanel>
<%--When the date changes in javascript a postback is called in the updatepanel--%>
__doPostBack('<%=UpdatePanelDateChanged.ClientID %>', null);
这个控件的主要代码部分是:
// Declare a delegate
public delegate void dateChangedEventHandler(object sender, DateChangedEventArgs e);
// Declare an event for any pages that have the control
public event dateChangedEventHandler dateChanged;
protected virtual void OnDateChanged(DateChangedEventArgs e)
{
dateChangedEventHandler handler = dateChanged;
// Raise the event
if (handler != null)
handler(this, e);
}
protected void UpdatePanelDateChanged_Load(object sender, EventArgs e)
{
// Trigger the controls public event
OnDateChanged(new DateChangedEventArgs(DateTime.Parse(dates[0]), DateTime.Parse(dates[1])));
}
在我的内容页面中,我的aspx
代码是:
<%@ Page Title="Downtime" Language="C#" MasterPageFile="~/Reporting/Reporting.Master" AutoEventWireup="true" CodeBehind="GensetDowntime.aspx.cs" Inherits="ReportingSystemV2.Reporting.GensetDowntime" %>
<%@ MasterType VirtualPath="~/Reporting/Reporting.Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ReportingSubContent" runat="server">
<div class="container-fluid">
<asp:UpdatePanel ID="updPanelDowntime" runat="server">
<ContentTemplate>
<div class="row">
<div id="downtimeDiv" runat="server">
<asp:GridView ID="gridDowntime" runat="server" AutoGenerateColumns="False" GridLines="None" CssClass="table table-striped table-condensed"
OnRowDataBound="gridDowntime_RowDataBound"
OnSelectedIndexChanged="gridDowntime_SelectedIndexChanged"
DataKeyNames="ID,ID_Location,iddown,idup,dtdown,dtup,isexempt" EmptyDataText="No exempts in the selected period.">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TemplateField HeaderText="Exempt?">
<ItemTemplate>
<asp:DropDownList ID="ddlDowntimeExempt" AutoPostBack="true" runat="server"
OnSelectedIndexChanged="ddlDowntimeExempt_SelectedIndexChanged">
<asp:ListItem Value="-1">Unverified</asp:ListItem>
<asp:ListItem Value="1">Yes</asp:ListItem>
<asp:ListItem Value="0">No</asp:ListItem>
</asp:DropDownList>
<asp:Label ID="lblDowntimeExempt" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"isexempt")%>' Visible="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Exclude?">
<ItemTemplate>
<asp:CheckBox ID="chkDowntimeExclude" runat="server" Checked='<%#Convert.ToBoolean(Eval("ISEXCLUDED")) %>' OnCheckedChanged="chkDowntimeExclude_CheckedChanged" AutoPostBack="true"/>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowSelectButton="True" SelectText="Details" />
</Columns>
</asp:GridView>
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</asp:Content>
最后,我的内容页面背后的代码是:
protected void Page_PreInit(object sender, EventArgs e)
{
// Attach to UserControl Event on the MasterPage
Master.reportDateChanged += new EventHandler(report_DateChanged);
}
// If the user changes the date lets update the table
private void report_DateChanged(object sender, EventArgs e)
{
// Bind the gridview
}
// Function to be called when the ddl selectedindex is changed, only called on initial page load
protected void ddlDowntimeExempt_SelectedIndexChanged(object sender, EventArgs e)
{
// Applies the values selected in the exempt DDL to the database value
}
// function to be called when the checkbox checked state is change, again only called on the initial page loading - not after a postback
protected void chkDowntimeExclude_CheckedChanged(object sender, EventArgs e)
{
// When the Exclude checkbox is changed, update the Db
}
上面的代码显示了control
使用 javascript 到.中trigger
的公共事件。有一个附加到事件的函数,这个函数会更新一些值并触发一个新的事件,该事件可由 my 访问。将一个函数附加到这个事件,当它被改变时,它将加载数据并绑定它。PostBack
UpdatePanel
triggers
Control
MasterPage
Control
MasterPage
ContentPage
ContentPage
MasterPage
DateRange
GridView