如何以编程方式从详细信息视图访问控件?
我需要能够在数据绑定上以编程方式修改详细信息视图中的控件。现在我正在使用此代码,但收到“索引超出范围”错误。
Private Sub dtlApplication_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtlApplication.DataBound
Dim resumeLink As HyperLink = dtlApplication.Rows.Item(0).FindControl("lnkResume")
resumeLink.NavigateUrl = "Resumes/"
End Sub
我也尝试过此操作,但收到“对象引用未设置到对象实例”错误。
Private Sub dtlApplication_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtlApplication.DataBound
Dim resumeLink As HyperLink = dtlApplication.FindControl("lnkResume")
resumeLink.NavigateUrl = "Resumes/"
End Sub
我认为问题可能是,当页面最初加载时,详细信息视图没有任何控件,因为直到我在主网格视图中选择一行后,它才获取它们。基本上,我尝试在网格视图中选择一行时执行此代码,而不是在页面最初加载时执行。会是这样吗?如果是这样,如果不在详细信息视图数据绑定中,我应该在哪里执行此代码?
这是详细信息视图和相应的数据源标记:
<asp:DetailsView ID="dtlApplication" runat="server" AutoGenerateRows="false"
DataKeyNames="appID" DataSourceID="ds2" CellPadding="0" BorderColor="Transparent"
BorderWidth="0px" GridLines="None" HorizontalAlign="Left" Width="459" CssClass="dtlView">
<Fields>
<asp:TemplateField showheader="false">
<ItemTemplate>
<h3>Resume</h3>
<asp:HyperLink runat="server" ID="lnkResume" Text="View Resume »"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Fields>
<PagerSettings Mode="NextPreviousFirstLast" PageButtonCount="5" FirstPageText="← First" LastPageText="Last →"
nextpagetext="Next »" previouspagetext="« Previous" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" CssClass="paging" />
</asp:DetailsView>
<asp:SqlDataSource ID="ds2" runat="server" ConnectionString="<%$ ConnectionStrings:cn %>"
SelectCommandType="StoredProcedure" SelectCommand="sp_SelectApplicationDetail"
EnableCaching="true" CacheDuration="600">
<SelectParameters>
<asp:ControlParameter Name="appID" ControlID="gvAdmin" PropertyName="SelectedValue"></asp:ControlParameter>
</SelectParameters>
</asp:SqlDataSource>
I need to be able to modify controls from my detailsview programmatically on databind. Right now I'm using this code but I'm getting an "Index was out of range" error.
Private Sub dtlApplication_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtlApplication.DataBound
Dim resumeLink As HyperLink = dtlApplication.Rows.Item(0).FindControl("lnkResume")
resumeLink.NavigateUrl = "Resumes/"
End Sub
I also tried this but got an "Object reference not set to an instance of an object" error.
Private Sub dtlApplication_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtlApplication.DataBound
Dim resumeLink As HyperLink = dtlApplication.FindControl("lnkResume")
resumeLink.NavigateUrl = "Resumes/"
End Sub
I think the problem might be that the detailsview does not have any controls when the page initially loads since it doesn't get them until I select a row in my main gridview. Basically, I am trying to execute this code when I select a row in the gridview, not when the page initially loads. Could that be it? If so, where should I execute this code if not in the detailsview databound?
Here is the detailsview and corresponding datasource markup:
<asp:DetailsView ID="dtlApplication" runat="server" AutoGenerateRows="false"
DataKeyNames="appID" DataSourceID="ds2" CellPadding="0" BorderColor="Transparent"
BorderWidth="0px" GridLines="None" HorizontalAlign="Left" Width="459" CssClass="dtlView">
<Fields>
<asp:TemplateField showheader="false">
<ItemTemplate>
<h3>Resume</h3>
<asp:HyperLink runat="server" ID="lnkResume" Text="View Resume »"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Fields>
<PagerSettings Mode="NextPreviousFirstLast" PageButtonCount="5" FirstPageText="← First" LastPageText="Last →"
nextpagetext="Next »" previouspagetext="« Previous" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" CssClass="paging" />
</asp:DetailsView>
<asp:SqlDataSource ID="ds2" runat="server" ConnectionString="<%$ ConnectionStrings:cn %>"
SelectCommandType="StoredProcedure" SelectCommand="sp_SelectApplicationDetail"
EnableCaching="true" CacheDuration="600">
<SelectParameters>
<asp:ControlParameter Name="appID" ControlID="gvAdmin" PropertyName="SelectedValue"></asp:ControlParameter>
</SelectParameters>
</asp:SqlDataSource>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
详细信息视图的数据源使用网格视图的选定值作为其选择控制参数,并且在页面加载时网格视图还没有选定索引,因此详细信息视图是空的。我必须在页面加载时设置 gridview 的 selectedindex 才能解决问题。
The detailsview's datasource uses a gridview's selectedvalue as it's select control parameter, and upon page load the gridview does not have a selectedindex yet, so the detailsview is empty. I had to set the gridview's selectedindex on page load to fix the problem.
看来 DataBound 事件不是解决此类问题的最佳事件。尝试使用 ItemCreated 事件而是事件处理程序。就像这里,例如:
It seems that DataBound event is not a the best event for such issue. Try to use ItemCreated event event handler instead. Like here, for example:
您还可以将详细信息视图可见属性设置为 false 在页面加载事件上
You can also set the detailsview visible property to false On page Load Event