即使没有错误,我也无法使用数据显示gridview。为什么? [英] I couldn't display gridview with data even there is no error. Why?
本文介绍了即使没有错误,我也无法使用数据显示gridview。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在一个带有SQLSERVER2008的webform中尝试了一个非常基本的数据绑定..所有东西似乎都很完美(没有错误),但是当它运行时仍然无法在浏览器上显示它...我什么也看不见jus一个空白的页面。
有什么不对的?
-i lookupon db连接它的罚款
I tried a very basic databinding in a webform with SQLSERVER2008..everything seems to be perfect(NO ERRORS),but still couldn't display it on browser while it runs..i see nothing jus a blank page.
What could be wrong?
-i lookedupon db connection its fine
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con =new SqlConnection ("data source=.; database=sample ; integrated security=SSPI; ");
SqlCommand cmd = new SqlCommand("Select * from Login_Info", con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
GridView1.DataSource = rdr;
GridView1.DataBind();
con.Close();
}
}
推荐答案
你绑定GridView
在同一GridView
的SelectedIndexChanged
事件中。由于网格没有任何数据,该事件永远不会触发。
尝试从Page_Load $ c $绑定网格c>事件而不是。你还想把它包装在
if(!IsPostBack){...}
块中。
You're binding theGridView
in theSelectedIndexChanged
event for the sameGridView
. Since the grid doesn't have any data, that event will never fire.
Try binding the grid from thePage_Load
event instead. You'll also want to wrap it in anif (!IsPostBack) { ... }
block.
using System;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindTheGrid();
}
}
private void BindTheGrid()
{
using (SqlConnection con = new SqlConnection ("data source=.; database=sample ; integrated security=SSPI; "))
using (SqlCommand cmd = new SqlCommand("Select * from Login_Info", con))
{
con.Open();
using (SqlDataReader rdr = cmd.ExecuteReader())
{
GridView1.DataSource = rdr;
GridView1.DataBind();
}
}
}
}
这篇关于即使没有错误,我也无法使用数据显示gridview。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文