简体   繁体   English

如何从.ascx的page_load上访问EditItemTemplate

[英]How to access EditItemTemplate from on page_load on .ascx

I have the following code on usercontrol.ascx 我在usercontrol.ascx上有以下代码

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="OrderStatusEdit.ascx.cs" Inherits="Admin_Controls_OrderStatusEdit" %>
<asp:FormView ID="fvwOrderStatus" runat="server" OnModeChanging="fvwOrderStatus_ModeChanging" OnItemUpdating="fvwOrderStatus_ItemUpdating" OnDataBound="fvwOrderStatus_DataBound" OnPreRender="fvwOrderStatus_PreRender">
    <ItemTemplate>
        <%# WebUtils.GetLocString((string)Eval("Status")) %>
        <asp:Button ID="btnEdit" runat="server" SkinID="Buttons" CommandName="Edit" Text="<%$ Resources:Common,Edit %>" />

    </ItemTemplate>
    <EditItemTemplate>
        <asp:DropDownList ID="ddlStatus" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlStatus_OnSelectedIndexChanged"  DataTextField="DisplayStatus" DataValueField="StatusId">
        </asp:DropDownList>
        <asp:Button ID="btnUpdate" runat="server" SkinID="Buttons" Text="<%$ Resources:Common,Update %>"  CommandName="Update" />
        <asp:Button ID="btnCancel" runat="server" SkinID="Buttons" Text="<%$ Resources:Common,Cancel %>" CommandName="Cancel" CausesValidation="false" />
    </EditItemTemplate>
</asp:FormView>

I used this Control on page.aspx 我在page.aspx上使用了此控件

My question how to disable btnUpdate in Page_Load fucntion so how I can access Button Control ( <asp:Button ID="btnUpdate" ) that exist inside EditItemTemplate and asp:FormView 我的问题是如何在Page_Load功能中禁用btnUpdate,以便如何访问EditItemTemplateasp:FormView中存在的按钮控件( <asp:Button ID="btnUpdate"

protected void Page_Load(object sender, EventArgs e)
    {
        //Here I need to Access btnUpdate ?! 
    }

You can't access child controls of a user control directly from the user control's parent page because the child controls are modified by the protected keyword meaning that they can only be accessed by their class or inheriting classes. 您不能直接从用户控件的父页面访问用户控件的子控件,因为子控件是通过protected关键字修改的,这意味着它们只能由其类或继承的类访问。

There is a Page_Load method on the user control itself, but you won't be able to use that either to access btnUpdate because its contained inside of a FormView's EditItemTemplate. 用户控件本身有一个Page_Load方法,但是您将无法使用该方法来访问btnUpdate,因为它包含在FormView的EditItemTemplate中。 You should, however, be able to use the FormView's DataBound event (which it appears you're already servicing based on your markup) like so: 但是,您应该能够像下面这样使用FormView的DataBound事件(看来您已经在根据标记进行服务了):

protected void fvwOrderStatus_DataBound(object sender, System.EventArgs e)
{
    if(FormView1.CurrentMode == FormViewMode.Edit)
    {
        Button btnUpdate = FindControl("btnUpdate") as Button;
        btnUpdate.Enabled = true;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM