This is a migrated thread and some comments may be shown as answers.

Default value when editing

4 Answers 190 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Anders
Top achievements
Rank 1
Anders asked on 10 Jun 2014, 04:01 PM
Hello
I am trying to autofill two of the many textboxes in my Radgrid. I want the first textbox to have the username of the logged on user and the other textbox should have current date and time.
With the help of another post I have managed this when creating a new record, but not when editing an existing record.
To be clear, if I edit an existing post I want the textboxes to have new values, current datetime and looged on user.
Here is my code:
Protected Sub RadGrid1_ItemCommand(sender As Object, e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.ItemCommand
 
        If (e.CommandName = RadGrid.InitInsertCommandName) Then
            
            e.Canceled = True
            Dim loggedInUser = User.Identity.Name.ToString()
            Dim myDate As Date = Now()
            
            Dim defaultValue As System.Collections.Specialized.ListDictionary = New System.Collections.Specialized.ListDictionary()
            defaultValue("LastUpdated") = myDate.ToString()
            defaultValue("LastUpdatedBy") = loggedInUser.ToString()
            
            e.Item.OwnerTableView.InsertItem(defaultValue)
            End If
    End Sub


Thanks.

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 11 Jun 2014, 06:04 AM
Hi Anders,

When you a have a BoundColumn in edit, it will automatically bind the textbox in edit to the corresponding value of that row. You can update this row on UpdateCommand and store to db.

ASPX:
<telerik:GridBoundColumn DataField="LastUpdatedBy" HeaderText="LastUpdatedBy" UniqueName="LastUpdatedBy">
</telerik:GridBoundColumn>

VB:
Protected Sub RadGrid1_UpdateCommand(sender As Object, e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.UpdateCommand
  If TypeOf e.Item Is GridEditableItem Then
    Dim editItem As GridEditableItem = DirectCast(e.Item, GridEditableItem)
      'Access the column in editmode and get the entered text
    Dim txtUser As TextBox = DirectCast(editItem("LastUpdatedBy").Controls(0), TextBox)        
    Dim user As String = txtUser.Text
      'Code to save to DB
  End If
End Sub

Thanks,
Princy
0
Anders
Top achievements
Rank 1
answered on 11 Jun 2014, 07:44 AM
Hi Princy and thank you for respondning.

I still dont understand how to set LastUpdatedBy with my new value, what do I write after 'Code to save to DB?

Since these two fields is to be set dynamic I'm thinking of making them not visible. Will that be a problem?

/Anders
0
Accepted
Princy
Top achievements
Rank 2
answered on 12 Jun 2014, 03:57 AM
Hi Anders,

I'm not clear about your requirement. If you donot want to show a column in view you can set its Display as false, and in edit mode if you want to set a default value you can try the following code snippet.

VB:
Protected Sub RadGrid1_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
    If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then
        Dim edit As GridEditableItem = DirectCast(e.Item, GridEditableItem)
        Dim txtLastUpdatedBy As TextBox = DirectCast(edit("LastUpdatedBy").Controls(0), TextBox)
        txtLastUpdatedBy.Text = "Some Value"
    End If
End Sub

In case if you donot want to show it in editmode, then set its ReadOnly property as true and in UpdateCommand you can assign the value and do the Update operation.
If this doesn't help, please provide your full code snippet.

Thanks,
Princy
0
Anders
Top achievements
Rank 1
answered on 13 Jun 2014, 06:42 AM
That is exactly what I wanted.
Thank you.
Tags
Grid
Asked by
Anders
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Anders
Top achievements
Rank 1
Share this question
or