请帮我写一个C# webforms前端使用bootstrap table加ajax调用一般处理程序,从Oracle数据库获取数据,显示一个包含图片的商品列表
时间: 2024-03-05 14:52:17 浏览: 125
好的,我理解了您的要求。以下是一个C# webforms前端使用bootstrap table加ajax调用一般处理程序,从Oracle数据库获取数据,显示一个包含图片的商品列表的示例代码:
前端HTML代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>商品列表</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href="https://cdn.bootcss.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.bootcss.com/bootstrap-table/1.15.3/bootstrap-table.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1>商品列表</h1>
<table id="table" data-toggle="table" data-url="Handler.ashx" data-pagination="true" data-page-size="10" data-search="true" data-height="600">
<thead>
<tr>
<th data-field="ID">商品编号</th>
<th data-field="Name">商品名称</th>
<th data-field="Price">商品价格</th>
<th data-field="Picture">商品图片</th>
</tr>
</thead>
</table>
</div>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap-table/1.15.3/bootstrap-table.min.js"></script>
</body>
</html>
```
前端JavaScript代码:
```javascript
$(document).ready(function () {
$('#table').bootstrapTable({
columns: [{
field: 'ID',
title: '商品编号'
}, {
field: 'Name',
title: '商品名称'
}, {
field: 'Price',
title: '商品价格'
}, {
field: 'Picture',
title: '商品图片',
formatter: function (value, row, index) {
return '<img src="' + value + '" width="100" height="100" />';
}
}]
});
});
```
后端C#代码:
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Data;
using System.Data.OracleClient;
using System.Configuration;
namespace BootstrapTableDemo
{
/// <summary>
/// Handler 的摘要说明
/// </summary>
public class Handler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string json = GetJson();
context.Response.Write(json);
}
public bool IsReusable
{
get
{
return false;
}
}
public string GetJson()
{
string connectionString = ConfigurationManager.ConnectionStrings["OracleConnectionString"].ConnectionString;
OracleConnection connection = new OracleConnection(connectionString);
OracleCommand command = new OracleCommand();
command.Connection = connection;
command.CommandText = "SELECT ID, Name, Price, Picture FROM Products";
OracleDataAdapter adapter = new OracleDataAdapter(command);
DataTable dt = new DataTable();
adapter.Fill(dt);
connection.Close();
List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
foreach (DataRow dr in dt.Rows)
{
Dictionary<string, object> dict = new Dictionary<string, object>();
foreach (DataColumn dc in dt.Columns)
{
dict.Add(dc.ColumnName, dr[dc]);
}
list.Add(dict);
}
JavaScriptSerializer serializer = new JavaScriptSerializer();
string json = serializer.Serialize(list);
return json;
}
}
}
```
其中,OracleConnectionString是连接Oracle数据库的连接字符串。您需要将其替换为您自己的连接字符串。此外,您还需要将数据库表名、列名和图片路径替换为您自己的实际情况。
阅读全文