我有一個gridview'gvAccounts'和一列'Entitlements'(等等)。每個Entitlements單元格包含另一個gridview'gvEntitlements',每個帳戶的權利列表(gvAccounts中的每一行都是一個帳戶)。使用RowEditing事件來更新GridView EditTemplate中的CheckBoxList,基於ItemTemplate中的值
在Entitlements單元格的EditTemplate中,我有一個CheckBoxList'cblEntitlements',它充滿了所有可用的權利。
當我點擊編輯時,我希望cblEntitlements只能檢查出現在gvEntitlements中的那些權利。我在RowEditing事件中嘗試了一大堆事情,並且沒有任何工作(在行上使用.FindControl時出現大量空引用錯誤),所以我想我會問問做這件事的最好方法是什麼,而不是粘貼我失敗的嘗試。
謝謝!
標記:
<asp:GridView
ID="gvAccounts"
runat="server"
DataSourceID="AccountsObjectDataSource"
AllowSorting="True"
AutoGenerateColumns="False"
DataKeyNames="CorpID,AppKey"
Width="100%"
OnRowDataBound="gvAccounts_RowDataBound"
OnRowEditing="gvAccounts_RowEditing"
EnableModelValidation="True" >
<Columns>
<asp:TemplateField
HeaderText="Entitlements"
SortExpression="Entitlements">
<ItemTemplate>
<asp:GridView
ID="gvEntitlements"
DataKeyNames="EntitlementID"
runat="server"
ShowHeader="False"
BorderStyle="None"
EmptyDataText="common"
GridLines="None"
AutoGenerateColumns="False">
<Columns>
<asp:BoundField
DataField="EntitlementID"
Visible="false" />
<asp:BoundField
DataField="Entitlement"/>
</Columns>
</asp:GridView>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBoxList
ID="cblEntitlements"
runat="server"
DataSourceID="cblObjectDataSource"
DataTextField="Entitlement"
DataValueField="EntitlementID"
RepeatColumns="2"
RepeatDirection="Horizontal">
</asp:CheckBoxList>
<asp:ObjectDataSource
ID="cblObjectDataSource"
runat="server"
SelectMethod="GetAppEntitlements"
TypeName="CMRPWebApp.Business.CMRPControllerApps">
<SelectParameters>
<asp:ControlParameter
ControlID="DropDownList1"
Name="AppKey"
PropertyName="SelectedValue"
Type="Int32"/>
</SelectParameters>
</asp:ObjectDataSource>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ObjectDataSource
ID="AccountsObjectDataSource"
runat="server"
SelectMethod="GetUsersForApp"
TypeName="CMRPWebApp.Business.CMRPControllerApps"
<SelectParameters>
<asp:ControlParameter
ControlID="DropDownList1"
Name="AppKey"
PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
後臺代碼:
protected void gvAccounts_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//for each row, run a query for that appkey + corp ID to retrieve the entitlements for that account.
System.Collections.Specialized.IOrderedDictionary datakeyNames = gvAccountsApp.DataKeys[e.Row.RowIndex].Values;
string corpID = datakeyNames["CorpID"].ToString();
int appKey = Convert.ToInt32(datakeyNames["AppKey"]);
if ((e.Row.FindControl("gvEntitlements") as GridView) != null)
{
GridView gv = e.Row.FindControl("gvEntitlements") as GridView;
gv.DataSource = CMRPControllerApps.GetAccountEntitlements(corpID, appKey);
gv.DataBind();
}
}
}
protected void gvAccounts_RowEditing(object sender, GridViewEditEventArgs e)
{
// ?
}
我認爲我們需要更多的信息來幫助。就目前來看,這個問題本質上是過於籠統的。你可以發佈你的兩個GridView的標記嗎?另外,請爲兩個GridView提供DataBinding代碼。 – 2013-04-30 18:01:39
好的 - 添加了代碼。我試圖削減脂肪,只留下相關的代碼,所以希望沒有丟失。 – 2013-04-30 18:53:48