C# UserControl中未触发Gridview行事件
我正在使用的用户控件有一个非常奇怪的问题。我正在编写一些用于DotNetNuke模块的用户控件 基本上,我的页面上有一个UserControl,其中包含一些控件,然后有一个占位符,我在其中加载一个UserControl,其中放置了一个GridView 基本上这里是我的页面结构C# UserControl中未触发Gridview行事件,c#,gridview,user-controls,ascx,commandfield,C#,Gridview,User Controls,Ascx,Commandfield,我正在使用的用户控件有一个非常奇怪的问题。我正在编写一些用于DotNetNuke模块的用户控件 基本上,我的页面上有一个UserControl,其中包含一些控件,然后有一个占位符,我在其中加载一个UserControl,其中放置了一个GridView 基本上这里是我的页面结构 <asp:Panel runat="server" ID="pnlServiceType" Visible="false"> <uc:ServiceType runat="server" ID="S
<asp:Panel runat="server" ID="pnlServiceType" Visible="false">
<uc:ServiceType runat="server" ID="ServiceType" />
</asp:Panel>
以及PopulateServicesGrid方法:
private void PopulateServicesGrid(List<NOAService> services)
{
//Creates a LINQ grouping based on the Billing Codes
//Allows a super easy creation of grids based on the grouped billing codes
var query = services.Select(service => service.BillingCode).Distinct();
foreach (string code in query)
{
var servicesByCode = services.Where(service => service.BillingCode == code).ToList();
ServicesGrid servicesGrid = LoadControl("~/DesktopModules/LEL Modules/NOA/ServicesGrid.ascx") as ServicesGrid;
Label lblServiceHeader = servicesGrid.FindControl("lblServiceHeader") as Label;
GridView gvServices = servicesGrid.FindControl("gvServices") as GridView;
phServices.Controls.Add(servicesGrid);
servicesGrid.ID = code;
lblServiceHeader.Text = servicesByCode[0].FormTypeName;
gvServices.DataSource = servicesByCode;
gvServices.DataBind();
Session["GridCount"] = phServices.Controls.Count;
}
}
Init
方法加载UserControl的新实例并将它们添加到占位符中,但数据源为null
。使用EnableViewState=true
时,看起来数据仍然被绑定,但如果我处理PreRender
,我可以看到我的gvServices上的数据源
为空
甚至可以在反复动态添加到占位符的GridView上编辑这样的行吗
修复了我在参考了这篇文章后发现了问题所在
这让我想,如果身份证换了怎么办?控件是嵌套的。所以我去把一块手表放在GridView的ID、ClientID和UniqueID上只是为了看看。当控件加载到我的Init
处理程序中时,添加时会为其分配一个超级通用ID
private void PopulateServicesGrid(List<NOAService> services)
{
//Creates a LINQ grouping based on the Billing Codes
//Allows a super easy creation of grids based on the grouped billing codes
var query = services.Select(service => service.BillingCode).Distinct();
foreach (string code in query)
{
var servicesByCode = services.Where(service => service.BillingCode == code).ToList();
ServicesGrid servicesGrid = LoadControl("~/DesktopModules/LEL Modules/NOA/ServicesGrid.ascx") as ServicesGrid;
Label lblServiceHeader = servicesGrid.FindControl("lblServiceHeader") as Label;
GridView gvServices = servicesGrid.FindControl("gvServices") as GridView;
phServices.Controls.Add(servicesGrid);
**servicesGrid.ID = code;**
lblServiceHeader.Text = servicesByCode[0].FormTypeName;
gvServices.DataSource = servicesByCode;
gvServices.DataBind();
Session["GridCount"] = phServices.Controls.Count;
}
}
private void PopulateServicesGrid(列出服务)
{
//根据计费代码创建LINQ分组
//允许基于分组计费代码超级轻松地创建网格
var query=services.Select(service=>service.BillingCode.Distinct();
foreach(查询中的字符串代码)
{
var servicesByCode=services.Where(service=>service.BillingCode==code.ToList();
ServicesGrid ServicesGrid=LoadControl(“~/DesktopModules/LEL Modules/NOA/ServicesGrid.ascx”)作为ServicesGrid;
标签lblServiceHeader=servicesGrid.FindControl(“lblServiceHeader”)作为标签;
GridView gvServices=servicesGrid.FindControl(“gvServices”)作为GridView;
phServices.Controls.Add(servicesGrid);
**servicesGrid.ID=代码**
lblServiceHeader.Text=servicesByCode[0]。FormTypeName;
gvServices.DataSource=servicesByCode;
gvServices.DataBind();
会话[“GridCount”]=phServices.Controls.Count;
}
}
我正在将ID设置为其他内容。因此,当我在网格上点击Edit row命令时,它再次在Init
上重新加载控件,并且ID再次从我的自定义设置代码(T2020,从数据库中提取)更改回通用ID,它不知道如何触发事件
我希望这能帮助一些人,因为我已经花了至少12个小时来解决这个问题
_serviceID = e.Value;
//Clear the controls out of the placeholder
phServices.Controls.Clear();
//Reset the count of grids for the OnInit() method
Session["GridCount"] = 0;
if (e.Value != "NULL")
{
PopulateServicesGrid(_service.GetServicesByFormType(Convert.ToInt32(e.Value)));
}
else
{
PopulateServicesGrid(_service.GetServicesByClient(Convert.ToInt32(_client)));
}
private void PopulateServicesGrid(List<NOAService> services)
{
//Creates a LINQ grouping based on the Billing Codes
//Allows a super easy creation of grids based on the grouped billing codes
var query = services.Select(service => service.BillingCode).Distinct();
foreach (string code in query)
{
var servicesByCode = services.Where(service => service.BillingCode == code).ToList();
ServicesGrid servicesGrid = LoadControl("~/DesktopModules/LEL Modules/NOA/ServicesGrid.ascx") as ServicesGrid;
Label lblServiceHeader = servicesGrid.FindControl("lblServiceHeader") as Label;
GridView gvServices = servicesGrid.FindControl("gvServices") as GridView;
phServices.Controls.Add(servicesGrid);
servicesGrid.ID = code;
lblServiceHeader.Text = servicesByCode[0].FormTypeName;
gvServices.DataSource = servicesByCode;
gvServices.DataBind();
Session["GridCount"] = phServices.Controls.Count;
}
}
void NOAServiceType_Init(object sender, EventArgs e)
{
for (int i = 0; i < Convert.ToInt32(Session["GridCount"]); i++)
{
ServicesGrid servicesGrid = LoadControl("~/DesktopModules/LEL Modules/NOA/ServicesGrid.ascx") as ServicesGrid;
phServices.Controls.Add(servicesGrid);
}
}
<asp:CommandField ShowEditButton="true" ItemStyle-HorizontalAlign="Right" EditText="Edit" UpdateText="Update"
EditImageUrl="~/images/LELModules/appbar.edit.rest.png" CancelImageUrl="~/images/LELModules/appbar.close.rest.png" UpdateImageUrl="~/images/LELModules/appbar.check.rest.png"
ButtonType="Image" CausesValidation="false" />
ServiceType UserControl < `Init` fires here
-Form elements
-Placeholder which holds
--UserControl with GridView
private void PopulateServicesGrid(List<NOAService> services)
{
//Creates a LINQ grouping based on the Billing Codes
//Allows a super easy creation of grids based on the grouped billing codes
var query = services.Select(service => service.BillingCode).Distinct();
foreach (string code in query)
{
var servicesByCode = services.Where(service => service.BillingCode == code).ToList();
ServicesGrid servicesGrid = LoadControl("~/DesktopModules/LEL Modules/NOA/ServicesGrid.ascx") as ServicesGrid;
Label lblServiceHeader = servicesGrid.FindControl("lblServiceHeader") as Label;
GridView gvServices = servicesGrid.FindControl("gvServices") as GridView;
phServices.Controls.Add(servicesGrid);
**servicesGrid.ID = code;**
lblServiceHeader.Text = servicesByCode[0].FormTypeName;
gvServices.DataSource = servicesByCode;
gvServices.DataBind();
Session["GridCount"] = phServices.Controls.Count;
}
}