2017-06-23 54 views
0

後面的按鈕我正在使用webforms。我首先學習了MVC。我有一個Telerik RadGrid,裏面有一個MasterTableView,然後在這個MasterTableView中有幾列。我想簡單地禁用代碼背後的一些按鈕,但Visual Studio一直告訴我這些按鈕不存在。在Google搜索中,我發現原因是因爲這些按鈕位於RadGrid內部。但是我沒有找到任何訪問它們的例子。Webforms:如何禁用Telerik 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;

請!這真讓我抓狂!

謝謝你們!

+0

網格是否有服務器端數據綁定事件?如果是的話,我想你可以訪問那裏的按鈕。只是一個狂野的猜測 – Etienne

回答

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; 
    } 
} 
} 
+0

謝謝Vaibhav你的快速答案。你的第二個例子應該幫助只是一個問題。假設是「cmdItem」?它給我一個錯誤。你的意思是在foreach中寫入「dataItem」嵌入? – SamyCode

+0

是的,我改變了cmdItem爲dataItem,現在它的工作。謝謝!! – SamyCode

1

您需要使用FindControl找到服務器控件網格內。

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