Web用户控件获取父控件ID?

时间:2013-01-23 13:08:01

标签: c# asp.net user-controls ascx

ASCX

<dx:ASPxGridViewExporter ID="ASPxGridViewExporter1" runat="server">
</dx:ASPxGridViewExporter>

<dx:ASPxButton ID="buttonSaveAs" runat="server" ToolTip="Export and save" Style="vertical-align: middle;"
                OnClick="buttonSaveAs_Click" Text="Save" Width="51px" />

包含ASCX以上的ASPX

<div>
    //ASCX
</div>
<div>
    <dx:ASPxPivotGrid ID="ASPxPivotGridElectricMeterReadings" runat="server" ClientIDMode="AutoID"
        DataSourceID="SqlDataSourceElectricMeterReadings" EnableCallBacks="False" Width="1000px">
    </dx:ASPxPivotGrid>
</div>

ASCX.cs

protected void buttonSaveAs_Click(object sender, EventArgs e)
{
    // I want to get pivotGridId from ASPX
    ASPxGridViewExporter1.ASPxPivotGridID = ASPX-PivotGrid-ID
}

如何将ASPX控件ID传递给ASCX button_click_event?

2 个答案:

答案 0 :(得分:1)

只需在后面的ascx代码中添加一个属性,然后在aspx initialize事件中填充它。

<强> ASCX.cs

public string PivotGridID { get; set; }

<强> ASPX.cs

if (!IsPostBack)
{
    exportPivotGrid.PivotGridID = ASPxPivotGridElectricMeterReadings.ID;
}

答案 1 :(得分:0)

在页面加载事件(用户控件)中尝试声明父控件,并且必须在回发事件之外。

 (this.Parent.Parent).FindControl("ErrLabel") as Label;