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

Dynamic defaults on insert...

6 Answers 89 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Raymond
Top achievements
Rank 1
Raymond asked on 23 Jan 2014, 08:29 PM
Hi,

I have a button outside the grid that calls the MasterTableView.InsertItem, passing in a ListDictionary object containing values for the fields as follows:

Protected Sub btnClick(sender As Object, e As EventArgs)
  Dim newValues As System.Collections.Specialized.ListDictionary = New System.Collections.Specialized.ListDictionary()
  newValues.Add("URL","www.123.com")
  newValues.Add("Descr","test insert")
 
  rgDocs.MasterTableView.InsertItem(newValues)
End Sub

In order to display the URL as a link, I'm using template columns as indicated below.  When the button click event (above) fires, it inserts a record.  The Descr field gets the value passed in newValues, but the URL field does not.  I don;t see why one would work and the other not.

As a workaround, I have been trying to add code in the ItemCreated event to transfer the values to the edit controls.  Are the values passed into the InsertItem method available in the ItemCreated event?  So far, I have been unable to find anything in the GridDataInsertItem that contains the values passed.



<telerik:GridTemplateColumn
    UniqueName="Descr"
    DataField="Descr"
    HeaderText="Description"
    >
    <ItemTemplate>
        <%# Eval("Descr")%>
    </ItemTemplate>
    <EditItemTemplate>
        <telerik:RadTextBox id="txtDescr" Runat="server" Text='<%# Eval("Descr")%> ' />
    </EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn
    UniqueName="URL"
    DataField="URL"
    HeaderText="URL"
    >
    <ItemTemplate>
        <a target='_blank' href='<%#Eval("URL")%>'><%#Eval("URL")%></a>
    </ItemTemplate>
    <EditItemTemplate>
        <telerik:RadTextBox id="txtURL" Runat="server" Text='<%# Eval("URL")%> ' />
    </EditItemTemplate>
</telerik:GridTemplateColumn>

6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 24 Jan 2014, 06:05 AM
Hi Raymond,

I guess you want to set a default value during insert mode, you can do it in the ItemCommand event of the Radgrid as follows:

VB:
Protected Sub RadGrid1_ItemCommand(sender As Object, e As GridCommandEventArgs)
    If e.CommandName = RadGrid.InitInsertCommandName Then
        e.Canceled = True
        e.Item.OwnerTableView.InsertItem()
        Dim insertedItem As GridEditableItem = e.Item.OwnerTableView.GetInsertItem()
        Dim txt As RadTextBox = DirectCast(insertedItem.FindControl("txtURL"), RadTextBox)
        txt.Text = "www.123.com"
    End If
End Sub

Thanks,
Princy
0
Raymond
Top achievements
Rank 1
answered on 24 Jan 2014, 05:12 PM
Hi Princy,

Thanks for the response...and I appreciate your attempt at providing a work around...but that doesn't address the question about what the InsertItem method does with the values passed.

The reason this is an issue is that I actually have several buttons that can insert items into the grid.  Each button applies different default values to different fields.  Trying to calculate which defaults to use from within the ItemCommand event is possible...but would make coding considerably more complicated.

I've found a different workaround that seems to work (see the code below).  That is to call the InsertItem method with no parameters.  Then use the GetInsertItem function to retrieve the newly inserted item, Then find the controls and set the values as needed. 

But again...this all seems like it would be unnecessary if the InsertItem method consistently handled the values passed as a parameter.  In short...is it not applying the values consistently a bug in the InsertItem method?  If so, is that bug being addressed?  If it's not a bug, then why does it apply one and not the other?  And am I just not looking in the right place within the ItemCommand event to find the values being passed in?

Anyway...the below code seems to work. 

Thanks,

Ray
'Contents of button click event
Dim NewURL as string
 
...calculate value of NewURL...
 
rgDocs.MasterTableView.InsertItem()
 
Dim ie As GridEditableItem = rgDocs.MasterTableView.GetInsertItem()
 
Dim txtURL As RadTextBox = TryCast(ie.FindControl("txtURL"), RadTextBox)
If Not IsNothing(txtURL) Then txtURL.Text = NewURL
0
Konstantin Dikov
Telerik team
answered on 29 Jan 2014, 02:51 PM
Hello Raymond,

I have tested the approach from your initial post and everything is working as expected on my end.

For your convenience I have prepared a sample page with the same GridTemplateColumns as yours and the same code on a server-side OnClick event of a button. Please give it a try and see what differs in your project.

If you are observing the same issue with the attached sample page, please elaborate on the version that you are using.


Regards,
Konstantin Dikov
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the UI for ASP.NET AJAX, subscribe to the blog feed now.
0
Raymond
Top achievements
Rank 1
answered on 29 Jan 2014, 04:23 PM
Hi,

You indicated you had attached a sample page, but I didn't see it attached to the post.  Did you forget to attach it, or did I miss it?

Thanks,
0
Raymond
Top achievements
Rank 1
answered on 29 Jan 2014, 09:21 PM
Not sure where it went, but there was another post confirming the existence of the file attachment, and posting the file again.  However, now I don't see that confirmation post...or the file attachment.  Since I found a workaround, the urgency of this question has dropped.  However, I would still like to get an answer on passing an array of defaults into the insert call.  That seems like a useful capability.

Thanks

Ray
0
Konstantin Dikov
Telerik team
answered on 30 Jan 2014, 03:30 PM
Hi Raymond,

You are right that another post was present for a while, but I just wanted to test if the files will be attached with the new post. 

Nevertheless, now the attached file should be visible in my previous post. 


Regards,
Konstantin Dikov
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the UI for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
Raymond
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Raymond
Top achievements
Rank 1
Konstantin Dikov
Telerik team
Share this question
or