How do I get a web control's HTML? - asp.net

i am working with String Builder and a Tree view control which i am generating Dynamically in my page.
This is what i have tried with :
Public sbMenu As New StringBuilder
In Page Load Event
Dim TreeView1 As New TreeView()
TreeView1.ID = "tree1"
TreeView1.ShowCheckBoxes = TreeNodeTypes.All
TreeView1.ShowLines = True
TreeView1.Nodes.Clear()
I am loading the TreeView1 using Database data and atlast appending to my string builser object as
sbMenu.Append("<div>")
sbMenu.Append(TreeView1)
sbMenu.Append("</div>")
But the sbMenu doesn't contain the TreeView1 instead it is storing
System.Web.UI.WebControls.TreeView
Can you please help me , By how my stringBuilder can hold the TreeView control and can make use of it..
I will use this in my .aspx page as
<%= sbMenu.Tostring() %>
here i need the TreeView control..

You can get a control's HTML by calling RenderControl. E.g.
var sb = new StringBuilder();
using (var sw = new StringWriter(sb))
using (var writer = new HtmlTextWriter(sw))
{
myControl.RenderControl(writer);
}
string html = sb.ToString();
Or in VB:
Dim sb = New StringBuilder()
Using sw As New StringWriter(sb)
Using writer As New HtmlTextWriter(sw)
myControl.RenderControl(writer)
End Using
End Using
Dim html As String = sb.ToString()

Related

How can I call bind method of grid through webmethod

This is my method to bind in code behind,I want to call it through web method
Private Sub BindData()
Dim objtable As New DataTable("projectinfoclass")
Dim Conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("conString").ConnectionString)
Dim Cmd As SqlDataAdapter = New SqlDataAdapter("select * from ProjectInfoNew", Conn)
Cmd.Fill(objtable)
projectinfolist.Clear()
For Each dr As DataRow In objtable.Rows
projectinfolist.Add(New Projectinfoclass With {
.ProjectNumber = dr("ProjectNumber").ToString(),
.projectId = dr("Projectid").ToString(),
.Projectname = dr("ProjectName").ToString(),
.projectmodifiedDate = dr("ProjectmodifiedDate").ToString(),
.Recordupdateddate = dr("Recordupdateddate").ToString(),
.ProjectLocation = dr("ProjectLocation").ToString(),
.LocationServerName = dr("LocationServerName").ToString(), .ProjectModifiedBy = dr("ProjectModifiedBy").ToString(),
.DBServer = dr("DBServer").ToString(),
.DBName = dr("DBName").ToString(),
.Flag = Nothing})
Next
GridView1.DataSource = objtable
GridView1.DataBind()
End Sub
put your GridView in a usercontrol and follow the solutions here:
How do I get the HTML output of a UserControl in .NET (C#)?
1) Keep the data source and bind bits in the page code behind, move the rest into a web method. The web method can be called in the code behind and you use the result.
or
2) move code into web method as above but use as an object data source in the aspx and that removes all the code behind wiring up.
I hope that's what you're after.

LinkButton not rendered completely

I have this function that I call in page int event
Protected Function RenderLB(text As String, Id As String, argument As String) As [String]
Using Lb = New System.Web.UI.WebControls.LinkButton
Lb.Text = text
Lb.ID = Id
Lb.Attributes.Add("runat", "Server")
Lb.CommandName = "RedirectFillsession"
Lb.CommandArgument = argument
Dim StringBuilder = New System.Text.StringBuilder()
Using stringWriter = New System.IO.StringWriter(StringBuilder)
Using htmlTextWriter = New System.Web.UI.HtmlTextWriter(stringWriter)
Lb.RenderControl(htmlTextWriter)
End Using
End Using
Return StringBuilder.ToString()
End Using
End Function
the returned value is anchor but CommandName and CommandArgument are not rendered
what should I do to render the CommandName and CommandArgument
CommandName and CommandArgument are never rendered into the output HTML, they are used in the accompanying JavaScript.
Also, you should add your control to the controls collection instead of rendering it manually. That way you can be sure its part of the entire Page Life Cycle.
Controls.Add(Lb)

Gridvew.RenderControl returns empty string

I am trying to export a Gridview to excel. I bind the gridview to a collection and can see that it has 6 data rows but when i call the RenderControl it returns an empty string. Below is the code i am using
Gridview1.DataSource = data;
Gridview1.DataBind();
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
Gridview1.RenderControl(htw);
var outputHtml = sw.ToString();
when i check the outputHtml it is an empty string. What i am doing wrong in this piece of code.
One thing to note is that gridview is lying inside a form with runat='server' tag and i have not overridden the VerifyRenderingInServerForm method.
My best bet is that you have set the visibility of the GridView to false. This will prevent the control from rendering, because well...it's invisible now. The result is an empty string.
If you don't want to show the GridView, just set the Visibility to true before you execute your rendering code and set it back afterwards:
gv_sample.Visible = true;
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
gv_sample.RenderControl(htw);
var outputHtml = sw.ToString();
gv_sample.Visible = false;
You possibly will run into trouble with the RenderControl method now. If so, make sure you have set EnableEventValidation="false" in the Page directive and override the VerifyRenderingInServerForm method:
public override void VerifyRenderingInServerForm(Control control)
{
return;
}

Render ASP.Net PlaceHolder .ToString(), not to page

I've searched around and have not been able to find an good solution. I have a custom extension to a PlaceHolder control that will contain expressions that I would like to take the string output of without having to call control.Render(), since that call automatically writes the contents out to the page.
Does anybody know how to get the would be rendered content into a string and prevent the page from containing it?
The often-regurgitated, slightly dated code for this goes something like:
public string RenderControl(Control ctrl)
{
StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctrl.RenderControl(hw);
return sb.ToString();
}

How to pass parameters to SSRS report programmatically

I'm looking for a little help on programmatically passing parameters to a SSRS report via VB.NET and ASP.NET. This seems like it should be a relatively simple thing to do, but I haven't had much luck finding help on this.
Does anyone have any suggestions on where to go to get help with this, or perhaps even some sample code?
Thanks.
You can do the following,: (it works both on local reports as in Full Blown SSRS reports. but in full mode, use the appropriate class, the parameter part remains the same)
LocalReport myReport = new LocalReport();
myReport.ReportPath = Server.MapPath("~/Path/To/Report.rdlc");
ReportParameter myParam = new ReportParameter("ParamName", "ParamValue");
myReport.SetParameters(new ReportParameter[] { myParam });
// more code here to render report
If the Report server is directly accessible, you can pass parameters in the Querystring if you are accessing the repoort with a URL:
http://MyServer/ReportServer/?MyReport&rs:Command=Render&Param1=54321&Param2=product
You can add output formatting by adding the following on the end of the URL:
&rs:Format=Excel
or
&rs:Format=PDF
It's been a while since I did this code, but it may help:
Your web project has to be a Web Site, and not a project of type "ASP.Net Web Application", or you won't be able to add the reference mentioned below.
Right click on the project and add an ASP.Net folder - App_WebReferences. You'll have to specify the server where your SRS is; choose the .asmx.
Once it's added, the folder under that level is called RSService, and under that are 2 things: reportservice.discomap & .wsdl.
In my VB, I do Imports RSService and Imports System.Web.Services.Protocols, then...
Dim MyRS As New ReportingService
The reporting service is on a different server than the webserver the app is on, so I can't do the following: MyRS.Credentials = System.Net.CredentialCache.DefaultCredentials
Instead: MyRS.Credentials = New System.Net.NetworkCredential(rs1, rs2, rs3),
where the rs1/2/3 are the login to SRS box, password to SRS box, & domain name". (These are encrypted in my web.config.)
Then, a mass-paste:
MyRS.Credentials = New System.Net.NetworkCredential(rs1, rs2, rs3)
Dim ReportByteArray As Byte() = Nothing
Dim ReportPath As String = "/SRSSiteSubFolder/ReportNameWithoutRDLExtension"
Dim ReportFormat As String = "PDF"
Dim HistoryID As String = Nothing
Dim DevInfo As String = "<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>"
'Dim x As ReportParameter - not necessary
Dim ReportParams(0) As ParameterValue
ReportParams(0) = New ParameterValue()
ReportParams(0).Name = "TheParamName"
ReportParams(0).Value = WhateverValue
Dim Credentials As DataSourceCredentials() = Nothing
Dim ShowHideToggle As String = Nothing
Dim Encoding As String
Dim MimeType As String
Dim ReportHistoryParameters As ParameterValue() = Nothing
Dim Warnings As Warning() = Nothing
Dim StreamIDs As String() = Nothing
'Dim sh As New SessionHeader() - not necessary
''MyRS.SessionHeaderValue = sh - not necessary
ReportByteArray = MyRS.Render(ReportPath, ReportFormat, HistoryID, DevInfo, ReportParams, Credentials, _
ShowHideToggle, Encoding, MimeType, ReportHistoryParameters, Warnings, StreamIDs)
'(Yay! That line was giving "HTTP error 401 - Unauthorized", until I set the credentials
' as above, as explained by http://www.odetocode.com/Articles/216.aspx.)
'Write the contents of the report to a PDF file:
Dim fs As FileStream = File.Create(FullReportPath, ReportByteArray.Length)
fs.Write(ReportByteArray, 0, ReportByteArray.Length)
fs.Close()
Call EmailTheReport(FullReportPath)
If IO.File.Exists(FullReportPath) Then
IO.File.Delete(FullReportPath)
End If
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.Reset();
Label1.Visible = false;
ReportViewer1.Visible = true;
DataSet dataSet = new DataSet();
dataSet = new ClassBLL().Load_Report_Detail(TextBox1.Text,
ddlType.SelectedValue, levelcode, fields);
ReportDataSource datasource = new ReportDataSource("DataSet_StoreprocedureName",
dataSet.Tables[0]);
if (dataSet.Tables[0].Rows.Count == 0)
{
ReportViewer1.Visible = false;
}
ReportViewer1.LocalReport.ReportPath = Server.MapPath("") + #"\Report.rdlc";
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(datasource);
string fields="name,girish,Z0117";
string[] filedName = fields.Split(',');
ReportParameter[] param = new ReportParameter[2];
//for (int i = 0; i < filedName.Length; i++)
//{
param[0] = new ReportParameter(filedName[0], filedName[0], true);
param[1] = new ReportParameter(filedName[3], filedName[3], true);
// }
ReportViewer1.LocalReport.SetParameters(param);
ReportViewer1.ServerReport.Refresh();

Resources