数据绑定无法更改 UpdatePanel 内 GridView 内自定义控件的值
Data binding fails to change values for custom control inside GridView inside an UpdatePanel
我正在这样的 UpdatePanel
中创建一个 GridView
:
<asp:ScriptManager runat="server" EnablePartialRendering="true" />
<div id="masterHistoryDialog" style="display: none">
<asp:UpdatePanel runat="server" ChildrenAsTriggers="true" UpdateMode="Always">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="historyRepeater" EventName="PageIndexChanging" />
</Triggers>
<ContentTemplate>
<asp:GridView ID="historyRepeater" AutoGenerateColumns="false" runat="server" AllowPaging="true" AllowSorting="false" PagerSettings-NextPageText="Next" PagerSettings-PreviousPageText="Previous" PageSize="4" OnPageIndexChanging="historyRepeater_PageIndexChanging">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<uc:CustomCalendarControl ChangeID='<%# DataBinder.Eval(Container.DataItem, "Item1") %>' ScheduleID='<%# DataBinder.Eval(Container.DataItem, "Item2") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
PageIndexChanging
事件按预期触发,调用以下事件处理程序:
Protected Sub historyRepeater_PageIndexChanging(sender As Object, e As GridViewPageEventArgs)
historyRepeater.PageIndex = e.NewPageIndex
historyRepeater.DataSource = GetSchedules()
historyRepeater.DataBind()
End Sub
CustomCalendarControl
是自定义控件。
我确认 GetSchedules
实际上正在获取我的整个数据集。
我对数据进行分页,每页有 4 个自定义控件(5 页共 20 个项目)。第一页工作正常。但是,当我尝试导航到其他页面之一时,自定义控件的初始化失败,因为它没有获得 ChangeID
或 ScheduleID
的实际值 - 它们始终为 0.
当我调用 DataBind()
时失败,因为自定义控件的初始化取决于 ChangeID
和 ScheduleID
是实际 ID。
我该如何解决这个问题?
您的问题必须在嵌套的用户控件填充其客户端内容时解决。这首先发生在控件上,然后是 GridView,因此在设置 ChangeID
和 ScheduleID
的值之前已经构建了 UserControl。
解决这个问题的一种方法是从父级手动调用控件的构建。
首先,修改 UserControl 使其具有 public 函数以正确显示值
public string ChangeID { get; set; }
public string ScheduleID { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
setUCValues();
}
public void setUCValues()
{
Label1.Text = ChangeID;
Label2.Text = ScheduleID;
}
Public Property ChangeID As String
Get
End Get
Set
End Set
End Property
Public Property ScheduleID As String
Get
End Get
Set
End Set
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
setUCValues
End Sub
Public Sub setUCValues()
Label1.Text = ChangeID
Label2.Text = ScheduleID
End Sub
接下来,在 GridView
的 UserControl 中添加一个 ID
<uc:CustomCalendarControl ID="calendarControl" runat="server" ChangeID=
然后在PageIndexChanging中,循环所有行并调用函数
protected void historyRepeater_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//databinding
foreach (GridViewRow row in historyRepeater.Rows)
{
WebUserControl1 control = row.FindControl("calendarControl") as WebUserControl1;
control.setUCValues();
}
}
Protected Sub historyRepeater_PageIndexChanging(sender As Object, e As GridViewPageEventArgs)
'databinding
For Each row As GridViewRow In historyRepeater.Rows
Dim control As WebUserControl1 = CType(row.FindControl("calendarControl"),WebUserControl1)
control.setUCValues
Next
End Sub
我正在这样的 UpdatePanel
中创建一个 GridView
:
<asp:ScriptManager runat="server" EnablePartialRendering="true" />
<div id="masterHistoryDialog" style="display: none">
<asp:UpdatePanel runat="server" ChildrenAsTriggers="true" UpdateMode="Always">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="historyRepeater" EventName="PageIndexChanging" />
</Triggers>
<ContentTemplate>
<asp:GridView ID="historyRepeater" AutoGenerateColumns="false" runat="server" AllowPaging="true" AllowSorting="false" PagerSettings-NextPageText="Next" PagerSettings-PreviousPageText="Previous" PageSize="4" OnPageIndexChanging="historyRepeater_PageIndexChanging">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<uc:CustomCalendarControl ChangeID='<%# DataBinder.Eval(Container.DataItem, "Item1") %>' ScheduleID='<%# DataBinder.Eval(Container.DataItem, "Item2") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
PageIndexChanging
事件按预期触发,调用以下事件处理程序:
Protected Sub historyRepeater_PageIndexChanging(sender As Object, e As GridViewPageEventArgs)
historyRepeater.PageIndex = e.NewPageIndex
historyRepeater.DataSource = GetSchedules()
historyRepeater.DataBind()
End Sub
CustomCalendarControl
是自定义控件。
我确认 GetSchedules
实际上正在获取我的整个数据集。
我对数据进行分页,每页有 4 个自定义控件(5 页共 20 个项目)。第一页工作正常。但是,当我尝试导航到其他页面之一时,自定义控件的初始化失败,因为它没有获得 ChangeID
或 ScheduleID
的实际值 - 它们始终为 0.
当我调用 DataBind()
时失败,因为自定义控件的初始化取决于 ChangeID
和 ScheduleID
是实际 ID。
我该如何解决这个问题?
您的问题必须在嵌套的用户控件填充其客户端内容时解决。这首先发生在控件上,然后是 GridView,因此在设置 ChangeID
和 ScheduleID
的值之前已经构建了 UserControl。
解决这个问题的一种方法是从父级手动调用控件的构建。
首先,修改 UserControl 使其具有 public 函数以正确显示值
public string ChangeID { get; set; }
public string ScheduleID { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
setUCValues();
}
public void setUCValues()
{
Label1.Text = ChangeID;
Label2.Text = ScheduleID;
}
Public Property ChangeID As String
Get
End Get
Set
End Set
End Property
Public Property ScheduleID As String
Get
End Get
Set
End Set
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
setUCValues
End Sub
Public Sub setUCValues()
Label1.Text = ChangeID
Label2.Text = ScheduleID
End Sub
接下来,在 GridView
的 UserControl 中添加一个ID
<uc:CustomCalendarControl ID="calendarControl" runat="server" ChangeID=
然后在PageIndexChanging中,循环所有行并调用函数
protected void historyRepeater_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//databinding
foreach (GridViewRow row in historyRepeater.Rows)
{
WebUserControl1 control = row.FindControl("calendarControl") as WebUserControl1;
control.setUCValues();
}
}
Protected Sub historyRepeater_PageIndexChanging(sender As Object, e As GridViewPageEventArgs)
'databinding
For Each row As GridViewRow In historyRepeater.Rows
Dim control As WebUserControl1 = CType(row.FindControl("calendarControl"),WebUserControl1)
control.setUCValues
Next
End Sub