如何以编程方式从详细信息视图访问控件?

发布于 2024-10-19 17:29:40 字数 2796 浏览 8 评论 0原文

我需要能够在数据绑定上以编程方式修改详细信息视图中的控件。现在我正在使用此代码,但收到“索引超出范围”错误。

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 &raquo;"></asp:HyperLink>                                        

                                </ItemTemplate>
                            </asp:TemplateField>                                
                        </Fields>
                        <PagerSettings Mode="NextPreviousFirstLast" PageButtonCount="5" FirstPageText="&larr; First" LastPageText="Last &rarr;"
                            nextpagetext="Next &raquo;" previouspagetext="&laquo; 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

再见回来 2024-10-26 17:29:40

详细信息视图的数据源使用网格视图的选定值作为其选择控制参数,并且在页面加载时网格视图还没有选定索引,因此详细信息视图是空的。我必须在页面加载时设置 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.

染墨丶若流云 2024-10-26 17:29:40

看来 DataBound 事件不是解决此类问题的最佳事件。尝试使用 ItemCreated 事件而是事件处理程序。就像这里,例如:

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

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:

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
寻找一个思念的角度 2024-10-26 17:29:40

您还可以将详细信息视图可见属性设置为 false 在页面加载事件上

You can also set the detailsview visible property to false On page Load Event

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文