我认为这是一个简单的问题,但它可能更复杂..
我花了几天时间在Google上寻找答案以及本网站上的各种问题,但似乎无法做到。
我要做的是从Page Load事件上的Default.aspx.cs绑定到User Control ascx页面上的Gridview。
用户控制标记如下:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="VulnerabilityExternalIP.ascx.cs" Inherits="VulnerabilityAssesment.Controls.VulnerabilityExternalIP" %>
<asp:Table runat="server" CellPadding="1" CellSpacing="2">
<asp:TableRow>
<asp:TableCell>
<asp:Label ID="lblVulnerabilityName" runat="server" Text="Vulnerability Name:" CssClass="itemName"></asp:Label>
</asp:TableCell>
<asp:TableCell>
<asp:Label ID="lblVulnerabilityNameText" runat="server" Text='<%# Eval("MainVulnerabilityName") %>'></asp:Label>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>
<asp:Label ID="lblVulnerabilityCategory" runat="server" Text="Category:" CssClass="itemName"></asp:Label>
</asp:TableCell>
<asp:TableCell>
<asp:Label ID="lblVulnerabilityCategoryText" runat="server" Text='<%# Eval("Category") %>'></asp:Label>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>
<asp:Label ID="lblVulnerabilityPopularity" runat="server" Text="Popularity:" CssClass="itemName"></asp:Label>
</asp:TableCell>
<asp:TableCell>
<asp:Label ID="lblVulnerabilityPopularityText" runat="server" Text='<%# Eval("Popularity") %>'></asp:Label>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>
<asp:Label ID="lblVulnerabilityRisk" runat="server" Text="Risk:" CssClass="itemName"></asp:Label>
</asp:TableCell>
<asp:TableCell>
<asp:Label ID="blVulnerabilityRiskText" runat="server" Text='<%# Eval("RiskFactor") %>'></asp:Label>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
<br />
<asp:Label ID="lblHostsAffected" runat="server" Text="Hosts Affected:" CssClass="itemName"></asp:Label>
<br />
<asp:TextBox ID="txtHostsAffected" runat="server" TextMode="MultiLine" Width="700px" ReadOnly="true" BroderWidth="0px" Text='<%# Eval("HostNamePort") %>'></asp:TextBox>
<asp:GridView ID="gvHostsAffected" runat="server" AutoGenerateColumns="False" AutoGenerateEditButton="True">
</asp:GridView>
我在Default.aspx上引用了User Control,如下所示:
<%@ Register Src="~/Controls/VulnerabilityExternalIP.ascx" TagName="VulnerabilityExternalIP" TagPrefix="uc1" %>
在default.asxp中我定义了以下内容:
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:ListView ID="lvVulnerabilityExternalIP" runat="server">
<ItemTemplate>
<uc1:VulnerabilityExternalIP Template="<%# Container.DataItem %>" runat="server" ID="vulnerabilityExtIP" name="vulnext" />
</ItemTemplate>
</asp:ListView>
</asp:Content>
Default.aspx.cs背后的代码如下:
namespace VulnerabilityAssesment
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
string HostNamePort = "";
string VulnerabilityDesc = "";
string VulnerabilitySummary = "";
string VulnerabilitySolution = "";
string VulnerabilityCVE = "";
string VulnerbilityLink = "";
// Use connection string from Web.Config
string connection = ConfigurationManager.ConnectionStrings["csVulnerabilityAssesment"].ConnectionString;
//Create new SQL Connection
SqlConnection conn = new SqlConnection(connection);
//Create Stored Procedure Command and Declare Parameters
SqlCommand vulnerabilityHeader = new SqlCommand("[dbo].[_spGet_VulnerabilityHeader]", conn);
vulnerabilityHeader.CommandType = CommandType.StoredProcedure;
vulnerabilityHeader.Parameters.Add("@VulnerabilityReport", SqlDbType.VarChar).Value = "External IP Ranges";
//Create Data Adapter and Data Set
SqlDataAdapter sdaVulnerabilityHeader = new SqlDataAdapter(vulnerabilityHeader);
DataSet dsVulnerabilityHeader = new DataSet();
//Open Connection
conn.Open();
//Fill Data Adapter
sdaVulnerabilityHeader.Fill(dsVulnerabilityHeader);
//Fill in template
List<VulnerabilityTemplate> Template = new List<VulnerabilityTemplate>();
if (dsVulnerabilityHeader.Tables != null)
if (dsVulnerabilityHeader.Tables[0].Rows.Count == 0)
{
}
else
if (dsVulnerabilityHeader.Tables.Count > 0)
{
foreach (DataRow dr in dsVulnerabilityHeader.Tables[0].Rows)
{
string GroupID = dr["GroupSequence"].ToString();
//Declare Stored Procedue for Vulnerability Details and set Parameters
SqlCommand vulnerabilityDetail = new SqlCommand("[dbo].[_spGet_VulnerabilityDetail]", conn);
vulnerabilityDetail.CommandType = CommandType.StoredProcedure;
vulnerabilityDetail.Parameters.Add("@VulnerabilityReport", SqlDbType.VarChar).Value = "External IP Ranges";
vulnerabilityDetail.Parameters.Add("@GroupSequence", SqlDbType.VarChar).Value = GroupID;
// Declare SQL Data Adapter for Vulnerability Detail
SqlDataAdapter sdaVulnerabilityDetail = new SqlDataAdapter(vulnerabilityDetail);
DataSet dsVulnerabilityDetail = new DataSet();
// Fill Data Adapter
sdaVulnerabilityDetail.Fill(dsVulnerabilityDetail);
// Declare Stored Procedure for Vulnerability Summary and Set Paramters
SqlCommand vulnerabilitySummary = new SqlCommand("[dbo].[_spGet_VulnerabilitySummary]", conn);
vulnerabilitySummary.CommandType = CommandType.StoredProcedure;
vulnerabilitySummary.Parameters.Add("@VulnerabilityReport", SqlDbType.VarChar).Value = "External IP Ranges";
vulnerabilitySummary.Parameters.Add("@GroupSequence", SqlDbType.VarChar).Value = GroupID;
// Declare SQL Data Adapter for Vulnerability Detail
SqlDataAdapter sdaVulnerabilitySummary = new SqlDataAdapter(vulnerabilitySummary);
DataSet dsVulnerabilitySummary = new DataSet();
// Fill Data Adapter
sdaVulnerabilityDetail.Fill(dsVulnerabilityDetail);
sdaVulnerabilitySummary.Fill(dsVulnerabilitySummary);
foreach (DataRow row in dsVulnerabilityDetail.Tables[0].Rows)
{
if (HostNamePort != "")
HostNamePort += Environment.NewLine;
HostNamePort += row["HostnamePort"].ToString();
}
foreach (DataRow vulnerabilitySummaryRow in dsVulnerabilitySummary.Tables[0].Rows)
{
VulnerabilityDesc += vulnerabilitySummaryRow["VulnerabilityDesc"].ToString();
VulnerabilitySummary += vulnerabilitySummaryRow["VulnerabilitySummary"].ToString();
VulnerabilitySolution += vulnerabilitySummaryRow["VulnerabilitySolution"].ToString();
}
//myGrid.DataSource = dsVulnerabilityDetail.Tables[0];
//GridView myGrid = (GridView)lvVulnerabilityExternalIP.Items.FindControl("gvHostsAffected");
// Always returns null :(
GridView myGrid = (GridView)lvVulnerabilityExternalIP.FindControl("gvHostsAffected");
Template.Add(new VulnerabilityTemplate
{
MainVulnerabilityName = dr["MainVulnerabilityName"].ToString(),
Category = dr["Category"].ToString(),
Popularity = dr["Popularity"].ToString(),
Riskfactor = dr["RiskFactor"].ToString(),
HostNamePort = HostNamePort
//VulnerabilityDesc = VulnerabilityDesc,
//VulnerabilitySolution = VulnerabilitySolution,
//VulnerabilitySummary = VulnerabilitySummary
}
);
myGrid.DataBind();
}
}
lvVulnerabilityExternalIP.DataSource = Template;
lvVulnerabilityExternalIP.DataBind();
conn.Close();
}
// Below Does not work
protected void ListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
DataTable table = (DataTable)e.Item.DataItem;
GridView myGrid = (GridView)e.Item.FindControl("gvHostsAffected");
myGrid.DataSource = table;
myGrid.DataBind();
}
}
}
}
我似乎无法找到gvHostsAffected网格视图将其绑定到dsVulnerabilityDetail.Tables [0]值(该列应该是名为HostsAffected的单个列,此结果可能会返回1行或更多行。
我已经包含了一些我试过用注释表示的东西//下面不起作用。
Template.Add方法确实有效,它会填充用户控件的必要信息。
有什么办法可以在迭代过程中找到Gridview控件并使用dsVulnerabilityDetail数据集的结果填充它?
提前谢谢。
2017年4月10日更新
下面是WebControl的Code Behind,我发现我忘记将其包含在原始问题中。
namespace VulnerabilityAssesment.Controls
{
public partial class VulnerabilityExternalIP : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public GridView myGridView
{
get
{
return gvHostsAffected;
}
set
{
gvHostsAffected = value;
}
}
}
}
Web Control的标记包括VDWWD的答案。
答案 0 :(得分:0)
首先,将一个公共GridView
属性添加到用户控件
public partial class WebUserControl1 : System.Web.UI.UserControl
{
public GridView myGridView
{
get
{
return GridView1;
}
set
{
GridView1 = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
然后你可以像访问同一页面上的GridView一样访问它。剩下要做的唯一事情就是使用FindControl
在ListView
protected void Button1_Click(object sender, EventArgs e)
{
WebUserControl1 control = lvVulnerabilityExternalIP.Items[2].FindControl("vulnerabilityExtIP") as WebUserControl1;
control.myGridView.DataSource = mySource;
control.myGridView.DataBind();
}