我有一个 gridview 'gvAccounts' 列 'Entitlements' (以及其他)。每个权利单元格包含另一个网格视图“gvEntitlements”,其中包含每个帐户的权利列表(gvAccounts 中的每一行都是一个帐户)。
在 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)
{
// ?
}