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

Add row based on existing item in DB or add a new entry

1 Answer 33 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Erik
Top achievements
Rank 1
Erik asked on 13 May 2011, 10:18 AM
Hi,

I'd like to use the Grid control to allow for inserting, updating and deleting items. The tricky part is the support of lookup to already existing records on insert/update.

In insert/edit mode, the first textbox will be a lookup, in which the other fields in the row will be filled in if found in DB. If not found, it will be inserted to DB as a new record.

Example:
I enter the ISBN number of a book and make a lookup in the DB to see if it's found, populate the other fields and save it as a row. If ISBN number is not found, then fill out all fields and save it as a new record to the DB.

Any tips about useful articles or code examples are appreciated.

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 13 May 2011, 01:04 PM
Hello Erik,

 You can attach onblur client event to textbox. Then you can use an AjaxManager on page and invoking the ajaxRequest from 'onblur ' client event. In AjaxManager_AjaxRequest server event, search in database for the record with given id(which is entered in TextBox). If the record is existing get the other controls(which is in edit mode) and populate it with corresponding value in database. If the record is not existing you can enter new values and add that record in to database.
aspx:
<EditItemTemplate>
     <asp:TextBox ID="Txt1"  runat="server" OnBlur="hhh();"></asp:TextBox>
</EditItemTemplate>
. . . . . . . .
</RadGrid>
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" OnAjaxRequest="RadAjaxManager1_AjaxRequest"
            DefaultLoadingPanelID="RadAjaxLoadingPanel1">
</telerik:RadAjaxManager>

Javascript:
function funOnBlur()
   {
       var ajxmgr = $find("<%=RadAjaxManager1.ClientID%>");
       ajxmgr.ajaxRequest();
   }

C#:
protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
    {
     int editIndex = Convert.ToInt32(RadGrid1.EditIndexes[0]);//getting the current edit index
     GridEditFormItem editrow = (GridEditFormItem)RadGrid1.MasterTableView.GetItems(GridItemType.EditFormItem)[editIndex];//accessing edit row.
       //get database record depends on the condition populate edit form control   
    }

Thanks,
Shinu.
Tags
Grid
Asked by
Erik
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or