protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView2, "Select$" + e.Row.RowIndex);
e.Row.ToolTip = "Click to select this row.";
}
}
基本上,每次在^内的代码被触发时刷新页面时,我都试图使我的网格视图中的行可单击以呈现有关该行的信息的新页面。但由于某种原因,页面加载会在每一行触发。
现在,我在页面加载中尝试了!isPostBack,它的问题是它阻止了gridview页面的渲染> 2。我的确做到了一些if语句阻止了该问题,但是它需要两次单击才能触发该事件,所以我知道我做错了事,希望有人能提供帮助。如果有人可以帮助的话,只需包含我的aspx和c#代码,将不胜感激。
<asp:GridView ID="GridView2" CssClass="mydatagrid" PagerStyle-CssClass="Gridpager" HeaderStyle-CssClass="Gridheader" RowStyle-CssClass="Gridrows"
runat="server" AutoGenerateColumns="false" AllowPaging="true" OnRowDataBound="GridView2_RowDataBound" OnPageIndexChanging="GridView2_PageIndexChanging"
PageSize="13" width="100%" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hdnIndex" runat="server"
Value='<%# Eval("RecordID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Certificate" HeaderText="Number" />
<asp:BoundField DataField="Company_Name" HeaderText="Bank Name" />
<asp:BoundField DataField="Phone1" HeaderText="Phone" />
<asp:BoundField DataField="Address_Line_1" HeaderText="Street Address" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="State" HeaderText="State" />
</Columns>
<PagerSettings mode="NumericFirstLast" FirstPageText="First" PreviousPageText="Previous" NextPageText="Next" LastPageText="Last" />
</asp:GridView>
和Page_Load
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConnectionStringGlobal;
conn.Open();
string query = "SELECT * FROM Clients";
SqlCommand cmd = new SqlCommand(query, conn);
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
conn.Close();
GridView2.DataSource = dt;
GridView2.DataBind();
}
最后一个分页代码:
protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView2.PageIndex = e.NewPageIndex;
GridView2.DataBind();
}
这是单击行的代码
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Attaching one onclick event for the entire row, so that it will
// fire SelectedIndexChanged, while we click anywhere on the row.
e.Row.Attributes["onclick"] =
ClientScript.GetPostBackClientHyperlink(this.GridView2, "Select$" + e.Row.RowIndex);
}
Console.WriteLine("");
}
答案 0 :(得分:0)
每次页面刷新或出现GridView
时都会加载PostBack
,因此只有在第一次加载页面时才可以加载数据,而且,我们正在保存查询结果生成一个Session
变量,供以后在PageIndexChanged
事件中使用:
protected void Page_Load(object sender, EventArgs e)
{
if( !Page.IsPostBack )
{
LoadData();
}
}
protected void LoadData()
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConnectionStringGlobal;
conn.Open();
string query = "SELECT * FROM Clients";
SqlCommand cmd = new SqlCommand(query, conn);
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
conn.Close();
GridView2.DataSource = dt;
GridView2.DataBind();
// Save into a session variable for later load on pagination.
Session.Add("tmp_dt",dt);
}
现在我们还需要在GridView2_PageIndexChanging
事件上加载数据,以便分页工作:
protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
// Load the data previously saved into a session variable.
// You might want to read the database instead.
DataTable dt = (DataTable) Session["tmp_dt"];
this.GridView2.DataSource = dt;
// Change the page index before databind. This differs from the LoadData() method.
GridView2.PageIndex = e.NewPageIndex;
GridView2.DataBind();
}
然后在您的ASPX
代码处,将AllowPaging=True
属性添加到GridView2
控件中。