0

我在updatepanel控件内的loginview中有formview,formview包含一个链接按钮。我必须将链接按钮注册为updatepanel触发器。

<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>

和后面的代码

  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);
       }

错误:使用新关键字创建对象实例很快无法访问链接按钮请帮助我谢谢

4

2 回答 2

2

由于 FormView 使用绑定数据时填充的模板,因此您只能像这样抓住 DataBound 事件的控件:

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.
    }
}
于 2013-03-05T20:13:19.177 回答
0

这可以通过上一个问题来回答:

asp.net findcontrol方法返回null

您是否尝试在 Page_load 事件中访问它?

于 2013-03-05T20:14:48.820 回答