RadGrid Telerik 中更新命令背后的代码(帮助)
大家好,
我是 asp.net 新手,现在正在使用 RadControls for Asp.net Ajax。我尝试遵循 Asp.Net GridDemo - 插入/更新/删除,它似乎对我来说工作得不太好,所以我做了一些更改,结果在某种程度上做得很好,但更新按钮仍然不起作用...
我有这 2 个表 tblUser 和 tblRole
在我的 Radgrid 中我只想显示我的 tblUser< 中的 user_id、last_name 和 first_name /strong> 和 role 来自我的 tblRole
以及我的编辑表单 唯一可编辑的区域是姓氏、名字和角色,但是当我点击更新按钮时它不起作用..-_-,
我已经对设计没问题了,唯一的问题是我的更新按钮不起作用..
这是我的代码
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>
<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>
<ClientSettings 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>
如果我的代码在
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
中,我们将非常寻求任何建议和建议..祝你有美好的一天..提前致谢
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>
<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>
<ClientSettings 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用代码隐藏来更新,您需要关闭AllowAutomaticUpdates/Insert
即AllowAutomaticUpdates =“False”
问候,
帕特里克
If your using code behind to update you need to turn off AllowAutomaticUpdates/Insert
i.e. AllowAutomaticUpdates="False"
Regards,
Patrick
看来您使用 SqlDataSource 正确实现了自动数据编辑,我没有看到任何弱点或错误的代码。要对更新进行故障排除,请至少使用本文中的指针这就是我在使用 Telerik 网格的数据源控件实现编辑时所做的事情。
迪克
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
如果您仍然遇到此问题,此示例可能有用:
使用 SqlDataSource 和 RadGrid 自动操作
该演示中提供的代码示例向您展示了如何配置 RadGrid 以使用自动操作。它使用内联编辑表单,但即使使用自定义 EditForm,概念也应该相似。希望有帮助。
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.