简体   繁体   English

如何从GridViewRow投射WebControl

[英]How to cast a WebControl from a GridViewRow

I have some GridView with a changing amount of Columns. 我有一些带有变化的列数的GridView Just the last column is always the same, but they don't have the same name. 仅最后一列始终是相同的,但是它们的名称不同。

<asp:GridView>
    <%-- columns are here--%>
    <asp:TemplateField>
        <ItemTemplate>
            <asp:LinkButton>
            </asp:LinkButton>
        </ItemTemplate>
    </asp:TemplateField>
</asp:GridView>

Now I loop trough my page to disable all the controls on a specific status: 现在,我循环浏览我的页面以禁用处于特定状态的所有控件:

public static void disableControls(ContentPlaceHolder cph)
{

    foreach (Control c in cph.Controls)
    {
        disableControl(c);
        foreach (Control c2 in c.Controls)
        {
    //and so on, this goes until 20 controls deep
        }
    }
}

and in my disableControl() I check for all types ( TextBox , Button etc) and then I disable them. 在我的disableControl()我检查所有类型( TextBoxButton等),然后禁用它们。 The onliest exception are the GridView s. 唯一的例外是GridView Because if I disable them they aren't scrollable anymore. 因为如果禁用它们,它们将不再滚动。 So i tried to keep the GridView s enabled and just loop through the rows and try to cast every cell to a LinkButton to disable it. 因此,我尝试保持GridView的启用状态,并仅遍历各行,并尝试将每个单元格LinkButton转换为LinkButton以禁用它。 Because somehow they don't get disabled trough my normal loop with all the controls and its childcontrols. 因为以某种方式,它们不会通过我所有控件及其子控件的正常循环而被禁用。 But this also doesn't work, because they never get casted. 但这也不起作用,因为它们永远不会被抛弃。 What I tried till now (and didn't work): 到现在为止我一直尝试的方法(没有用):

Try 1: 尝试1:

foreach (GridViewRow gvr in gv.Rows)
{
    if (gvr.RowType == DataControlRowType.DataRow)
    {
        for (int i = 0; i < gv.Columns.Count; i++)
        {
            try
            {
                System.Web.UI.WebControls.LinkButton lb = (System.Web.UI.WebControls.LinkButton)gvr.Cells[i].Controls[0];
                lb.Enabled = false;
             }
             catch(Exception)
             {}              
        }
    }
}

Try 2: 尝试2:

foreach (GridViewRow gvr in gv.Rows)
{
    foreach (TableCell cell in gvr.Cells)
    {
        foreach (Control con in cell.Controls)
        {
            try
            {
                System.Web.UI.WebControls.LinkButton lb = (System.Web.UI.WebControls.LinkButton)con;
                lb.Enabled = false;
            }
            catch (Exception)
            { }
        }
    }
}

How can I make sure that the LinkButton s in the GridView s get disabled, but the GridView itself not? 如何确保GridView中的LinkButton被禁用,但GridView本身未禁用?

Btw: What came to my mind after writing all this long text: Is it possible that "Parent"-Controls(The GridViews) Enabled -Status overwrites the "Child"-Controls-Enabled-Status?! 顺便说一句:写完所有这么长的文字后,我想到了什么:“父项”-控件(GridViews)已Enabled状态是否会覆盖“子级”-控件-已启用状态?

The issue is your check assumes it's a linkbutton, but you type check the control first: 问题是您的检查假定它是一个链接按钮,但是您首先键入check控件:

if (lb is System.Web.UI.WebControls.LinkButton)
{
    ((System.Web.UI.WebControls.LinkButton)con).Enabled = false
}

You can also type check on WebControl, and disable all web controls that way too. 您还可以在WebControl上键入check,并以这种方式禁用所有Web控件。

I went around it with manually disabling all controls (looping through the panel-children & its children etc, recursively) of the panels 我通过手动禁用面板的所有控件(递归地遍历面板子级及其子级等)来解决它

Somehow the "scrollable"-function doesn't gets lost if you set all WebControls by hand to disabled, if you set only the Panel to disabled it gets inherited and the "scrollable"-function of the GridView gets lost. 如果您将所有WebControl手动设置为Disabled,那么以某种方式不会丢失“ scrollable”功能,如果仅将Panel设置为Disabled,则会继承它,而GridView的“ scrollable”功能也会丢失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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