开发者

RadGrid Column editable on insert but set to readonly on update

开发者 https://www.devze.com 2023-01-23 13:46 出处:网络
I have a 开发者_如何转开发RadGrid which has a column like: <telerik:GridTemplateColumn HeaderText=\"Car\" >

I have a 开发者_如何转开发RadGrid which has a column like:

<telerik:GridTemplateColumn HeaderText="Car" >
    <ItemTemplate>
        <asp:Label ID="MakeLabel" runat="Server" Text='<%# Eval("Make") %> />
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="MakeTextBox" runat="Server" Text='<%# Bind("Make") %> />
    </EditItemTemplate>
</telerik:GridTemplateColumn >

and I'm wanting to set it up so that this column will allow input when inserting new values but won't when updating values.

Can someone please make a suggestion?


I think the simplest way for you to accomplish this would be by adding an InsertItemTemplate and changing the EditItemTemplate to a Label.

<telerik:GridTemplateColumn HeaderText="Car" >
    <ItemTemplate>
        <asp:Label ID="MakeLabel" runat="Server" Text='<%# Eval("Make") %> />
    </ItemTemplate>
    <InsertItemTemplate>
        <asp:TextBox ID="MakeTextBox" runat="Server" Text='<%# Bind("Make") %> />
    </InsertItemTemplate>
    <EditItemTemplate>
        <asp:Label ID="MakeLabel" runat="Server" Text='<%# Eval("Make") %> />
    </EditItemTemplate>
</telerik:GridTemplateColumn >

This way, you don't have to mess around with it in your code behind.


I found your question when searching for a solution to this myself. I was able to cobble something together that works; don't know if it's the best way, but I tend to go with what works. ;-)

Set your GridTemplateColumn as ReadOnly="true", and be sure to give it a UniqueName property. Then create a PreRender handler for the grid that does this:

    Private Sub RadGrid1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadGrid1.PreRender
        For Each column As GridColumn In RadGrid.Columns
            If column.UniqueName = "MyName" Then
                If column.Owner.IsItemInserted Then
                    CType(column, GridTemplateColumn).ReadOnly = False 
                Else
                    CType(column, GridTemplateColumn).ReadOnly = True
                End If
                Exit For
            End If
        Next

        RadGrid1.Rebind()
    End Sub


I ended up going with in the Page_Load

myTextBox.Enabled = Parent.NamingContainer is GridEditFormInsertItem;

This is in the usercontrol used for updates/inserts of data

0

精彩评论

暂无评论...
验证码 换一张
取 消