2012-06-11 103 views
1

以下是我如何填充GridView控件。我是從代碼隱藏,而不是從.aspx前端執行此操作。下面是什麼,我有一個非常簡化版本:RowDataBound事件中的一行與Gridview的分頁機制衝突

private void UpdateGridView() 
{ 
    DataTable temptable = new DataTable(); 
    DataColumn idcol = new DataColumn(); 
    DataColumn titlecol = new DataColumn(); 
    idcol.ColumnName = "ID"; 
    titlecol.ColumnName = "Title"; 
    temptable.Columns.Add(idcol); 
    temptable.Columns.Add(titlecol); 

...(get data from the database, store it as variable "x")... 

    DataRow tempdr; 
    tempdr[idcol] = x.ID; 
    tempdr[titlecol] = x.Title; 

    temptable.Rows.Add(tempdr); 

    GridView1.DataSource = temptable; 
    GridView1.DataBind(); 
} 

要處理分頁GridView的「AllowPaging」設置爲true,和我有以下事件處理程序:

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) 
{ 
    GridView1.PageIndex = e.NewPageIndex; 
    UpdateGridView(); 
} 

而這個偉大工程!

不過,我也有RowDataBound事件處理程序:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    e.Row.Cells[0].Visible = false; //hide the ID 

    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';"; 
     e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';"; 
     e.Row.Attributes["onclick"] = "location.href='newsindex.aspx?NewsArticleID=" + e.Row.Cells[0].Text + "'"; 
    } 
} 

我在這裏的目標是讓行本身是點擊,並導致帶有等於該行的ID查詢字符串的另一頁。我需要ID列中的值,以便在創建行時可以訪問它,以便我可以將ID添加到鏈接的QueryString中。但我不希望ID列可見,所以我在行中添加了:e.Row.Cells[0].Visible = false;這樣做會破壞分頁功能。頁碼不再顯示。如果我註釋掉這一行,它就可以工作,但ID在GridView中可見。

1)爲什麼? 2)我可以做些什麼來獲得相同的功能,但可能的變化最少?

回答

0

經過多次試驗和錯誤,我找到了答案。這似乎是一個非常罕見的問題,因此互聯網上沒有太多關於它的地方,因爲條件非常具體。我讀過的大部分內容都是關於如何使用DataKeyNames來代替將ID放在GridView中作爲常規列的。

我相信這可能會奏效,但我需要儘快解決,因爲我已經浪費了大量的時間與此搏鬥。

的解決之道在於GridView的RowDataBound事件:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header) 
    { 
     e.Row.Cells[0].Visible = false; //hide the ID 
     e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';"; 
     e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';"; 
     e.Row.Attributes["onclick"] = "location.href='newsindex.aspx?NewsArticleID=" + e.Row.Cells[0].Text + "'"; 
    } 
} 

很顯然,如果你使用e.Row.Cells[0].Visible = false;用,因爲我曾在我原來的問題沒有條件,它在某種程度上解釋,作爲一個標誌,使尋呼機無形的好。解決方法是在if區塊中包含e.Row.Cells[0].Visible = false;行,該行僅表示僅使DataRows的第一個單元格和標題(而不是其他元素,如尋呼機)不可見。

編輯:我只是意識到,如果你使用GridView排序,上面會搞砸你的排序。爲了保持排序啓用,並顯示尋呼機,並隱藏第一列,這沒有竅門:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header) 
    { 
     e.Row.Cells[0].Visible = false; //hide the ID 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';"; 
      e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';"; 
      e.Row.Attributes["onclick"] = "location.href='newsindex.aspx?NewsArticleID=" + e.Row.Cells[0].Text + "'"; 
     } 
    } 
} 
相關問題