c# - Populating Gridview inside Listview ItemTemplate On Web User Control from Code Behind -
i have think simple question, might more complex..
i have spent few days looking answer on google , various questions on site cannot seem come right.
what trying bind gridview on user control ascx page default.aspx.cs on page load event.
user control markup follows:
<%@ 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>
i referencing user control on default.aspx follows:
<%@ register src="~/controls/vulnerabilityexternalip.ascx" tagname="vulnerabilityexternalip" tagprefix="uc1" %>
within default.asxp have following defined:
<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>
the code behind on default.aspx.cs follows:
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 web.config string connection = configurationmanager.connectionstrings["csvulnerabilityassesment"].connectionstring; //create new sql connection sqlconnection conn = new sqlconnection(connection); //create stored procedure command , 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 , 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 vulnerability details , 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 vulnerability detail sqldataadapter sdavulnerabilitydetail = new sqldataadapter(vulnerabilitydetail); dataset dsvulnerabilitydetail = new dataset(); // fill data adapter sdavulnerabilitydetail.fill(dsvulnerabilitydetail); // declare stored procedure vulnerability summary , 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 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"); // 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 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(); } } } }
i cannot seem find gvhostsaffected grid view bind dsvulnerabilitydetail.tables[0] value (which should single column called hostsaffected, result return 1 or more rows.
i have included 2 things have tried denoted comment //below not work.
the template.add method work , populates necessary information on user control.
is there way can find gridview control during iteration , populate results dsvulnerabilitydetail data set?
thank in advance.
update 10 april 2017
below code behind webcontrol, see forgot include in original question.
namespace vulnerabilityassesment.controls { public partial class vulnerabilityexternalip : system.web.ui.usercontrol { protected void page_load(object sender, eventargs e) { } public gridview mygridview { { return gvhostsaffected; } set { gvhostsaffected = value; } } }
}
the markup web control include answer vdwwd.
first, add public gridview
property user control
public partial class webusercontrol1 : system.web.ui.usercontrol { public gridview mygridview { { return gridview1; } set { gridview1 = value; } } protected void page_load(object sender, eventargs e) { }
then can access yo gridview on same page. thing left use findcontrol
find user control inside listview
protected void button1_click(object sender, eventargs e) { webusercontrol1 control = lvvulnerabilityexternalip.items[2].findcontrol("vulnerabilityextip") webusercontrol1; control.mygridview.datasource = mysource; control.mygridview.databind(); }
Comments
Post a Comment