我创建了一个母版页(我的模板页面),并在其上放置了一个gridview。现在我将创建将使用此母版页的多页(aspx)。我不是要为这些aspx页面进行所有设置,而是要配置数据源。当aspx加载时,它将数据源连接到gridview(在主页面上)并加载它。现在我想操作gridview中的数据,但我的datakeys count返回0.我可以通过gv.Rows(dr.RowIndex).Cells(5)
访问值。但我想通过数据键来做到这一点。我的代码如下,任何帮助表示赞赏。感谢。
Public Sub ConnectDataSource()
Dim mpContentPlaceHolder As ContentPlaceHolder
Dim gv As GridView
mpContentPlaceHolder = CType(Master.FindControl("GridViewPlaceHolder"), ContentPlaceHolder)
If Not mpContentPlaceHolder Is Nothing Then
gv = CType(mpContentPlaceHolder.FindControl("GridView1"), GridView)
If Not gv Is Nothing Then
Dim es As EntityDataSource = EntityDataSource1
gv.DataSource = es
gv.DataBind()
Debug.WriteLine("datakey count " + gv.DataKeys().Count.ToString())
' gv.HeaderRow.Cells(2).Text = "sdfds" // to change header text for each column.
End If
End If
End Sub
Private Sub PaymentRules_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim toolbarPlaceHolder As ContentPlaceHolder
Dim btn As ImageButton
toolbarPlaceHolder = CType(Master.FindControl("ToolBarPlaceHolder"), ContentPlaceHolder)
If Not toolbarPlaceHolder Is Nothing Then
btn = DirectCast(toolbarPlaceHolder.FindControl("ImageButton2"), ImageButton)
If Not btn Is Nothing Then
AddHandler btn.Click, AddressOf ImageButton2_Click
End If
End If
If (Not Page.IsPostBack) Then
ConnectDataSource()
End If
End Sub
Protected Sub ImageButton2_Click(sender As Object, e As EventArgs)
Debug.WriteLine("ImageButton2_Click")
Dim maList As New List(Of Integer)
Dim mContentPlaceHolder As ContentPlaceHolder
Dim gv As GridView
mContentPlaceHolder = CType(Master.FindControl("GridViewPlaceHolder"), ContentPlaceHolder)
If Not mContentPlaceHolder Is Nothing Then
gv = CType(mContentPlaceHolder.FindControl("GridView1"), GridView)
If Not gv Is Nothing Then
For Each dr As GridViewRow In gv.Rows
If ((TryCast(dr.FindControl("CheckBox1"), CheckBox)).Checked = True) Then
'Debug.WriteLine(dr.RowIndex.ToString())
'Dim id As Integer = gv.DataKeys(dr.RowIndex).Value
'maList.Add(id)
Debug.WriteLine("row index " + gv.Rows(dr.RowIndex).Cells(5).Text)
Debug.WriteLine("datakey count " + gv.DataKeys().Count.ToString())
End If
Next
End If
End If
End Sub
已编辑 - 母版页代码
<%@ Master Language="VB" AutoEventWireup="false" CodeBehind="JSPSite.master.vb" Inherits="SSPayroll.JSPSite" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
<link href="../Styles/Style1.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="Form1" runat="server">
<div class="page">
<div class="header">
<div class="title">
<h1>
<asp:Image ID="Image1" runat="server" ImageUrl="~/images/GSCV1.01 - Copy.png"
Height="73px" style="margin-top: 20px" Width="428px" />
</h1>
</div>
<div class="loginDisplay">
<asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false">
<LoggedInTemplate>
<asp:Label ID="Label2" runat="server" Text="User: " Font-Names="Cambria"></asp:Label><span class="bold">
<asp:LoginName ID="HeadLoginName" runat="server" Font-Names="Cambria" />
</span> [
<asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect"
LogoutPageUrl="~/" Font-Names="Cambria" Font-Size="Small" ForeColor="#D9D5D5" Font-Bold="True" LogoutText="Sign Out" />
]
<div>
</div>
</LoggedInTemplate>
<RoleGroups>
<asp:RoleGroup>
</asp:RoleGroup>
</RoleGroups>
</asp:LoginView>
<asp:Label ID="Label1" runat="server" Text="Date/Time" Font-Names="Cambria"
Font-Size="Small" ForeColor="#333333"></asp:Label>
</div>
<div class="clear hideSkiplink">
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="False"
IncludeStyleBlock="False" Orientation="Horizontal" BackColor="#FFFBD6"
DynamicHorizontalOffset="2" Font-Names="Verdana" Font-Size="0.8em"
ForeColor="#990000" StaticSubMenuIndent="10px" Enabled="True">
<DynamicHoverStyle BackColor="#990000" ForeColor="White" />
<DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
<DynamicMenuStyle BackColor="#FFFBD6" />
<DynamicSelectedStyle BackColor="#FFCC66" />
<Items>
<asp:MenuItem Text="Menu1" Value="Tools">
<asp:MenuItem NavigateUrl="~/JSPayroll/Payment_Rules/PaymentRules.aspx" Text="Payment Rules" Value="Payment Rules"></asp:MenuItem>
</asp:MenuItem>
</Items>
<StaticHoverStyle BackColor="#990000" ForeColor="White" />
<StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
<StaticSelectedStyle BackColor="#FFCC66" />
</asp:Menu>
</div>
</div>
<asp:ContentPlaceHolder ID="ToolBarPlaceHolder" runat="server" >
<asp:Panel ID="Panel1" runat="server" HorizontalAlign="Center">
<div>
<hr />
<asp:ImageButton ID="ImageButton1" runat="server" Height="48px" ImageUrl="~/IconsPack/Home.png" Width="48px" ToolTip="Home" CssClass="morph" />
<asp:ImageButton ID="ImageButton2" runat="server" Height="48px" ImageUrl="~/IconsPack/Add.png" ToolTip="Add Record" CssClass="morph" Width="48px"/>
<asp:ImageButton ID="ImageButton3" runat="server" Height="48px" ImageUrl="~/IconsPack/Edit E.png" ToolTip="Edit Record" CssClass="morph" Width="48px" />
<asp:ImageButton ID="ImageButton4" runat="server" Height="48px" ImageUrl="~/IconsPack/ViewDetails.png" ToolTip="View Record Details" CssClass="morph" Width="48px" />
<asp:ImageButton ID="ImageButton5" runat="server" Height="48px" ImageUrl="~/IconsPack/Delete.png" ToolTip="Delete Record" CssClass="morph" Width="48px" />
<asp:ImageButton ID="ImageButton6" runat="server" Height="48px" ImageUrl="~/IconsPack/Approved.png" ToolTip="Approve Record" CssClass="morph" Width="48px"/>
<asp:ImageButton ID="ImageButton7" runat="server" Height="48px" ImageUrl="~/IconsPack/Denied.png" ToolTip="Unapprove Record" CssClass="morph" Width="48px"/>
<asp:ImageButton ID="ImageButton8" runat="server" Height="48px" ImageUrl="~/IconsPack/Update.png" ToolTip="Refresh Table" CssClass="morph" Width="48px"/>
<hr />
</div>
</asp:Panel>
</asp:ContentPlaceHolder>
<%--<asp:ContentPlaceHolder ID="GridViewPlaceHolder" runat="server">--%>
<div class="EU_TableScroll" id="showData" style="display: block">
<asp:GridView ID="GridView1" runat="server" CssClass="EU_DataTable" EmptyDataText="No Data Available.">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<%--</asp:ContentPlaceHolder>--%>
<div class="main">
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
</div>
<div class="clear">
</div>
</div>
<div class="footer">
</div>
</form>
</body>
</html>
答案 0 :(得分:0)
将ImageButtons和GridView以及EntityDataSource放在ContentPlaceHolde之外 你不需要使用FindControl而是使用CheckBox 在GridView1中,将 DataKeyNames 属性设置为您的密钥。
答案 1 :(得分:0)
您是否在Gridview .aspx端设置了DataKeyNames属性。请查看以下msdn的示例代码。如果您设置DataKeyNames属性,那么我认为您可能只能访问后面代码中的DataKeys属性。
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
autogeneratecolumns="true"
emptydatatext="No data available."
autogenerateselectbutton="true"
datakeynames="CustomerID"
onselectedindexchanged="CustomersGridView_SelectedIndexChanged"
runat="server">
</asp:gridview>