![](/img/trans.png)
[英]How can I use the selected rows in GridView as a source for a GridView in another page?
[英]GridView rows all selected when page is loaded
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,它的問題是它阻止了呈現> 2的gridview頁面。 我的確做到了一些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("");
}
每次頁面刷新或有PostBack
加載GridView
因此僅在第一次加載頁面時才可以加載數據,而且,我們還將查詢結果保存到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
控件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.