开发者

Code Behind Update Command in RadGrid Telerik (help)

开发者 https://www.devze.com 2023-01-08 10:57 出处:网络
Greetings Everyone I am new in asp.net and i\'m using RadControls for Asp.net Ajax now. I tried to followthe Asp.Net GridDemo - Insert/Update/Delete and it seems to be not all working pretty fine wi

Greetings Everyone

I am new in asp.net and i'm using RadControls for Asp.net Ajax now. I tried to follow the Asp.Net GridDemo - Insert/Update/Delete and it seems to be not all working pretty fine with me so i did few changes and turned out to be somehow doing well but the update button still not working...

I have this 2 tables tblUser and tblRole

In my Radgrid i only wanted to display the user_id,last_name and first_name from my tblUser and role from my tblRole

And in my Edit Form The only editable region is the last_name,first_name and role but when i hit the update Button it doesn't work.. -_-,

I'm already ok with the designs the only problem is my update button is not working..

Here's my code

TeacherRole.aspx

 <telerik:RadGrid ID="RadGrid1" runat="server" AllowFilteringByColumn="True" 
        AllowPaging="True" AllowSorting="True" 
        AutoGenerateEditColumn="True" DataSourceID="SqlDataSource1" GridLines="None" 
        Skin="Black" DataKeyNames="user_id" EditMode="PopUp" 
        AllowAutomaticUpdates="True" ShowGroupPanel="True" GroupPanel-ID = "RadAjaxLoadingPanel1">

        <MasterTableView EditMode="PopUp" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" DataKeyNames = "user_id">
            <Columns>
                <telerik:GridBoundColumn DataField="user_id" DataType="System.Int32" 
                    HeaderText="User ID" SortExpression="user_id" UniqueName="user_id">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="last_name" HeaderText="Last Name" 
                    SortExpression="last_name" UniqueName="last_name">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="first_name" HeaderText="First Name" 
                    SortExpression="first_name" UniqueName="first_name">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="role" 
                    HeaderText="Role" SortExpression="role" UniqueName="role">
                </telerik:GridBoundColumn>
            </Columns>
            <EditFormSettings InsertCaption="Add new item" CaptionFormatString="Edit User ID: {0}" 
                CaptionDataField="user_id" EditFormType="Template" PopUpSettings-Modal="True" PopUpSettings-ScrollBars="Auto">
                <FormTemplate>
                    <table id="Table1" cellspacing="1" cellpadding="1" width="250" border="0">
                            <tr>
                                <td>
                                    User ID:
                                </td>
                                <td>
                                    <asp:label id="Message1" runat="server" text='<%# Bind( "user_id" ) %>'/>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Last Name:
                                </td>
                                <td>
                                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind( "last_name" ) %>'>
                                    </asp:TextBox>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    First Name:
                                </td>
                                <td>
                                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind( "first_name") %>' TabIndex="1">
                                    </asp:TextBox>
                                </td>
                            </tr>

                            <tr>
                                <td>
                                    Role:
                                </td>
                                <td>
                                    <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind("role") %>'
                                        DataSource='<%# (new string() { "Admin", "Teacher", "Student"}) %>' TabIndex="7"
                                        AppendDataBoundItems="True">
                                        <asp:ListItem Selected="True" Text="Select" Value=""></asp:ListItem>
                                    </asp:DropDownList>
                                </td>
                            </tr>
                        </table>

                        <table style="width: 100%">
                            <tr>
                                <td align="right" colspan="2">
                                    <asp:Button ID="Button1" Text='<%# Iif (TypeOf Container is GridEditFormInsertItem, "Insert", "Update") %>'
                                        runat="server" CommandName='<%# Iif (TypeOf Container is GridEditFormInsertItem, "PerformInsert", "Update") %>'>
                                    </asp:Button>&nbsp;
                                    <asp:Button ID="Button2" Text="Cancel" runat="server" CausesValidation="False" CommandName="Cancel">
                                    </asp:Button>
                                </td>
                            </tr>
                        </table>
                    </FormTemplate>
                    <PopUpSettings ScrollBars="Auto" Modal="True"></PopUpSettings>
            </EditFormSettings>
        </MasterTableView>
        <Cli开发者_StackOverflow社区entSettings AllowDragToGroup="True">
            <Scrolling AllowScroll="True" UseStaticHeaders="True" />
            <ClientEvents OnRowDblClick="RowDblClick" />
        </ClientSettings>
</telerik:RadGrid>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ProLearnConnectionString %>" 
        SelectCommand="SELECT tblUser.user_id, tblUser.last_name, tblUser.first_name, tblRole.role FROM tblUser INNER JOIN tblRole ON tblUser.role_id = tblRole.role_id ORDER BY tblUser.user_id"
        OldValuesParameterFormatString="original_{0}" 
        UpdateCommand="UPDATE [tblUser] SET [last_name] = ?, [first_name] = ? WHERE [user_id] = ? ">
    <UpdateParameters>
        <asp:Parameter Name="last_name" Type="String" />
        <asp:Parameter Name="first_name" Type="String" />
        <asp:Parameter Name="original_user_id" Type="Int32" />
    </UpdateParameters>

</asp:SqlDataSource>

And this if my code in

TeacherRole.aspx.vb

Imports Telerik.Web.UI

Partial Class Admin_TeacherRole
    Inherits System.Web.UI.Page

    Private Sub RadGrid1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadGrid1.PreRender
        If Not IsPostBack Then
            For Each item As GridItem In RadGrid1.MasterTableView.Items
                If TypeOf item Is GridEditableItem Then
                    Dim editableItem As GridEditableItem = CType(item, GridDataItem)
                    editableItem.Edit = True
                End If
            Next

        End If
    End Sub
    Protected Sub RadGrid1_UpdateCommand(ByVal source As Object, ByVal e As GridCommandEventArgs)
        If e.CommandName = RadGrid.UpdateCommandName Then
            If TypeOf e.Item Is GridEditFormItem Then
                Dim item As GridEditFormItem = DirectCast(e.Item, GridEditFormItem)
                Dim id As Integer = Convert.ToInt32(item.GetDataKeyValue("user_id"))
                If id <> 0 Then
                    Dim txtlastname As TextBox = DirectCast(item.FindControl("TextBox1"), TextBox)
                    Dim txtfirstname As TextBox = DirectCast(item.FindControl("TextBox5"), TextBox)

                End If
            End If
        End If
    End Sub

End Class

Any advise and suggestion would be highly sought .. Have a nice day.. Thanks in Advance


If you're still have challenges with this problem, this example may be useful:

Automatic operations with SqlDataSource and RadGrid

The code sample provided in that demo shows you how to configure a RadGrid to work with automatic operations. It uses in-line edit forms, but the concepts should be similar even when using a custom EditForm. Hope that helps.


If your using code behind to update you need to turn off AllowAutomaticUpdates/Insert i.e. AllowAutomaticUpdates="False"

Regards, Patrick


It looks that you implemented automatic data editing with SqlDataSource properly, I do not see any weakness or wrong code. To troubleshoot the updates, use the pointers from this article, at least that is what I do when implementing editing with data source controls for the Telerik grid.

Dick

0

精彩评论

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

关注公众号