我需要能够在databind上以编程方式修改我的detailsview中的控件。现在我正在使用此代码,但我收到“索引超出范围”错误。
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
我认为问题可能是详细信息视图在页面最初加载时没有任何控件,因为直到我在主网格视图中选择一行时它才会获取它们。基本上,当我在gridview中选择一行时,我尝试执行此代码,而不是在页面最初加载时。可能是吗?如果是这样,如果不在详细信息视图数据绑定中,我应该在哪里执行此代码?
以下是详细信息视图和相应的数据源标记:
<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>
答案 0 :(得分:1)
detailsview的数据源使用gridview的selectedvalue作为其选择控制参数,并且在页面加载时gridview还没有selectedindex,因此detailsview为空。我必须在页面加载时设置gridview的selectedindex以解决问题。
答案 1 :(得分:0)
似乎DataBound事件不是此类问题的最佳事件。请尝试使用ItemCreated event事件处理程序。像这里一样,例如:
Private Sub dtlApplication_ItemCreated(sender As Object, e As EventArgs) Handles dtlApplication.ItemCreated
Dim someRow As DetailsViewRow = dtlApplication.Rows(0);
If someRow Is Nothing Then Exit Sub
Dim link As HyperLink = DirectCast(someRow.FindControl("lnkResume"), HyperLink)
If link Is Nothing Then Exit Sub
link.NavigateUrl = "Resumes/"
End
答案 2 :(得分:0)
您还可以将detailsview visible属性设置为false在页面加载事件