我的Checkbox_OnCheckedChanged
和Dropdownlist_OnSelectedIndexChanged
事件都没有触发我的代码中的相关功能。
两个控件都绑定到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
使用j trigger
PostBack
UpdatePanel
triggers
Control
MasterPage
中的公共事件}}。 Control
具有附加到MasterPage
事件的函数,此ContentPage
函数会更新一些值并触发可以访问ContentPage
的新事件。 MasterPage
为此DateRange
事件附加了一个函数,当GridView
更改时,它将加载import unittest
from foobar import MyException
import foobar as fb
class FooBarTestCases(unittest.TestCase):
def test_bar(self):
with self.assertRaises(ValueError):
fb.FooBar().bar()
def test_foo(self):
with self.assertRaises(MyException):
fb.FooBar().foo()
if __name__ == '__main__':
unittest.main()
的数据并绑定它。
答案 0 :(得分:0)
母版页在“子”页面的控件开头附加额外的名称。很可能你会期望下拉列表中有一个名称,但它会有一个不同的更长的名称,如ctrl1 $ something $ dropdown。
使用Chrome中的inspect或dev工具检查ida,确认这是您的问题。
答案 1 :(得分:0)
我认为你错过了异步回发触发器。当更新面板部分回发时,它会发送回整个页面,进行处理,然后仅返回更改的部分。可能在这个过程中,发生了轻微的ID更改,然后错过了注册。 report_DateChanged
很可能被解雇,因为它是页面上第一个实现IPostBackEventHandler
的控件(因为复选框和下拉列表上的回发不再起作用)
要解决这个问题,请将应该执行异步回发的每个控件显式添加到更新面板的触发器中。像这样:
protected void gridDowntime_RowDataBound(object sender, GridViewRowEventArgs e)
{
DropDownList ddl = e.Row.FindControl("ddlDowntimeExempt") as DropDownList;
CheckBox cb = e.Row.FindControl("chkDowntimeExclude") as CheckBox;
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(ddl);
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(cb);
}
答案 2 :(得分:0)
问题已解决,通过修改DateRangeControl
中的代码,UpdatePanel
上的PostBacks
不再被__doPostBack('<%=UpdatePanelDateChanged.ClientID %>', null);
触发,只有Javascript
中的protected void UpdatePanelDateChanged_Load(object sender, EventArgs e)
{
if (Request["__EVENTTARGET"] == UpdatePanelDateChanged.ClientID)
{
//Trigger Event
OnDateChanged(new DateChangedEventArgs(DateTime.Parse(dates[0]), DateTime.Parse(dates[1])));
}
}
被调用了http://encosia.com/are-you-making-these-3-common-aspnet-ajax-mistakes/
。
This is a commit msg.
解决方案参考:~/my_repo