1

我有两个问题Checkbox_OnCheckedChangedDropdownlist_OnSelectedIndexChanged事件没有触发我后面的代码中的相关函数。

两个控件都绑定到 a 中的值GridView。在从 UI 引发的第一个OnCheckedChangedOnSelectedIndexChanged事件中,后面的代码中的函数不会出错,但是如果我刷新我UpdatePanel的 ,例如通过更改内部数据的日期范围GridView并因此加载或多或少的记录OnCheckedChangedOnSelectedIndexChanged则更长的调用,但是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 访问。将一个函数附加到这个事件,当它被改变时,它将加载数据并绑定它。PostBackUpdatePaneltriggersControlMasterPageControlMasterPageContentPageContentPageMasterPageDateRangeGridView

4

3 回答 3

0

母版页在“子”页中的控件开头附加额外的名称。您很可能希望下拉列表有一个名称,但它会有一个不同的更长的名称,例如 ctrl1$something$dropdown。

使用 Chrome 或开发工具中的检查检查 ida,首先确认这是您的问题。

于 2016-02-19T02:49:06.403 回答
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);  
}  
于 2016-02-19T06:55:25.603 回答
0

问题解决了,通过修改 my 中的代码DateRangeControl不再UpdatePanel触发 all PostBacks,只有__doPostBack('<%=UpdatePanelDateChanged.ClientID %>', null);在 my中调用它的那些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/

于 2016-02-19T15:53:11.663 回答