سؤال

I have the following code on 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

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

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.

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

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 مع الإسناد
لا تنتمي إلى StackOverflow
scroll top