如何使用 RadControls for ASP.NET AJAX 将记录插入数据库
我在 asp.net 中开发了一个小型应用程序,我正在使用 RadControls for ASP.NET AJAX telrik 当我尝试插入一条记录时,它不会插入到数据库中,但它可以工作选择命令,任何人都可以帮助我,请在这里我发布我的代码 ...
<%@ Register TagPrefix="sds" Namespace="Telerik.Web.SessionDS" %> <%@ Register TagPrefix="Telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %> <%--<%@ 注册 TagPrefix="qsf" Namespace="Telerik.QuickStart" %>--%> <%---%>
<script type="text/javascript">
// <![CDATA[
//On insert and update buttons click temporarily disables ajax to perform upload actions
function conditionalPostback(sender, eventArgs) {
var theRegexp = new RegExp("\.UpdateButton$|\.PerformInsertButton$", "ig");
if (eventArgs.get_eventTarget().match(theRegexp)) {
var upload = $find(window['UploadId']);
//AJAX is disabled only if file is selected for upload
if (upload.getFileInputs()[0].value != "") {
eventArgs.set_enableAjax(false);
}
}
}
function validateRadUpload(source, e) {
e.IsValid = false;
var upload = $find(source.parentNode.getElementsByTagName('div')[0].id);
var inputs = upload.getFileInputs();
for (var i = 0; i < inputs.length; i++) {
//check for empty string or invalid extension
if (inputs[i].value != "" && upload.isExtensionValid(inputs[i].value)) {
e.IsValid = true;
break;
}
}
}
// ]]>
</script>
</telerik:RadCodeBlock>
<telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" ClientEvents-OnRequestStart="conditionalPostback">
<telerik:RadProgressManager ID="RadProgressManager1" runat="server" />
<telerik:RadProgressArea ID="RadProgressArea1" runat="server" />
<telerik:RadGrid runat="server" ID="RadGrid1" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" Width="97%" DataSourceID="SqlDataSource1" AllowAutomaticInserts="true"
AllowAutomaticUpdates="true" AllowAutomaticDeletes="true" ShowStatusBar="True"
GridLines="None" OnItemDataBound="RadGrid1_ItemDataBound" OnItemCreated="RadGrid1_ItemCreated" PageSize="3">
<PagerStyle Mode="NumericPages" AlwaysVisible="true" />
<MasterTableView Width="100%" CommandItemDisplay="Top" DataKeyNames="ID" DataSourceID="SqlDataSource1">
<Columns>
<telerik:GridEditCommandColumn ButtonType="ImageButton">
<HeaderStyle Width="3%" />
</telerik:GridEditCommandColumn>
<telerik:GridTemplateColumn HeaderText="Image Name" UniqueName="ImageName" SortExpression="Name">
<ItemTemplate>
<asp:Label runat="server" ID="lblName" Text='<%# Eval("Name") %>' />
</ItemTemplate>
<EditItemTemplate>
<telerik:RadTextBox runat="server" Width="200px" ID="txbName" Text='<%# Bind("Name") %>' />
<asp:RequiredFieldValidator ID="Requiredfieldvalidator1" runat="server" ControlToValidate="txbName" ErrorMessage="Please, enter a name!" Display="Dynamic" SetFocusOnError="true" />
</EditItemTemplate>
<HeaderStyle Width="30%" />
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="Description" UniqueName="Description" DataField="Description">
<ItemTemplate>
<asp:Label ID="lblDescription" runat="server" Text='<%# Eval("Description")%>' />
</ItemTemplate>
<EditItemTemplate>
<telerik:RadTextBox ID="txbDescription" Width="300px" runat="server" TextMode="MultiLine"
Text='<%# Bind("Description") %>' Height="150px" />
</EditItemTemplate>
<ItemStyle VerticalAlign="Top" />
</telerik:GridTemplateColumn>
<telerik:GridBinaryImageColumn DataField="Data" HeaderText="Image" UniqueName="Upload" ImageAlign="NotSet"
ImageHeight="80px" ImageWidth="80px" ResizeMode="Fit" DataAlternateTextField="Description"
DataAlternateTextFormatString="Image of {0}">
<HeaderStyle Width="10%" />
</telerik:GridBinaryImageColumn>
<telerik:GridButtonColumn Text="Delete" CommandName="Delete" ButtonType="ImageButton">
<HeaderStyle Width="2%" />
</telerik:GridButtonColumn>
</Columns>
<EditFormSettings>
<EditColumn ButtonType="ImageButton" />
</EditFormSettings>
</MasterTableView>
</telerik:RadGrid>
</telerik:RadAjaxPanel>
<sds:SessionDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [Mybusinessapp_Pictures]" InsertCommand="INSERT INTO [Mybusinessapp_Pictures] ([Name], [Description], [Data]) VALUES (@Name, @Description, @Data)"
UpdateCommand="UPDATE [Mybusinessapp_Pictures] SET [Name] = @Name, [Description] = @Description, [Data] = @Data WHERE [ID] = @ID"
DeleteCommand="DELETE FROM [Mybusinessapp_Pictures] WHERE [ID] = @ID" PrimaryKeyFields="ID" SessionKey="UploadGridCS">
<InsertParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Description" Type="String" />
<asp:Parameter Name="Data" DbType="Binary" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Description" Type="String" />
<asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
<%-- <asp:Parameter Name="Data" DbType="Binary" />--%>
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
</sds:SessionDataSource>
<%---%>
i have dovelop a small application in asp.net in this i am using RadControls for ASP.NET AJAX telrik when i try to insert a record it doesn't insert to database but it working select command anybody can help me please here i post my code ...
<%@ Register TagPrefix="sds" Namespace="Telerik.Web.SessionDS" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<%--<%@ Register TagPrefix="qsf" Namespace="Telerik.QuickStart" %>--%>
<%-- --%>
<script type="text/javascript">
// <![CDATA[
//On insert and update buttons click temporarily disables ajax to perform upload actions
function conditionalPostback(sender, eventArgs) {
var theRegexp = new RegExp("\.UpdateButton$|\.PerformInsertButton$", "ig");
if (eventArgs.get_eventTarget().match(theRegexp)) {
var upload = $find(window['UploadId']);
//AJAX is disabled only if file is selected for upload
if (upload.getFileInputs()[0].value != "") {
eventArgs.set_enableAjax(false);
}
}
}
function validateRadUpload(source, e) {
e.IsValid = false;
var upload = $find(source.parentNode.getElementsByTagName('div')[0].id);
var inputs = upload.getFileInputs();
for (var i = 0; i < inputs.length; i++) {
//check for empty string or invalid extension
if (inputs[i].value != "" && upload.isExtensionValid(inputs[i].value)) {
e.IsValid = true;
break;
}
}
}
// ]]>
</script>
</telerik:RadCodeBlock>
<telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" ClientEvents-OnRequestStart="conditionalPostback">
<telerik:RadProgressManager ID="RadProgressManager1" runat="server" />
<telerik:RadProgressArea ID="RadProgressArea1" runat="server" />
<telerik:RadGrid runat="server" ID="RadGrid1" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" Width="97%" DataSourceID="SqlDataSource1" AllowAutomaticInserts="true"
AllowAutomaticUpdates="true" AllowAutomaticDeletes="true" ShowStatusBar="True"
GridLines="None" OnItemDataBound="RadGrid1_ItemDataBound" OnItemCreated="RadGrid1_ItemCreated" PageSize="3">
<PagerStyle Mode="NumericPages" AlwaysVisible="true" />
<MasterTableView Width="100%" CommandItemDisplay="Top" DataKeyNames="ID" DataSourceID="SqlDataSource1">
<Columns>
<telerik:GridEditCommandColumn ButtonType="ImageButton">
<HeaderStyle Width="3%" />
</telerik:GridEditCommandColumn>
<telerik:GridTemplateColumn HeaderText="Image Name" UniqueName="ImageName" SortExpression="Name">
<ItemTemplate>
<asp:Label runat="server" ID="lblName" Text='<%# Eval("Name") %>' />
</ItemTemplate>
<EditItemTemplate>
<telerik:RadTextBox runat="server" Width="200px" ID="txbName" Text='<%# Bind("Name") %>' />
<asp:RequiredFieldValidator ID="Requiredfieldvalidator1" runat="server" ControlToValidate="txbName" ErrorMessage="Please, enter a name!" Display="Dynamic" SetFocusOnError="true" />
</EditItemTemplate>
<HeaderStyle Width="30%" />
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="Description" UniqueName="Description" DataField="Description">
<ItemTemplate>
<asp:Label ID="lblDescription" runat="server" Text='<%# Eval("Description")%>' />
</ItemTemplate>
<EditItemTemplate>
<telerik:RadTextBox ID="txbDescription" Width="300px" runat="server" TextMode="MultiLine"
Text='<%# Bind("Description") %>' Height="150px" />
</EditItemTemplate>
<ItemStyle VerticalAlign="Top" />
</telerik:GridTemplateColumn>
<telerik:GridBinaryImageColumn DataField="Data" HeaderText="Image" UniqueName="Upload" ImageAlign="NotSet"
ImageHeight="80px" ImageWidth="80px" ResizeMode="Fit" DataAlternateTextField="Description"
DataAlternateTextFormatString="Image of {0}">
<HeaderStyle Width="10%" />
</telerik:GridBinaryImageColumn>
<telerik:GridButtonColumn Text="Delete" CommandName="Delete" ButtonType="ImageButton">
<HeaderStyle Width="2%" />
</telerik:GridButtonColumn>
</Columns>
<EditFormSettings>
<EditColumn ButtonType="ImageButton" />
</EditFormSettings>
</MasterTableView>
</telerik:RadGrid>
</telerik:RadAjaxPanel>
<sds:SessionDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [Mybusinessapp_Pictures]" InsertCommand="INSERT INTO [Mybusinessapp_Pictures] ([Name], [Description], [Data]) VALUES (@Name, @Description, @Data)"
UpdateCommand="UPDATE [Mybusinessapp_Pictures] SET [Name] = @Name, [Description] = @Description, [Data] = @Data WHERE [ID] = @ID"
DeleteCommand="DELETE FROM [Mybusinessapp_Pictures] WHERE [ID] = @ID" PrimaryKeyFields="ID" SessionKey="UploadGridCS">
<InsertParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Description" Type="String" />
<asp:Parameter Name="Data" DbType="Binary" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Description" Type="String" />
<asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
<%-- <asp:Parameter Name="Data" DbType="Binary" />--%>
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
</sds:SessionDataSource>
<%-- --%>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 SessionDataSource InsertCommand 更改为:
InsertCommand="INSERT INTO [Mybusinessapp_Pictures] ([Name], [Description], [Data]) VALUES (?, ?, ?)"
Change your SessionDataSource InsertCommand into :
InsertCommand="INSERT INTO [Mybusinessapp_Pictures] ([Name], [Description], [Data]) VALUES (?, ?, ?)"