【问题标题】:Webforms: How to disable a button that is inside a Telerik RadGrid in code behindWebforms:如何在后面的代码中禁用 Telerik RadGrid 内的按钮
【发布时间】:2017-11-26 14:43:14
【问题描述】:

我是使用网络表单的新手。我首先学习了 MVC。我有一个 Telerik RadGrid,里面有一个 MasterTableView,然后在这个 MasterTableView 里面有几列。我想简单地禁用后面代码中的一些按钮,但 Visual Studio 一直告诉我这些按钮不存在。在 Google 搜索中,我发现原因是按钮位于 RadGrid 内。但是我没有找到任何示例来访问它们。

按钮位于 radgrid 内,它们看起来像这样:

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

我怎样才能访问这些按钮,以便在下面的代码中写入:buttonName.Enabled = false;

请!这真让我抓狂!

谢谢你们!

【问题讨论】:

  • 网格是否有服务器端数据绑定事件?如果是的话,我认为您可能可以访问那里的按钮。只是一个疯狂的猜测

标签: c# asp.net webforms telerik radgrid


【解决方案1】:

也许这会对你有所帮助

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;            

    } 
 } 

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

【讨论】:

  • 感谢 Vaibhav 对您的快速答复。你的第二个例子应该有帮助。就一个问题。什么是“cmdItem”?它给我一个错误。您的意思是在 foreach 中写入“dataItem”?
  • 是的,我将 cmdItem 更改为 dataItem,现在它可以工作了。谢谢!!
【解决方案2】:

您需要使用FindControl 来查找Grid 内部的服务器控件。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多