我找到了答案,我的問題上Code Project:
我現在在我的GridView控件使用GridView
<asp:TemplateField>
<ItemTemplate>
<asp:GridView ID="SubGridView"
因爲在GridView中延伸的,在GridView會當我點擊加號顯示出來(見鏈接)
在頁面加載我執行以下操作:
protected void Page_Load(object sender, EventArgs e)
{
GridView1.RowCreated += new GridViewRowEventHandler(GridView1_RowCreated);
GridView1.DataSource = dc.GetLogEntriesWithUsername();
GridView1.DataBind();
我在這個gridview上已經有了一個DataBound和一個Selected Index Changed事件。
該行創建的事件我執行以下操作:
void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView SubGridView = e.Row.FindControl("SubGridView") as GridView;
List<GetLogEntriesWithUsernameByParentIdResult> subLogEntries = dc.GetLogEntriesWithUsernameByParentId(((GetLogEntriesWithUsernameResult)e.Row.DataItem).logentry_id).ToList();
if (subLogEntries.Count > 0)
{
SubGridView.DataSource = subLogEntries;
SubGridView.DataBind();
(e.Row as ExtGridViewRow).ShowExpand = SubGridView.Rows.Count > 0;
}
}
}
在subgridview我也有一個數據綁定和SelectedIndex的Changed事件。這現在起作用了!
我用這個數據綁定事件:
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{ // only apply changes if its DataRow
GridView sendingGridView = (GridView)sender;
if (e.Row.RowType == DataControlRowType.DataRow)
{
// when mouse is over the row, save original color to new attribute, and change it to highlight yellow color
e.Row.Attributes.Add("onmouseover",
"this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#C0C0C0';this.style.cursor='pointer';");
// when mouse leaves the row, change the bg color to its original value
e.Row.Attributes.Add("onmouseout",
"this.style.backgroundColor=this.originalstyle;this.style.cursor='cursor'");
//e.Row.Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex));
e.Row.Cells[1].Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex);
e.Row.Cells[2].Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex);
e.Row.Cells[3].Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex);
e.Row.Cells[4].Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex);
e.Row.Cells[5].Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex);
和所選擇的指數變化情況:
protected void GridView_SelectedIndexChanged(object sender, EventArgs e)
{
GridView sendingGridView = (GridView)sender;
ViewDetails(Convert.ToInt32(sendingGridView.SelectedDataKey["logentry_id"].ToString()));
}
的ViewDetails功能顯示在不同的DIV選擇logentry的細節。 現在我忙於最後一步,那就是不斷顯示數據,因爲它是在我點擊一行之前。
感謝您的幫助,但是這是解決我的問題。
我想您所suscribing事件,在第二回傳不再存在subgridview;這將是該事件不加 – 2010-01-07 15:58:13
這聽起來很合乎邏輯的原因,但也應該有一種方法來選擇此subgridview不應該嗎? – Michael 2010-01-07 16:01:06
呃...當我一直在使用動態控件時,問題在於它們不再存在,所以我的解決方法是檢查Request對象以查看實際來自客戶端的內容,並在重新創建子網格視圖時考慮這些值 – 2010-01-07 16:03:46