Cannot get inner content of '' because the contents are not literal - asp.net

I am trying to iterate over the contents of a Repeater containing an html table, and use the contents of each cell. This works fine for standard HTML components, but blows up when I try to use a control. The code below will print the value of the first cell, "Item #1", but will throw an HttpException when trying to access .InnerText of the second cell. The error is as follows:
Cannot get inner content of because the contents are not literal.
I have tried to use RenderControl via this solution found elsewhere, which failed for two reasons; 1) it rendered the entire HTML of the first cell and 2) it still blew up when trying to access the second cell with the following message:
'LinkButton1' of type 'LinkButton' must be placed inside
a form tag with runat=server
Is there an easy way to get the LinkButton text I'm after? Repeater code and C# can be found below.
The repeater code:
<asp:Repeater ID="Rep1" runat="server">
<HeaderTemplate>
<table id="Table1" class="data">
</HeaderTemplate>
<ItemTemplate>
<tr id="Row" runat="server">
<td>Item #1</td>
<td><asp:LinkButton ID="LinkButton1" OnClick="DoSomething"
Text="Item #2" runat="server" /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
The C#:
foreach (RepeaterItem item in Rep1.Items)
{
HtmlTableRow row = item.Controls[0].FindControl("Row") as HtmlTableRow;
foreach (HtmlTableCell cell in row.Cells)
{
if (cell.InnerText != string.Empty)
{
Console.WriteLine(cell.InnerText);
}
}
}

For anyone else who arrives here:
You cannot get InnerHtml or InnerText on a Control unless its contents are literal, i.e. there are not any server controls or controls with runat="server" inside it that required rendering

Iterate/recurse through any child controls in the Controls collection. When you get to a leaf node (no children), then access that control's InnerText property.

RenderControl C#
StringWriter iSW = new StringWriter();
HtmlTextWriter iHTW = new HtmlTextWriter(iSW);
iDiv.RenderControl(iHTW);
string iS = iSW.GetStringBuilder().ToString();
Include
public override void VerifyRenderingInServerForm(Control control)
{
return;
}
RenderControl VB
Dim iSW As New StringWriter
Dim iHTW As New HtmlTextWriter(iSW)
iDiv.RenderControl(iHTW)
Dim iS As String = iSW.GetStringBuilder().ToString()
Include
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
Return
End Sub

Also, make sure that you do not have anything such trying to access another view control while another view control is activated in a multi view control.

string value = ((Literal)(cell.Controls[0])).Text

Related

Add rows to a table dynamically

At the moment I am trying to create a table whose content is supposed to be created by a subclass (result of querying a RESTful web service). I have been working for quite some time on that now and I just cannot seem to get it to work. I have tried so many different solutions.
creating the table in the subclass and add it to Page.Controls. That gets me "The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)." which does not make sense at all.
I have tried to create an empty table on the page and passed its handle to the subclass which was responsible for adding rows. Noting happened.
I have tried to return a TableRowCollection and assigned it to the previously created (empty) table. Nothing happened.
Now I just want to add one row and one cell to the table (baby steps towards what I need). Not even that works. Please find the code for that attached:
TableRow row = new TableRow();
table.Rows.Add(row);
TableCell cell = new TableCell();
row.Cells.Add(cell);
cell.Controls.Add(new TextBox());
The table is simple and empty:
<asp:Table ID="table" runat="server" />
The source code that is displayed by my browser looks like that:
<table id="table">
</table>
I have been looking at countless examples on the web and all look like this. I guess it is only a tiny problem somewhere but I have not been able to figure it out. Now I am willing to offer life-long gratitude to everybody who can provide the hint for solving this mess.
It is working, I have tested it:
In my page load, I have:
protected void Page_Load(object sender, EventArgs e)
{
TableRow row = new TableRow();
TableCell cell = new TableCell();
cell.Controls.Add(new TextBox());
row.Cells.Add(cell);
table.Rows.Add(row);
}
On my aspx page, I have:
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:Table ID="table" runat="server" />
</asp:Content>
The html rendered by this code:
<table id="MainContent_table">
<tbody><tr>
<td><input name="ctl00$MainContent$ctl00" type="text"></td>
</tr>
</tbody></table>
I would use either an asp:GridView or an asp:Repeater
eg with Repeater
<table>
<asp:Repeater id="repeater1" runat="server">
<ItemTemplate>
<tr>
<td><asp:Literal id="literal1" runat="server" /></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
then in your code behind
repeater1.DataSource = myDatasource;
repeater1.DataBind();
or you could use a GridView
using table.Rows.Add() will tend to cause you problems, particularly with disappearing content on postback, or problems with event handlers not firing should you need to add any LinkButtons or anything like that to your table cells
The question is where are you adding these rows in the table, I mean in which event of page?
As I feel that post back is clearing all the added rows.
Kindly tell the execution sequence and also try to put your code in page_init event for adding rows.
Might solve your problem.
Make table runat="server" in your aspx code
<table id="tbl" runat="server">
</table>
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
TableRow tr = new TableRow();
TableCell tc = new TableCell();
TextBox txtBox = new TextBox();
// Add the control to the TableCell
tc.Controls.Add(txtBox);
// Add the TableCell to the TableRow
tr.Cells.Add(tc);
// Add the TableRow to the Table
tbl.Rows.Add(tr);
}
}

find control in listview

By pressing button in GridView i need to find control in Listview.
<ItemTemplate>
<td>
<asp:Label ID="lblMarketLVBalanceHeader" runat="server" Text="Balance: "></asp:Label>
</td>
<td>
<asp:Label ID="lblMarketLVBalanceValue" runat="server" Text='<%# Bind("Money", "{0:####}$") %>'></asp:Label>
</td>
</ItemTemplate>
Code Behind:
protected void GVMarketItems_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Buy"))
{ GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
/* string itemID = row.Cells[0].Text;
string itemName = row.Cells[1].Text;
string itemCostStr = row.Cells[3].Text; */
string curBalanceStr = ((Label)LVMarketBalanceShow.FindControl("lblMarketLVBalanceValue")).Text;
}
Code seems find, but i have "Object reference not set to an instance of an object" when i'm trying to Find control.
string curBalanceStr = ((Label)LVMarketBalanceShow.FindControl("lblMarketLVBalanceValue")).Text;
Did the same to DetailsView and it was Ok.. What's wrong with ListView?
UPD: Trying to get first fow of listview
ListViewDataItem curBalanceLV = LVMarketBalanceShow.Items[0];
string curBalanceStr =((Label)curBalanceLV.FindControl("lblMarketLVBalanceValue")).Text;
But getting an error "Index was out of range."
I think you want to find the control within the specific row.
string curBalanceStr = ((Label)row.FindControl("lblMarketLVBalanceValue")).Text
Did the same to DetailsView and it was Ok.. What's wrong with
ListView?
A DetailsView is used to tackle with one row at a time so you can directly call FindControl() on the detailsview but gridview and listview are meant to display multiple records and your markup you define inside <ItemTemplate /> is just a template for each row. You'll be able to find the controls that you define in the template inside of each row.

Dynamically change the number of header cells in listview LayoutTemplate

I'm trying to dynamically create a listview. on reports.aspx user selects a bunch of checkboxes. On the next page, user sees reports.aspx, and should see a table with columns of the checkboxes he selected. My idea was to create a listview, then dynamically change the header row of the LayoutTemplate, then change the select statement depending on which columns selected. This is what I have:
<asp:ListView runat="server" ID="ReportListView" DataSourceID="ReportListViewSDS">
<LayoutTemplate runat="server">
<table runat="server">
<tr runat="server">
<%
' Doesn't work because code blocks (<%%>) aren's allowed inside <LayoutTemplate> blocks
'For Each i As String In Request.Form
'Response.Write("<th>" & Request.Form(i) & "</th>")
'Next
%>
</tr>
</table>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
</LayoutTemplate>
...
Problem is that this doesn't work because i can't put a code block (<%%>) inside the LayoutTemplate. Is there a way in the code behind to edit the LayoutTemplate, or another way to cycle through the Request.Form vars and build the table header row with it?
Thanks for any advice/direction!
-Russ
Try using the ItemTemplate for the binding syntax instead of the layout template. I believe the layout template is strictly for layout.
Also, it looks like you're using classic ASP code blocks. ASP.NET code blocks look like this:
For data binding:
<%# Eval("<COLUMN NAME>")%>
For other cases not involving data binding:
<%= Request.QueryString["Hello"] %>
Since the control is already a server side control, try giving the and id and then modifying the header on pre-render:
<asp:ListView runat="server" ID="ReportListView" DataSourceID="ReportListViewSDS">
<LayoutTemplate runat="server">
<table runat="server">
<tr id='trCustomHeader" runat="server">
Then in your code behind, attach this logic to the listview's pre-render
ReportListView_PreRender(...)
{
TableRow tr = ReportListView.FindControl("trCustomerHeader");
TableCell tempCell = new TableCell();
tempCell.Text = ...
tr.Cells.Add(tempCell);
}
I just created a separate table on the page, outside of the listview, that was the simple way of doing it.
<asp:Table ID="HeaderTable" runat="server">
<asp:TableHeaderRow ID="HeaderTableHeaderRow" runat="server" />
</asp:Table>
<asp:ListView ...>
...
</asp:ListView>
Then in the code behind:
For Each i As String In Request.Form
If i.IndexOf("checkbox_") = 0 Then
Dim c As New TableHeaderCell()
Dim l As New LinkButton()
l.Text = i.Substring(Len("checkbox_"))
c.Controls.Add(l)
c.CssClass = "customreport"
HeaderTableHeaderRow.Cells.Add(c)
End If
Next
Pretty simple. So I didn't have to use a LayoutTemplate at all.

How to get user input from dynamically created form

I am building an ASP.NET website that allows users to create and take tests. Tests can contain various types of questions (multiple choice, true/false, essay, etc.). Because of the dynamic nature of the tests, I am creating the "Take Test" page with repeaters.
My problem now is: how can I get the user's answers? With a fixed number/type of questions this would be simple, but I'm not sure how to grab answers from items with dynamically created IDs or how to pass a variable number of answers back to my database.
Edit:
I found my answer here.
You can use Request.Form
But Here is Another approach using FindControl and Repeater:
For Each item As RepeaterItem In Me.RptItems.Items
Dim value = CType(item.FindControl("TxtName"), TextBox).Text
Next
you can use FindControl method with each RepeaterItem and find desired control inside it by ID.
ASPX file:
<asp:Repeater ID="RptItems" runat="server">
<HeaderTemplate>
<table>
<tr>
<td>
Name
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:TextBox ID="TxtName" runat="server" Text='<%# Eval("Name")%>'></asp:TextBox>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
If you are using Asp.Net MVC you can reference this article. Model Bind To Collection
If its webforms you can always access each of the inputs submitted via the Request.Form collection.
Here is what I ended up doing for each question type, based on links including this one.
foreach (RepeaterItem item in myRptr.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandText = "myPackage.myProcedure";
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add("user_id", OracleType.VarChar).Value = Session["UserId"].ToString();
cmd.Parameters.Add("question_id", OracleType.Number).Value = ((HiddenField)item.FindControl("myHidden")).Value;
cmd.Parameters.Add("answer", OracleType.VarChar).Value = ((TextBox)item.FindControl("myTxt")).Text;
cmd.ExecuteNonQuery();
}
}

Object reference not set to an instance of an object (NullreferenceException was unhandled by user code)

How can I get around this exception?
Dim imagepathlit As Literal = DownloadsRepeater.FindControl("imagepathlit")
imagepathlit.Text = imagepath
Here is the repeater:
<asp:Repeater ID="DownloadsRepeater" runat="server">
<HeaderTemplate>
<table width="70%">
<tr>
<td colspan="3"><h2>Files you can download</h2></td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td width="5%">
<asp:Literal ID="imagepathlit" runat="server"></asp:Literal></td>
<td width="5%"></td>
<td> </td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
Here is the code that gets the data for the repeater:
c.Open()
r = x.ExecuteReader
While r.Read()
If r("filename") Is DBNull.Value Then
imagepath = String.Empty
Else
imagepath = "<img src=images/" & getimage(r("filename")) & " border=0 align=absmiddle>"
End If
End While
c.Close()
r.Close()
My guess is that there is no control found in the DownloadsRepeater control called imagepathlit, therefore the imagepathlit control is null after the call.
Remember that Control.FindControl() looks up the control based on ID, not the name of the control. Therefore, to find the control in the collection...you would have to had something like this earlier in the application:
Dim imagepathlit As Literal = new Literal()
imagepathlit.ID = "imagepathlit"
UPDATE
Since you're using a repeater, the child controls get layed out a bit differently. You're going to have an instance of the Literal for each Item in the Repeater. Therefore, to get each instance of the control, you have to loop through the Items in the Repeater and call FindControl() on each Item:
For Each item As Item In DownloadsRepeater.Items
Dim imagepathlit As Literal = item.FindControl("imagepathlit")
Next
Assuming the code you posted is where the exception is indeed thrown, I would say that the DownloadRepeater does not have a control in it that has an ID of imagepathlit.
Check your aspx.
Because the control is within the ItemTemplate, you cannot use repeater.findcontrol; you have to loop through the items of the repeater to look for the control, as the itemtemplate is repeatable. So you have to loop through each one to look for the control as in:
foreach (var item in repeater.Items)
{
var control = item.FindControl("ID") as Type;
}
Use that syntax.

Resources