Telerik Forums
UI for ASP.NET AJAX Forum
5 answers
170 views
Hello!

I've spent couple of hours trying to figure out how to delete, update or insert data into database via RadGrid. Could you point me out what I'm doing wrong. What I'd like to accomplish is to edit the data via EditForm, insert data via "Add New Record" Command and delete the data.

I've been going through Telerik's documentation, but I'm totally lost.

Codefront:
<telerik:RadGrid ID="gvKontakti" runat="server" AllowPaging="True" AllowSorting="True"
    AutoGenerateColumns="False"
    CellSpacing="0" GridLines="None"
    OnUpdateCommand="gvKontakti_UpdateCommand" Skin="Default"
    onDeletecommand="gvKontakti_DeleteCommand"
    oninsertcommand="gvKontakti_InsertCommand">
    <MasterTableView DataKeyNames="idKontakt" CommandItemDisplay="TopAndBottom">
        <Columns>
            <telerik:GridEditCommandColumn UniqueName="EditCommandColumn">
            </telerik:GridEditCommandColumn>
            <telerik:GridBoundColumn UniqueName="Kontakt" DataField="Naziv" HeaderText="Kontakt">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="Funkcija" HeaderText="Funkcija" DataField="Funkcija">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="Tel1" HeaderText="Telefon 1" DataField="Tel1">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="Tel2" HeaderText="Telefon 2" DataField="Tel2">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="Mob1" HeaderText="Mobitel 1" DataField="Mob1">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="Mob2" HeaderText="Mobitel 2" DataField="Mob2">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="Email1" HeaderText="E-mail 1" DataField="E-mail1">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="Email2" HeaderText="E-mail 2" DataField="E-mail2">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="Fax" HeaderText="Fax" DataField="Fax">
            </telerik:GridBoundColumn>
            <telerik:GridButtonColumn ConfirmText="Obrisati ovog korisnika?" ConfirmDialogType="RadWindow"
                    ConfirmTitle="Brisanje" ButtonType="ImageButton" CommandName="Delete" />
        </Columns>
        <EditFormSettings EditFormType="Template">
            <EditColumn UniqueName="EditColumn">
            </EditColumn>
            <FormTemplate>
                <table>
                    <tr>
                        <td>
                            Ime:
                        </td>
                        <td>
                            <asp:TextBox ID="txtIme" runat="server" Text='<%# Bind("Ime") %>'></asp:TextBox>
                        </td>
                        <td>
                            Prezime:
                        </td>
                        <td>
                            <asp:TextBox ID="txtPrezime" runat="server" Text='<%# Bind("Prezime") %>'></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Funkcija:
                        </td>
                        <td colspan="3">
                            <asp:TextBox ID="txtFunkcija" runat="server" Width="430px" Text='<%# Bind("Funkcija") %>'></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Telefon 1:
                        </td>
                        <td>
                            <asp:TextBox ID="txtTel1" runat="server" Text='<%# Bind("Tel1") %>'></asp:TextBox>
                        </td>
                        <td>
                            Telefon 2:
                        </td>
                        <td>
                            <asp:TextBox ID="txtTel2" runat="server" Text='<%# Bind("Tel2") %>'></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Mobitel 1:
                        </td>
                        <td>
                            <asp:TextBox ID="txtMob1" runat="server" Text='<%# Bind("Mob1") %>'></asp:TextBox>
                        </td>
                        <td>
                            Mobitel 2:
                        </td>
                        <td>
                            <asp:TextBox ID="txtMob2" runat="server" Text='<%# Bind("Mob2") %>'></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Fax:
                        </td>
                        <td>
                            <asp:TextBox ID="txtFax" runat="server" Text='<%# Bind("Fax") %>'></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            E-mail 1:
                        </td>
                        <td>
                            <asp:TextBox ID="txtEmail1" runat="server" Text='<%# Bind("Email1") %>'></asp:TextBox>
                        </td>
                        <td>
                            E-mail 2:
                        </td>
                        <td>
                            <asp:TextBox ID="txtEmail2" runat="server" Text='<%# Bind("Email2") %>'></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Adresa 1:
                        </td>
                        <td colspan="3">
                            <asp:TextBox ID="txtAdresa1" runat="server" Width="430px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Adresa 2:
                        </td>
                        <td colspan="3">
                            <asp:TextBox ID="txtAdresa2" runat="server" Width="430px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Adresa 3:
                        </td>
                        <td colspan="3">
                            <asp:TextBox ID="txtAdresa3" runat="server" Width="430px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Grad:
                        </td>
                        <td>
                            <asp:TextBox ID="txtGrad" runat="server"></asp:TextBox>
                        </td>
                        <td>
                            PoÅ¡tanski broj:
                        </td>
                        <td>
                            <asp:TextBox ID="txtPBR" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Država:
                        </td>
                        <td>
                            <asp:TextBox ID="txtDrzava" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            BiljeÅ¡ke:
                        </td>
                        <td colspan="3">
                            <asp:TextBox ID="txtBiljeske" runat="server" Width="455px" Height="200px" TextMode="MultiLine"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Aktivan korisnik:
                        </td>
                        <td colspan="3">
                            <asp:CheckBox ID="chkAktivan" runat="server" Checked='<%# Bind("Aktivan") %>' />
                        </td>
                    </tr>
                </table>
                <asp:Button ID="btnUpdate" CommandName="Update" Text="Update" runat="server" />
                <asp:Button ID="btnDelete" CommandName="Delete" Text="Delete" runat="server" />
            </FormTemplate>
        </EditFormSettings>
    </MasterTableView>
</telerik:RadGrid>

Codebehind:

protected void Page_Load(object sender, EventArgs e)
{
    var kontakti = BindContacts();
    gvKontakti.DataSource = kontakti;
    gvKontakti.DataBind();
}
 
public object BindContacts()
{
    TSEntities db = new TSEntities();
    var kontakti = from k in db.Kontakts
                   select new { k.idKontakt, Naziv = k.Ime + " " + k.Prezime, Funkcija = k.Funkcija, k.Ime, k.Prezime, k.Tel1, k.Tel2, k.Mob1, k.Mob2, k.Email1, k.Email2, k.Fax, k.Aktivan };
 
    return kontakti;
}
 
protected void gvKontakti_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
    TSEntities db = new TSEntities();
 
    if (e.CommandName == RadGrid.UpdateCommandName)
    {
        if (e.Item is GridEditFormItem)
        {
            GridEditFormItem item = (GridEditFormItem)e.Item;
            int idKontakt = Convert.ToInt32(item.GetDataKeyValue("IdKontakt"));
 
            if (idKontakt > 0)
            {
                Kontakt kontakt = new Kontakt();
                kontakt = db.Kontakts.SingleOrDefault(k => k.idKontakt == idKontakt);
 
                TextBox txtIme = (TextBox)item.FindControl("txtIme");
                TextBox txtPrezime = (TextBox)item.FindControl("txtPrezime");
 
                kontakt.Ime = txtIme.Text;
                kontakt.Prezime = txtPrezime.Text;
                db.SaveChanges();
 
                gvKontakti.Rebind();
            }
        }
    }
}
 
protected void gvKontakti_DeleteCommand(object sender, GridCommandEventArgs e)
{
    TSEntities db = new TSEntities();
    Kontakt kontakt = new Kontakt();
 
    int idKontakt = (int)((GridDataItem)e.Item).GetDataKeyValue("IdKontakt");
 
    kontakt = db.Kontakts.SingleOrDefault(k => k.idKontakt == idKontakt);
 
    if (kontakt != null)
    {
        db.Kontakts.DeleteObject(kontakt);
        try
        {
            db.SaveChanges();
        }
        catch (System.Exception)
        {
             
        }
    }
 
}
 
protected void gvKontakti_InsertCommand(object sender, GridCommandEventArgs e)
{
    TSEntities db = new TSEntities();
}


Any help would be appreciated.

Elliott
Top achievements
Rank 2
 answered on 23 Aug 2011
2 answers
130 views
Hello,

I have a custom appointment with custom tooltips.  On the tooltip there is a rad button that when clicked updates the table that drives the scheduler.  When the button is clicked and the DB is updated i would like to have the schedule update the appointments at the same time.  I have been unable to accomplish this.  Can you point me in the right direction?

Thanks,

Michael.
Michael Constantine
Top achievements
Rank 1
 answered on 23 Aug 2011
2 answers
341 views
Hi,

I am using RAD DatePicker control from a licenced version of telerik cotnrols.
I had to allow the user to choose dates only after 14 days.hence I ahd set
txtdtcontrol.Mindate=Today.AddDays(14)

This should have applied only for choosing the dates.But it is considering this case for getting the date from the database and assigning it to the field also and this is the issue.

Please help solve this problem.How shall I set the calender popup to disable dates lesser than 14 days from now but does not affect for assigning the values to the selectedDate property of the control.

Is it possible to disable dates just on the popup and not to the textbox value as such.
Sachidananda
Top achievements
Rank 1
 answered on 23 Aug 2011
7 answers
214 views
In my tree I have a root node that all other nodes fall under.  I want this to act as a header and have different styles (background image, hover effect, etc..) than the rest of the tree. Is this possible and what classes would I use to achieve this?
Ivan Zhekov
Telerik team
 answered on 23 Aug 2011
1 answer
93 views
Hi. I've loved Telerik's products for some years now; best of breed with great support :)

Any chance the SAK can include the entire Telerik r.a.d. library of controls when it's released? It would sure make life easier for us SharePoint 2010 developers.

Also, any update on a release date? You're keeping us all in suspense!

Thanks and keep up the great work :)

P.S. Whether these other controls are included or not, we're all looking forward to the SAK.
Sebastian
Telerik team
 answered on 23 Aug 2011
3 answers
143 views
Hi

I have a Grid with NestedView Table which I create programmatically. 
Each row on the child table has a checkbox field which selects the whole row.
It has a custom Command header with an Image Button which is acting like an "Add to Cart" button.

When the button is clicked, I can access the selected child items like so:

if (e.CommandName == "AddToCart")
{
  if (RadGrid1.SelectedIndexes.Count == 0)
  {
     RadWindowManager1.RadAlert("No documents have been selected", 250, 100, "Add to Document Cart", "");
     return;
  }
  GridCommandItem Item = (GridCommandItem)e.Item;
  ImageButton btnAddToCart = (ImageButton)e.Item.FindControl("btnAddSelectedToCart");
  System.Collections.ArrayList doclist = new System.Collections.ArrayList();
  foreach (GridDataItem item in RadGrid1.SelectedItems)
  {
    doclist.Add(new HRTDocData(Convert.ToInt32(item["RecordID"].Text), Convert.ToInt32(item["DocID"].Text), item["DocTypes"].Text, item["DocReferences"].Text, item["DocContents"].Text));
  }
        }

I actually need to make a javascript call to some existing code with the list of selected items.  I have tried to access the same information from the client side but it isnt working - I just get null values - or not selected???
This is my js function
function GetChildValues() {
    var grid = $find("<%=RadGrid1.ClientID %>");
    var MasterTable = grid.get_masterTableView();
    var selectedRows = MasterTable.get_selectedItems();
    for (var i = 0; i < selectedRows.length; i++) {
        var row = selectedRows[i];
        var cell = MasterTable.getCellByColumnUniqueName(row, "RecordID")
        alert(cell); 
    }

What am I missing please?


 

Simon
Top achievements
Rank 1
 answered on 23 Aug 2011
1 answer
98 views
I have a dynamic grid, in aspx page it's Defined visible="false", when I Defined him to visible=true in code, do not know the JavaScript functions defined page. The solution?
Vasil
Telerik team
 answered on 23 Aug 2011
3 answers
201 views
I tried to put AsyncUpload into one of my usercontrols, but I always  got null exception error when I tried to get addtional field at code behind. At first I thought that's user control's problem, but  later on, even I put the exact the same code as demo into my test page, I got the same null exception error  on this line :

<%#((Telerik.Web.UI.UploadedFile)Container.DataItem).GetFieldValue("TextBox").ToString()%>

Anyone else got same issue as I got? Pls take a look whenever you have time, really appreciate.

 
<telerik:RadScriptManager ID="ScriptManager1" runat="server" />
    <telerik:RadAsyncUpload runat="server" ID="RadAsyncUpload1" OnClientFileUploaded="onClientFileUploaded">
    </telerik:RadAsyncUpload>
    <telerik:RadButton ID="saveradbtn" runat="server" OnClick="saveradbtn_Click" Text="save image" />
    <asp:Label ID="labelNoResults" runat="server" Visible="True">No uploaded files</asp:Label>
    <asp:Repeater runat="server" ID="Repeater1">
        <ItemTemplate>
            <span style="font-weight: normal; font-size: 10px; color: black; position: relative">
                File info: <%#((Telerik.Web.UI.UploadedFile)Container.DataItem).GetFieldValue("TextBox").ToString()%>,<br />
                File name: <%# DataBinder.Eval(Container.DataItem, "FileName").ToString() %>,<br />
                File size: <%# DataBinder.Eval(Container.DataItem, "ContentLength").ToString() %><br />
                <br />
                <br />
            </span>
        </ItemTemplate>
    </asp:Repeater>
    <script type="text/javascript">
     //<![CDATA[
        var $ = $telerik.$;
 
        function onClientFileUploaded(radAsyncUpload, args) {
            var $row = $(args.get_row());
            var inputName = radAsyncUpload.getID("TextBox");
            var inputType = "text";
            var inputID = inputName;
            var input = createInput(inputType, inputID, inputName);
            var label = createLabel(inputID);
            $row.append("<br/>");
            $row.append(label);
            $row.append(input);
        }
 
        function createInput(inputType, inputID, inputName) {
            var input = '<input type="' + inputType + '" id="' + inputID + '" name="' + inputName + '" />';
            return input;
        }
 
        function createLabel(forArrt) {
            var label = '<label for=' + forArrt + '>File info: </label>';
            return label;
        }
    //]]>
    </script>
 
 
--------------code behind-------------------------------------
 protected void saveradbtn_Click(object sender, EventArgs e)
        {
            if (RadAsyncUpload1.UploadedFiles.Count > 0)
            {
                labelNoResults.Visible = false;
                Repeater1.Visible = true;
                Repeater1.DataSource = RadAsyncUpload1.UploadedFiles;
                Repeater1.DataBind();
            }
            else
            {
                labelNoResults.Visible = true;
                Repeater1.Visible = false;
            }
        }
Peter Filipov
Telerik team
 answered on 23 Aug 2011
5 answers
790 views
Pretty simple question. I'm sure its quite easy but its causing me some trouble.

I've written these 2 lines, neither or which seem to pick up on the line breaks.

desc = Replace(Me.txt_description.Text, System.Environment.NewLine, "<br />")
 
desc = Replace(Me.txt_description.Text, vbCr, "<br />")
Alan T
Top achievements
Rank 1
 answered on 23 Aug 2011
3 answers
148 views
I got on a page 6 radcharts which are dynamically loaded when clicking on a button.

On my local server, I got no problem, the 6 radcharts are loading without any problem.
But on remote server, when I launch my ajax request, 2 to 4 of the charts load (not the same ones everytime), and for the remaining one, I get the following message :

"Error loading RadChart image.
You may also wish to check the ASP.NET Trace for further details.
Display stack trace?"

If I click OK, the website show me the chart that "couldn't be loaded".

I suppose the problem is that the answer to my ajax request come too soon and the website try to show an image that isn't finished to create, isn't it ?

If it's the case, is it a "delay" property or something to make the chart wait X seconds more before binding ?

Regards,
Nicolas
Evgenia
Telerik team
 answered on 23 Aug 2011
Narrow your results
Selected tags
Tags
+? more
Top users last month
Bohdan
Top achievements
Rank 3
Iron
Iron
Iron
Rob
Top achievements
Rank 3
Bronze
Bronze
Iron
Elliot
Top achievements
Rank 1
Iron
Iron
Iron
Sunil
Top achievements
Rank 1
Cynthia
Top achievements
Rank 1
Iron
Iron
Iron
Want to show your ninja superpower to fellow developers?
Top users last month
Bohdan
Top achievements
Rank 3
Iron
Iron
Iron
Rob
Top achievements
Rank 3
Bronze
Bronze
Iron
Elliot
Top achievements
Rank 1
Iron
Iron
Iron
Sunil
Top achievements
Rank 1
Cynthia
Top achievements
Rank 1
Iron
Iron
Iron
Want to show your ninja superpower to fellow developers?
Want to show your ninja superpower to fellow developers?