简体   繁体   中英

Webforms: How to disable a button that is inside a Telerik RadGrid in code behind

I am new working with webforms. I learned MVC first. I have a Telerik RadGrid that has a MasterTableView inside and then, a couple of columns inside this MasterTableView. I want to simply disable some buttons in the code behind but Visual Studio keeps telling me that the buttons does not exists. In a Google search I found that the reason is because the buttons are inside the RadGrid. However I didn't find any example to access them.

The buttons are inside the radgrid and they looks like this:

                                    <telerik:GridTemplateColumn HeaderStyle-Width="72px" HeaderText="Acciones" >
                                        <ItemTemplate >
                                            <div style="width: 100px">
                                                <span  style="position:relative;" class="grid-buttonColor1">
                                                    <i class="material-icons">create</i>
                                                    <asp:Button ID="btnEditReportDetail"  
                                                        CommandArgument='<%# Item.ReportDetailId %>' 
                                                        OnClick="btnReportDetail_Click"
                                                        runat="server" 

                                                        Style="position:absolute; opacity:0;  top:0; left:0; width:100%; height:100%;"  
                                                type="button" 
                                                causesvalidation="false" />
                                                </span>
                                                &nbsp;
                                                <span style="position: relative;" class="grid-buttonColor2">
                                                    <button 
                                                        type="button" 
                                                        style="background-color: transparent; border: none; padding: 0" 
                                                        data-toggle="modal" 
                                                        data-target="#MessageBoxModal" 
                                                        onclick="ShowMessageBoxWithMessage_<%= ucMessagebox.ClientID%>('Confirmación', '¿Está seguro que desea eliminar la tarea?','DeleteTaskReports','<%# Item.ReportDetailId.ToString() %>')">
                                                        <i class="material-icons prefix">delete</i>
                                                    </button>
                                                </span>
                                            </div>
                                        </ItemTemplate>
                                    </telerik:GridTemplateColumn>

How can I access those buttons in order to write in the code behind something like: buttonName.Enabled = false;

Please! This is driving me crazy!

Thank you guys!

May be this will help you

protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
{ 
    if (e.Item is GridDataItem) 
    { 
        GridDataItem item = (GridDataItem)e.Item; 
        Button btn = item.FindControl("img1") as Button; 
        btn.Enabled = false;            

    } 
 } 

or

protected void RadGrid1_PreRender(object sender, EventArgs e)
{
if ("your Condition")
{
    foreach (GridDataItem dataItem in RadGrid1.MasterTableView.Items)
    {
        ((Button)cmdItem.FindControl("btnEditReportDetail")).Visible = false;
    }
}
}

You need to use FindControl to find server control that inside of Grid.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    Button button = e.Row.FindControl("Button1") as Button;
    button.Enabled = false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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