![](/img/trans.png)
[英]how to fill selected values into Checkboxlist from gridview edit options in asp.net using c#?
[英]How to fill an inner gridview based on outer gridview selection (ASP.NET/C#)
我有2個GridView,InnerGridView嵌套在我的OuterGridView的TemplateField中。 每個GridView都有一個ObjectDataSource(ODS)。 我希望InnerGridView顯示對OuterGridView中列出的GroupName唯一的數據。 我已經使用Google搜索了幾個星期,看到基於RowDataBound和ODS Selecting事件的各種想法。
我不認為RowDataBound是答案,因為當為OuterGridView調用RowDataBound時,已經調用了InnerGridView ODS的Selecting事件。
因此,我需要向InnerGridView的ODS發送參數:
protected void ProductDataSource_Selecting( object sender, ObjectDataSourceSelectingEventArgs e ) {
e.InputParameters["productGroup"] = <here I need to access the GroupName from the OuterGridView>;
}
注意:此方法已分配給InnerGridView ODS的Selecting事件。
我的問題是:在處理InnerGridView時,如何從OuterGridView ...訪問GroupName。 這是不起作用的示例:OuterGridView.SelectedRow.FindControl(“ GroupName”).ToString();
我聽說可以通過搜索某一層次從Selecting事件內部找到任何控件。 但是我不知道如何使用“ e”或“ sender”參數訪問該層次結構。 如果我能找到有關各種對象和控件的說明...以及它們在何處具有有效范圍以及如何訪問它們...這將很有幫助。
非常感謝,保羅
如果將發送方對象強制轉換為gridview對象,則使用SelectedRow,如此處所示,並獲取組名?
如果要相對於外部GridView的GroupName綁定內部GridView,請執行以下步驟
1)if(!IsPostBack)綁定外部GridView。 2)在外部GridViews RowDataBound事件中,通過檢查以下條件是否找到內部GridView:(e.Row.RowType == DataControlRowType.DataRow){DataRowView drv =((DataRowView)e.Row.DataItem); GridView InnerGridView =(GridView)e.Row.FindControl(“ InnerGridView”); 如果(InnerGridView!= null){字符串GroupName = drv [“ GroupName”]; //以獲取GropName。 InnerGridView.DataSource =“查詢WRT組名”; InnerGridView.DataBind(); }}
希望以下幫助
ASPX頁面:
<asp:ObjectDataSource ID="ODS1" runat="server"></asp:ObjectDataSource>
<asp:GridView ID="GV1" runat="server" DataKeyNames="productGroup"
onrowdatabound="GV1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ObjectDataSource ID="ODS2" runat="server">
</asp:ObjectDataSource>
<asp:GridView ID="GV2" runat="server">
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
背后的代碼:
protected void GV1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
string productgroup = ((GridView)sender).DataKeys[e.Row.RowIndex]["productGroup"].ToString();
ObjectDataSource objDS = (ObjectDataSource)e.Row.FindControl("ODS2");
objDS.SelectParameters["productGroup"].DefaultValue = productgroup;
}
}
注意使用數據鍵保存產品組詳細信息。
顯然,您將需要配置對象數據源,並根據需要將它們連接到gridviews。
本文改編自本文,概述了使用SQL數據源的嵌套gridview,但是頁面生命周期應該相同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.