RadGrid make field invisible on Edit

时光怂恿深爱的人放手 提交于 2019-12-12 04:48:43

问题


<telerik:RadGrid runat="server" ID="rdReport" AutoGenerateColumns="false" AllowPaging="true" Skin="Metro"  OnItemCommand="ItemCommand" OnItemDataBound="rdReport_ItemDataBound" OnPreRender="rdReport_PreRender" DataSourceID="FountainSource" AllowAutomaticUpdates="true" AllowAutomaticDeletes="true">
    <MasterTableView DataKeyNames="ID" CommandItemDisplay="None">
        <Columns>
            <telerik:GridEditCommandColumn ButtonType="ImageButton" />                                   
            <telerik:GridBoundColumn DataField="LocName" HeaderText="Location" ReadOnly="true" /> 
            <     
            <telerik:GridBoundColumn DataField="Field1Value" HeaderText="Custom Field1" />
            <telerik:GridBoundColumn DataField="Field2Value" HeaderText="Custom Field2" />
            <telerik:GridBoundColumn DataField="Field3Value" HeaderText="Custom Field3" />

            <telerik:GridButtonColumn ConfirmText="Delete?" ConfirmDialogType="RadWindow"
                ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" />
        </Columns>
        <EditFormSettings>
            <EditColumn ButtonType="ImageButton" />
        </EditFormSettings>
    </MasterTableView>
    <PagerStyle Mode="NextPrevAndNumeric" />
</telerik:RadGrid>

On Edit, I like to make a field invisible. I am using the following code which works but want to check to see if it is best practice:

protected void rdReport_ItemDataBound(object sender, GridItemEventArgs e)
{
    // Edit Mode
    if ((e.Item is GridEditFormItem) && (e.Item.IsInEditMode))
    {
        GridEditFormItem fndColumn = (GridEditFormItem)e.Item;
        fnColumn["Field1Value"].Parent.Visible = false;
    }
}

回答1:


The solution which was provided by you is perfect but it would be nice if you will also add UniqueName property in each column. If we will not assign the UniqueName than it is consider DataField value as UniqueName.

ASPX

<telerik:GridButtonColumn DataField="Field1Value" HeaderText="Custom Field1" UniqueName="Field1Value" />

ASPX.CS

protected void rdReport_ItemDataBound(object sender, GridItemEventArgs e)
{
    // Edit Mode
    if ((e.Item is GridEditFormItem) && (e.Item.IsInEditMode))
    {
        GridEditFormItem fndColumn = (GridEditFormItem)e.Item;
        fnColumn["Field1Value"].Parent.Visible = false; // "Field1Value" is column uniquename
    }
}

Let me know if you required more information.




回答2:


This is just a suggested alternative. I've worked with telerik a few times, and it is a pain to say the least. What you could do is also add a tertiary condition to the parent container. If in edit mode, then show a class, such as 'edit', or 'current'. If not in edit mode, don't show the class. Then, in your CSS you can select for whatever element you want to hide.

<div>
<input type="text" id="whateverisgenerated" class="uniqueclass" />
</div>

Then, when in edit mode, you will have

<div class="edit">
<input type="text" id="whateverisgenerated" class="uniqueclass" />
</div>

With your CSS:

div.edit input.uniqueclass { display: none; }

As for your telerik control (I just grabbed something from your code), you can do the following to add your class:

<telerik:GridButtonColumn ConfirmText="Delete?" ConfirmDialogType="RadWindow" ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" CssClass="uniqueclass" />

I added CssClass="uniqueclass" in the control above.

What I provided is just an example. I hope you find a use for it. Thanks



来源:https://stackoverflow.com/questions/29245141/radgrid-make-field-invisible-on-edit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!