【问题标题】:asp.net findcontrol method return null after AsyncPostBackTriggerasp.net findcontrol 方法在 AsyncPostBackTrigger 后返回 null
【发布时间】:2013-07-22 07:38:55
【问题描述】:

我有一个大问题,当我使用完整的回发时,我可以在DataList 的..._ItemDataBound(...) 事件中找到控件,但是当使用AsyncPostBackTrigger 时,我找不到控件并给我null。

这是我的 aspx 代码:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
    <asp:DataList ID="DataListGallery" runat="server" RepeatColumns="3" RepeatDirection="Horizontal" OnItemDataBound="DataListGallery_ItemDataBound"   >
       <ItemTemplate>
            <asp:LoginView ID="LoginView1" runat="server">
               <LoggedInTemplate>
                   <asp:HiddenField ID="FieldPhoneId" Value='<%# Eval("Phone_InfoID") %>' runat="server" />                    
                       <img src="../images/cart.gif" alt="" title="" border="0" class="left_bt" /></a>--%>
                           <asp:ImageButton ID="btnShop" OnClick="btnShop_Click" ImageUrl="images/cart.gif" CssClass="left_bt_item" title="header=[خريد] body=[&nbsp;] fade=[on]" runat="server" />                    
                       <img src="../images/favs.gif" alt="" title="" border="0" class="left_bt" /></a>--%>
                           <asp:ImageButton CssClass="left_bt_item" title="header=[مورد علاقه] body=[&nbsp;] fade=[on]" OnClick="btnFavourite_Click" ID="btnFavourite"  ImageUrl="images/unfav.png" runat="server" />                      
                       <img src="../images/favorites.gif" alt="" title="" border="0" class="left_bt" /></a>--%>                    
               </LoggedInTemplate>

<Triggers>
                   <asp:AsyncPostBackTrigger ControlID="LinkButton1" EventName="Command"></asp:AsyncPostBackTrigger>
                   <asp:AsyncPostBackTrigger ControlID="LinkButton2" EventName="Command" />
                   <asp:AsyncPostBackTrigger ControlID="LinkButton3" EventName="Command" />
                   <asp:AsyncPostBackTrigger ControlID="LinkButton4" EventName="Command" />
                   <asp:AsyncPostBackTrigger ControlID="LinkButton5" EventName="Command" />
                   <asp:AsyncPostBackTrigger ControlID="LinkButton6" EventName="Command" />
                   <asp:AsyncPostBackTrigger ControlID="LinkButton7" EventName="Command" />
                   <asp:AsyncPostBackTrigger ControlID="LinkButton0" EventName="Command" />
                   <asp:AsyncPostBackTrigger ControlID="btnSearchHead" EventName="Command" />
                   <asp:AsyncPostBackTrigger ControlID="LinkButton8" EventName="Command" />
                   <asp:AsyncPostBackTrigger ControlID="lnkNext" EventName="Click" />
                   <asp:AsyncPostBackTrigger ControlID="lnkPrevious" EventName="Click" />                   
               </Triggers> 

和代码隐藏:

protected void DataListGallery_ItemDataBound(object sender, DataListItemEventArgs e)
{
    if (User.Identity.IsAuthenticated)
    {
        // Get LoginView for access to ImageButton on it.
        var loginView = e.Item.FindControl("LoginView1");

        ImageButton btnFav = (ImageButton)loginView.FindControl("btnFavourite");
        HiddenField hf = (HiddenField)loginView.FindControl("FieldPhoneId");

        List<int> listFav = (List<int>)Session["Fav"];

        if (listFav.Contains(int.Parse(hf.Value)))
            btnFav.ImageUrl = "~/images/favs.gif";
    }
}

当我登录并使用AsyncPostBackTrigger 时,我无法访问这些控件:btnFav 和hf。请注意,它们位于 LoginView 内。

谢谢。

【问题讨论】:

    标签: asp.net postback findcontrol loginview


    【解决方案1】:

    我从这个网站解决了我的问题: enter link description here

     protected void Page_Load(object sender, EventArgs e)
    {
        //http://www.aspdotnetfaq.com/Faq/how-to-determine-whether-an-asynchronous-partial-postback-has-occurred-on-page.aspx
    
        // get a reference to ScriptManager and check if we have a partial postback
    
        if (ScriptManager.GetCurrent(this.Page).IsInAsyncPostBack)
        {
    
            // partial (asynchronous) postback occured
    
            // insert Ajax custom logic here  
    
        }
        // enable property is re-creating page controls
        else if (!Page.IsPostBack || enable)
        {
            //enable = false;
            if (Page.Request["Page"] != null || Page.Request["Page"] != "")
            {...
    

    ] 感谢这个网站。

    【讨论】:

      猜你喜欢
      • 2012-11-21
      • 2013-09-20
      • 2012-10-31
      • 1970-01-01
      • 1970-01-01
      • 2011-10-30
      • 2019-01-09
      • 2013-03-16
      • 2012-08-18
      相关资源
      最近更新 更多