Search code examples
asp.netcontrolsupdatepanelformviewloginview

asp.net cannot access update panel inside login view inside formview controls


I have formview inside loginview inside updatepanel control and formview contains a linkbutton.I have to register link button as updatepanel trigger.

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                <div id="comments">
                <h6>20 Diyo</h6>
                <div class=" yorumat-div">

                    <asp:LoginView ID="loginYorumAtView" runat="server">
                    <LoggedInTemplate>

                        <asp:FormView ID="frmYorumAt" runat="server" DefaultMode="Insert"  OnItemInserting="frmYorumAt_ItemInserting" DataSourceID="ods_YorumAt" >

                          <InsertItemTemplate>
                              <asp:TextBox ID="txtYorumAt" runat="server" CssClass="yorumat-txtbox" TextMode="MultiLine"  Text='<%# Bind("Yorum_Text") %>'></asp:TextBox>
                              <asp:LinkButton ID="btnYorumAt" runat="server" CssClass="yorumat-button" CommandName="Insert" >Yolla</asp:LinkButton>
                              <asp:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender1" runat="server" TargetControlID="txtYorumAt" WatermarkText=" Yorum At ....">
                              </asp:TextBoxWatermarkExtender>
                              <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" Display="None" ControlToValidate="txtYorumAt"></asp:RequiredFieldValidator>
                          </InsertItemTemplate>
                        </asp:FormView>

                    </LoggedInTemplate>
                    </asp:LoginView>

                    <asp:ObjectDataSource ID="ods_YorumAt" runat="server" InsertMethod="YorumEkle" 
                        TypeName="yonet" DataObjectTypeName="Yorum" 
                        oninserting="ods_YorumAt_Inserting" >
                       <InsertParameters>
                       <asp:QueryStringParameter DbType="Int32" Name="comment_post_idy" />
                       </InsertParameters>

                    </asp:ObjectDataSource>

                   </div>
                    <ul class="commentlist" >
                    <asp:DataList ID="datalistYorum" runat="server" DataSourceID="ods_yorumlar" ItemStyle-CssClass="commentlist" RepeatLayout="Table">

                       <ItemTemplate>

                        <li class="comment">

                        <div class="comment-body">
                                                   ............<%# Eval("comment")%>... 
                        </div>

                        </li>
                        </ItemTemplate>
                        </asp:DataList>
                       <asp:ObjectDataSource ID="ods_yorumlar" runat="server" 
                            DataObjectTypeName="Yorum" TypeName="yonet" SelectMethod="PostYorumlariGetir" 
                            ondatabinding="ods_yorumlar_DataBinding" onselecting="ods_yorumlar_Selecting">
                       <SelectParameters>
                       <asp:QueryStringParameter Name="comment_post_id" QueryStringField="post_id" Type="Int32" DefaultValue="2" />
                       </SelectParameters>
                       </asp:ObjectDataSource>


                </ContentTemplate>
                <Triggers>

                </Triggers>
                </asp:UpdatePanel>

and code behind

  protected void Page_Load(object sender, EventArgs e)
        {

            //LinkButton btnYorumAt = loginYorumAtView.FindControl("frmYorumAt").FindControl("btnYorumAt") as LinkButton;

            FormView frmYorumAt = (FormView)loginYorumAtView.FindControl("frmYorumAt");
            LinkButton btnYorumAt = (LinkButton)frmYorumAt.FindControl("btnYorumAt");
            ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(btnYorumAt);
       }

error : use the new keyword to create an object instance Shortly cannot acces linkbutton pls help me thanks


Solution

  • As the FormView uses templates which are populated when data is bound, you can only grab your control on the DataBound event like so:

    protected void Page_Load(object sender, EventArgs e)
    {
        FormView frmYorumAt = (FormView)loginYorumAtView.FindControl("frmYorumAt");
    
        frmYorumAt.DataBound += new EventHandler(frmYorumAt_DataBound);
    }
    
    void frmYorumAt_DataBound(object sender, EventArgs e)
    {
        FormView formView = sender as FormView;
    
        // Let's make sure the InsertItemTemplate is actually being run, otherwise 
        // the LinkButton won't exist. 
        if (formView.CurrentMode == FormViewMode.Insert)  
        {
            LinkButton btnYorumAt = (LinkButton)formView .FindControl("btnYorumAt");
    
            // Do whatever you need to do with the LinkButton here.
        }
    }