如何使用jquery和c#将数据库值显示到html表中 [英] how to display database value into a html table using jquery and c#

查看:323
本文介绍了如何使用jquery和c#将数据库值显示到html表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者我想使用jquery和c#将数据库值显示到html表中。条件是当我提交搜索按钮时,在页面上的另一个aspx表单上显示所有值

am beginner i wanted to display database value into a html table using jquery and c#.the condition is when i submit a search button the all value display on another aspx form on page load

推荐答案

您可以使用ajax post来执行此操作。您必须创建一个Web方法,并且该Web方法应该带来数据库中的值,您可以使用jquery来调用该Web方法,并且在该Web方法中,您可以使用字符串构建器构建html表,并使用jquery绑定您的div内的桌子。



严格使用此代码



C#代码



You can use ajax post for doing this. You have to create a web method and that web method should bring the value from database and you can use jquery to call that web method and in that web method you can construct your html table by using string builder and by using jquery you can bind your table inside a div.

Use this code carrefully

C# Code

[WebMethod]
        public static string GetServices(string entry_id)
        {
            string HtmlData = string.Empty;
            try
            {
                HtmlData = GetString();
            }
            catch (Exception ex)
            {
                /////
            }
            return HtmlData;
        }

        private static string GetString()
        {
            //write the code for bring value from database

            DataTable table = new DataTable();//bring all your value in to this datatable
            DataRowCollection drows = table.Rows;
            StringBuilder HTML = new StringBuilder();
            HTML.AppendLine("<table>");
            foreach (DataRow dr in drows)
            {
                HTML.AppendLine("<tr>");
                HTML.AppendFormat("<td>{0}</td>\n", dr["FieldName"]);
                HTML.AppendLine("</tr>");

            }
            HTML.AppendLine("</table>");
            HTML.AppendLine("</div>");
            return HTML.ToString();
        }





Jquery



在这里导入jquery库



< script type =text / javascript>



Jquery

Import the jquery library here

<script type="text/javascript">


(#button)。 click(function(){

storeInfo();

});



function storeInfo(){

var elem_id = elem.attr('id');

var page_url ='Pagename.aspx / GetServices';
("#button").click(function(){
storeInfo();
});

function storeInfo() {
var elem_id = elem.attr('id');
var page_url = 'Pagename.aspx/GetServices';


.ajax({

类型:'POST',

url:page_url,

数据:,

contentType:'Application / json; charset = utf-8',

dataType:'json',

成功:功能(详情)

{
.ajax({
type: 'POST',
url: page_url,
data: "",
contentType: 'Application/json; charset=utf-8',
dataType: 'json',
success: function (details)
{


这篇关于如何使用jquery和c#将数据库值显示到html表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆