【问题标题】:Changing a columns contents in a GridView from VB code从 VB 代码更改 GridView 中的列内容
【发布时间】:2017-07-29 14:12:23
【问题描述】:

我正在显示一个数据网格,其中包括用户名和个人资料图片等。 如果他们没有上传个人资料图片,我已经设置了下面的代码来设置 imgPic.ImageUrl = "~/files/images/blankProfilePic.png"

但是,这仅适用于显示的 6 页数据的第一页。当您切换到第二页时,我会看到丢失的图片图标,因为下面的代码仅适用于显示的初始页面。 我需要更改此代码以在每个页面上工作,因为它仅在页面加载时调用。

VB 代码


Protected Sub Page_LoadComplete(sender As Object, e As System.EventArgs) Handles Me.LoadComplete

    Dim acc As New accounts(Membership.GetUser.ProviderUserKey)

    If acc.region = "North East" Then
        For Each r As GridViewRow in gdvNorthEast.Rows
            If r.RowType = DataControlRowType.DataRow Then
                Dim imgPic As Image
                imgPic = r.Cells(4).FindControl("imgProfilePic")
                If imgPic.ImageUrl = "~/catalog/images/"                                                                                                                                        
                    imgPic.ImageUrl = "~/files/images/blankProfilePic.png"
                End If
            End If
        Next r
    End If

ASP.NET


<asp:SqlDataSource 
     ID="DSLeaderboardNorthEast"
     runat="server"
     ConnectionString="<%$ ConnectionStrings:DBConnectionString %>"
     SelectCommand="SELECT tblAccounts.contactName, tblAccounts.profilePic, tblAccounts.minAge, tblAccounts.maxAge, tblAccounts.AccountID, tblAccounts.region
                    FROM tblAccounts 
                    WHERE tblAccounts.region='North East' 
                    ORDER BY tblAccounts.contactName ">
</asp:SqlDataSource>

<asp:GridView ID="gdvNorthEast" width="100%" runat="server" AllowPaging="True" AutoGenerateColumns="False" CssClass="mGrid" DataKeyNames="accountID" DataSourceID="DSLeaderboardNorthEast" PageSize="20" AllowSorting="True">
    <AlternatingRowStyle CssClass="alt" />
    <Columns>
        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <asp:Image ID="imgProfilePic" AlternateText="Players profile picture" runat="server" width="100px" height="100px" ImageUrl='<%# "~/catalog/images/" &  Eval("ProfilePic").ToString %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="contactName" HeaderText="Player Name" />     
        <asp:BoundField DataField="minAge" HeaderText="Won" />
        <asp:BoundField DataField="maxAge" HeaderText="Lost" />
        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                    <asp:HyperLink ID="hypMessage" runat="server" CssClass="nyroModalMsg" NavigateUrl='<%# "~/sendLeaderboardMessage.aspx?" & "&aID=" & Eval("accountID").ToString %>'>Msg</asp:HyperLink>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

【问题讨论】:

标签: sql asp.net vb.net


【解决方案1】:

用于分配默认图像的 foreach 循环应该是可重用的方法。

使用PageIndexChanged 事件处理程序调用相同的可重用方法。

更新: 由于上述选项没有帮助,请关注this post

    //RowDataBound Event  
protected void gdvNorthEast_RowDataBound(object sender, GridViewRowEventArgs e)  
{  
    //Checking the RowType of the Row  
    if(e.Row.RowType==DataControlRowType.DataRow)  
    {  
        //your logic goes here without foreach loop  
    }  
}  

【讨论】:

  • 嗯,对.. 有道理.. 我会去研究 PageIndexChanged。我用 Page_LoadComplete 试过了,但没用。我快到了 ;-)
  • 抱歉,如果我发错地方了.. 对我来说很棘手,但我无法继续使用 Pageindexchanged,如上所述。任何进一步的帮助表示赞赏。
  • 当你改变页面时它肯定会进入子程序,但代码对任何页面都不起作用,但首先。
  • 我还注意到您使用单个 = 来比较图像 URL。它在工作吗? == 比较 = 赋值..
  • 是的,我正在尝试在 VB 中执行此操作,所以只有一个 '='
【解决方案2】:

使用Gridview_RowDataBound事件使替换功能改为使用for指令

【讨论】:

  • 你的意思是“使用页面加载”?
  • 谢谢@fabioTos 我不完全确定该怎么做。很多代码都是在我刚刚学习时为我编写的。你能告诉我在哪里添加一个 For 吗?
  • 他的意思是你必须使用 RowDataBound 事件而不是 Page_LoadComplete 事件。这是另一种选择。
  • 我会配合欢呼。不过我还在挣扎。我设置了 Public Sub gdvNorthEast_PageIndexChanged(ByVal sender As Object, ByVal e As EventArgs) 并在grindview控件中放置了 onpageindexchanged="gdvNorthEast_PageIndexChanged" 但是当您再次更改页面时,同样的事情会发生。
  • 尝试放入 Sub gdvNorthEast_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) 事件并在您的 gridview 上添加 onrowdatabound 属性。在此事件中,您不需要为每个 cicly 进行,但您可以使用参数 e 获取行信息
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多