在ASP.NET GridView中显示数据

法罗克·穆阿迈德(Faroke Moahmed)

我想知道如何显示从服务器返回到GridView的JSON数据。
我已经反序列化了JSON数据并存储在一个变量中,但是无法在Grid中显示数据。
因此,请一个人帮我在Grid中显示数据。
我的密码:

protected void btn_ShowCust_Click(object sender, EventArgs e)
{
   try
       {
          string url = clsGuid.GetAPI() + "/service/service/student/search/" + txtstuname.Text.Trim();
          HttpWebRequest reque = (HttpWebRequest)HttpWebRequest.Create(url);
          reque.Method = "GET";
          reque.Accept = "application/json";
          reque.ContentType = "application/json";//application/x-www-form-urlencoded
          reque.UserAgent = "Java/1.6.0_22";
          reque.AuthenticationLevel =   System.Net.Security.AuthenticationLevel.MutualAuthRequested;
          reque.Credentials = new NetworkCredential("abcd", "123456");
          reque.PreAuthenticate = true;
          reque.KeepAlive = true;
          reque.Headers["Authorization"] = "Basic ERTGFEDCVTY=";


                HttpWebResponse response = (HttpWebResponse)reque.GetResponse();

          if (response.StatusCode == HttpStatusCode.OK)
            {
              Stream streamResponse = response.GetResponseStream();
              StreamReader streamRead = new StreamReader(streamResponse);

              using (var twitpicResponse = (HttpWebResponse)reque.GetResponse())
               {
                 using (var reader = new StreamReader(twitpicResponse.GetResponseStream()))
                  {
                     JavaScriptSerializer js = new JavaScriptSerializer();
                     var objText = reader.ReadToEnd();
                     List<RootObject> myojb = js.Deserialize<List<RootObject>>(objText);
                     DataTable dt = new DataTable();                                                                        
                     int countvalue = myojb.Count;                         

                     for (int i = 0; i < countvalue; i++)
                         {

                           //Need to display the data in GRIDVIEW...
                           //FirstName =dt.Rows.Add(myojb[i].firstName).ToString();
                           //LastName = dt.Rows.Add(myojb[i].lastName).ToString(); 

                           and so on...
                         }
                     gvRecipient.DataSource = dt;
                     gvRecipient.DataBind();
                  }
              }

          }
      }
      catch
      {
        Msg.InnerHtml = "<div class='msg-error'>We are facing some technical issues in processing your request. We will make it up quickly. Please try after some time.</div>";
      }

}

我的JSON数据

[{"firstName":"asdf","lastName":"qwer"...so on..}]

我的GRID

在此处输入图片说明

错误:

在此处输入图片说明

拉扎·拉巴尼(Raza Rabbani)

我认为您必须定义以下列:

dt.Columns.Add("FirstName", typeof (string));
dt.Columns.Add("LastName", typeof (string));

试试这个代码

protected void btn_ShowCust_Click(object sender, EventArgs e)
{
    try
    {
        string url = clsGuid.GetAPI() + "/service/service/student/search/" + txtstuname.Text.Trim();
        HttpWebRequest reque = (HttpWebRequest)HttpWebRequest.Create(url);
        reque.Method = "GET";
        reque.Accept = "application/json";
        reque.ContentType = "application/json";//application/x-www-form-urlencoded
        reque.UserAgent = "Java/1.6.0_22";
        reque.AuthenticationLevel =   System.Net.Security.AuthenticationLevel.MutualAuthRequested;
        reque.Credentials = new NetworkCredential("abcd", "123456");
        reque.PreAuthenticate = true;
        reque.KeepAlive = true;
        reque.Headers["Authorization"] = "Basic ERTGFEDCVTY=";


            HttpWebResponse response = (HttpWebResponse)reque.GetResponse();

        if (response.StatusCode == HttpStatusCode.OK)
        {
            Stream streamResponse = response.GetResponseStream();
            StreamReader streamRead = new StreamReader(streamResponse);

            using (var twitpicResponse = (HttpWebResponse)reque.GetResponse())
            {
                using (var reader = new StreamReader(twitpicResponse.GetResponseStream()))
                {
                    JavaScriptSerializer js = new JavaScriptSerializer();
                    var objText = reader.ReadToEnd();
                    List<RootObject> myojb = js.Deserialize<List<RootObject>>(objText);
                    DataTable dt = new DataTable();      
                    dt.Columns.Add("FirstName", typeof (string));
                    dt.Columns.Add("LastName", typeof (string));

                     int countvalue = myojb.Count;                         

                      for (int i = 0; i < countvalue; i++)
                        {

                            //Need to display the data in GRIDVIEW...
                            //FirstName =dt.Rows.Add(myojb[i].firstName).ToString();
                            //LastName = dt.Rows.Add(myojb[i].lastName).ToString(); 

                            and so on...
                        }
                      gvRecipient.DataSource = dt;
                      gvRecipient.DataBind();
                }
            }

        }
    }
    catch
    {
        Msg.InnerHtml = "<div class='msg-error'>We are facing some technical issues in processing your request. We will make it up quickly. Please try after some time.</div>";
    }

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

只选择特定的列并在ASP.NET中的gridview控件中显示这些列

来自分类Dev

ASP.NET将JSON数据读取到GridView中

来自分类Dev

ASP.NET GridView中的动态链接

来自分类Dev

在ASP.Net MVC中显示成功保存数据的消息

来自分类Dev

GridView中的ASP.NET代码

来自分类Dev

显示来自ASP.Net GridView中的链接的图像

来自分类Dev

在CodeBehind ASP NET中找不到GridView

来自分类Dev

在TextBox ASP.NET VB中显示来自GridView的选定数据

来自分类Dev

绑定数据库中ASP.NET Gridview中的标签字体大小

来自分类Dev

Gridview仅显示MySQL数据库中的1行-ASP.net C#

来自分类Dev

ASP.Net-Gridview中的Bootstrap Popover

来自分类Dev

ASP.NET Gridview不显示

来自分类Dev

ASP.NET将JSON数据读取到GridView中

来自分类Dev

在JavaScript的Asp.net标签中显示Gridview列总计

来自分类Dev

在asp.net中从GridView获取数据

来自分类Dev

无法在asp.net的GridView中显示数据

来自分类Dev

显示来自ASP.Net GridView中的链接的图像

来自分类Dev

C#asp.net在gridview中显示图像

来自分类Dev

在asp.net中的gridView中显示特定字段

来自分类Dev

在asp.net的GridView列中显示子XML元素

来自分类Dev

在CodeBehind ASP NET中找不到GridView

来自分类Dev

ASP.NET GridView无法在页面加载中显示

来自分类Dev

ASP.net Webform显示数据库中的信息

来自分类Dev

如何在ASP.Net C#中的GridView的EditMode中显示DropdownList?

来自分类Dev

在gridview单元格工具提示ASP.net/C#中显示列的名称

来自分类Dev

Gridview 表未显示在 asp.net webform 中

来自分类Dev

c#使用asp.net在gridview页脚中显示总列数

来自分类Dev

使用 SQLDataSource / Asp.Net 在 Gridview 中显示来自 Guid 的 ContactName

来自分类Dev

JQuery DataTables 未在 ASP.NET MVC 中显示数据

Related 相关文章

热门标签

归档