Some Sharepoint controls sadly behave different on postback:
I started with:
<SharePointWebControls:NoteField id="3" FieldName="MultiText" runat="server" />
<PublishingWebControls:RichLinkField id="4" FieldName="Link" runat="server" />
These controls render   for blank fields. That lead to unwanted whitespace on postback.
After some research I found a solution like
suggested here :
<PublishingWebControls:EditModePanel runat=server id="EditModePanelView" PageDisplayMode="Display" SuppressTag="true">
<SharePointWebControls:FieldValue id="3" FieldName="MultiText" runat="server"/>
<SharePointWebControls:FieldValue id="4" FieldName="Link" runat="server" />
</PublishingWebControls:EditModePanel>
<PublishingWebControls:EditModePanel runat="server" id="EditModePanelEdit">
<SharePointWebControls:NoteField id="7" FieldName="MultiText" runat="server" />
<PublishingWebControls:RichLinkField id="8" FieldName="Link" runat="server" />
</PublishingWebControls:EditModePanel>
...no   in blank fields anymore:
In Edit-Mode the specific WebControls are used.
In display mode of the page, there is used SharepointWebControls:FieldValue to simply render the content.
Now a new problem occured:
Any line breaks (rendered as br-Tags) that are entered in edit mode for a NoteField are lost, when rendered as SharePointWebControls:FieldValue. Is there any way to render the html of the NoteField as is in display mode? So that the breaks don't get lost and there are no additional non-breaking spaces?
I ended up with overwriting the Sharepoint Controls like suggested here:
I had to overwrite NoteField#RenderFieldForDisplay like this, so that NoteField behaves the same on postback and on GET:
protected override void RenderFieldForDisplay(HtmlTextWriter output)
{
TextWriter writer = new StringWriter();
base.RenderFieldForDisplay(new HtmlTextWriter(writer));
string x= writer.ToString();
string y= " ";
string z= "<br/>";
x= x.Equals(y) ? x.Replace(y, string.Empty) : x.Replace(" ", y).Replace("\r\n", z).Replace("\n", z).Replace("\r", z);
output.Write(x);
}
This works but it sucks. I still don't get it why sharepoint controls behave different on postback and replace e.g. br-Tags with line breaks...
Related
I have an ASP.NET WebForms with a Label.
On .aspx file I have:
<asp:label id="message" runat="server" />
And on aspx.cs I'm doing:
message.Text = $"Thing {AntiXssHelper.HtmlEncode(thing.name)} was saved.";
Every thing.name value is perfectly encoded except for this: Alert, with this case, I get the link (which I shouldn't)
Does anyone knows what can be going wrong here?
Thank you!
I would like to start off by saying that I am extremely new to ASP.NET.
I have a piece of ASP.NET code with this line in the Default.aspx file:
<asp:Image ID="my_awesome_image" ImageUrl="images/my_awesome_image.png" runat="server" Visible="<%# MyAwesomeImageVisibility %>" />
I have this section of code in the Default.aspx.cs file:
[System.ComponentModel.Bindable(true)]
public bool MyAwesomeImageVisibility {
get { return false; }
set {}
}
This is the only code I have for this image, the one line in the .aspx file and that section of code in the .cs file ... absolutely nothing else regarding this image.
When I run my application, though, the image is still visible, even though I have it returning false.
If I change the Default.aspx file to this:
<asp:Image ID="my_awesome_image" ImageUrl="images/my_awesome_image.png" runat="server" Visible="false" />
My image is, indeed, hidden.
I will eventually be pulling a value from the database to determine whether or not this image is visible, but for now, I just want to see if I can get the visibility to work from a boolean.
your code set visible property after aspx is translated in html.
As a result, it will add visible="false" to dom element generated by <asp:Image ... /> ie <img ... visible="false" /> .
Here is my suggestion :
in Default.aspx
<asp:Image ID="my_awesome_image" ImageUrl="images/my_awesome_image.png" runat="server" />
in Default.aspx.cs (for exemple : inside page_load event function) :
my_awesome_image.visible = false;
My idea
When the user click on the a tag with his avatar, he must redirect to another page. I do this with the code by number one (see below).
<div>
<!--show new messages | only show when log in. -->
<a href="<%=ResolveUrl("~/messages/inbox.aspx") %>" class="click headeritem" id="messages">
<img src="<%=ResolveUrl("~/images/message.png") %>" alt="new messages" id="messages" />
<asp:Label class="number" id="lblNewMessages" runat="server">1</asp:Label>
</a>
<!--log in | only show when log out. -->
<div class="user" id="logOut" runat="server">
Log in
Regist
</div>
<!--go to the profile of the user | only show when log in. -->
<!--1-->
<a class="click user" id="logIn" href="<%=ResolveUrl("~/gebruiker.aspx") %>">
<img id="picture" src="<%=ResolveUrl("~/afbeeldingen/person-male.png") %>" alt="user" />
<asp:Label runat="server" id="points" class="points">10</asp:Label>
</a>
</div>
With this C# code I place some tags invisible dependent on log in or out.
if (Request.Cookies["user"] != null) // log in
{
FindControl("logOut").Visible = false; // 2
}
else // log out
{
FindControl("logIn").Visible = false; // 2
FindControl("messages").Visible = false;
}
Extra information about the code: If you are login, I place a cookie with the user's id. If the cookie is not null, the user is login, other ways not. If you are login, it place the a-tag with id logout unvisible.
My problem
Now this code will give a NullReferenceException on line two.
Additional information: Object reference not set to an instance of an object.
If I place runat="server" to the a-tags, it give me this:
Server Labels should not contain <% ... %>-constructs.
There is an <% ... %>-constructor added on the a-tag in the code above for get the correct url to go to the correct page.
This is my problem. You can not add a <% ... %>-constructor where runat="server" stand. How can you do it on a correct way?
Other information
Maybe also important to know is that my project has sub directories. It must be important to go from messages/inbox.aspx to user/profile.aspx for eg.
All this code above is added to a master page that I use for all the pages.
Can anyone help me? Thanks and sorry for my poor English.
Instead of using plain a-tags, you could use WebForm-controls like Panels or Hyperlinks, e.g.:
<!--log in | only show when log out. -->
<asp:Panel CssClass="user" id="logOut" runat="server">
<asp:HyperLink NavigateUrl="~/gebruikers/aanmelden.aspx" CssClass="click" id="logIn" Text="Log in" runat="server" />
<asp:HyperLink NavigateUrl="~/gebruikers/registreren.aspx" CssClass="click" id="regist" style="left:100px" Text="Regist" runat="server"/>
</asp:Panel>
This might reduce the amount of control you have over the generated HTML (so you'd have to check whether the HTML is good for you), but would enable you to access the controls in Code behind more easily, e.g.:
if (Request.Cookies["user"] != null) // log in
{
logOut.Visible = false; // 2
}
else // log out
{
logIn.Visible = false; // 2
messages.Visible = false;
}
There are a few different varieties of ASP.net inline tags. Please see the full list here: https://support.microsoft.com/en-us/kb/976112
Not all of them support placement inside the attributes of a server-side control's tag. The <%# ... %> data-binding expression inline format would allow you to do that, and I think the older <% ... %> format also. The <%= ... %> inline tag will definitely not work inside the server-side control tag because the whole expression is directly compiled instead of displaying the content as an attribute value.
If your main goal is controlling visibility of a server-side control, then you should just be able to set control.Visible = false; in your code-behind. If you'd like to control visibility of a non-server-side control (or a block of controls), then the <asp:Panel> server-side control might be your best route. ASP.net has tried to move away from the excessive inlining approach of the old ASP.
I used to get errors similar to the one you specified. Because the ResolveUrl uses "", avoid using that for the HREF attribute too as it might break the code. Try the below code:
<a href='<%=ResolveUrl("~/messages/inbox.aspx") %>' class="click headeritem" id="messages">
<img src="<%=ResolveUrl("~/images/message.png") %>" alt="new messages" id="messages" />
<asp:Label class="number" id="lblNewMessages" runat="server">1</asp:Label>
</a>
Note: This is an asp.net page but the XSLT Transformation is occurring client-side.
So I am trying to set a default value in an asp.net textarea and it is being escaped which is causing me problems
Here is the asp.net text area
<asp:TextBox id="Description" TextMode="MultiLine"
Columns="50" Rows="4" runat="server"
ClientIDMode="Static" CausesValidation="false">
<xsl:value-of select="/oohru/form/desc" />
</asp:TextBox>
On the page it becomes
<textarea id="Description" cols="50" rows="4"
name="ctl00$RightColumn$Description">
<xsl:value-of select="/oohru/form/desc" />
</textarea>
Putting in the text area the literally
<xsl:value-of select="/oohru/form/desc" />
I'd like to get the non escaped value in there.... if i Just use a regular text area like
<textarea rows="5" cols="5">
<xsl:value-of select="/oohru/form/desc" />
</textarea>
It works fine.... How can I accomplish this with an ASP.NET control? It's basically breaking my clientside xslt but ONLY on the textarea...
Thanks!
Note: I also did try using the text="{/oohru/form/desc}" inside of the text area... same thing the text area contained that exact oohru/form/desc rather than the referenced value.
You seem to be trying to use XSLT too-late.
My guess is that you want to generate the control with XSLT.
In this case inside your XSLT transformation you'll have:
<asp:TextBox id="Description" TextMode="MultiLine"
Columns="50" Rows="4" runat="server"
ClientIDMode="Static" CausesValidation="false">
<xsl:value-of select="/oohru/form/desc" />
</asp:TextBox>
where you'll also need to bind the prefix asp: to some namespace.
Alternatively, within the XSLT transformation you can generate the textarea directly:
<textarea rows="5" cols="5">
<xsl:value-of select="/oohru/form/desc" />
</textarea>
Final note: If my guesses are wrong and you just want the end-user to see in the textarea the string "<xsl:value-of select="/oohru/form/desc" />", then it doesn't matter that you see it(before being displayed by the browser) escaped -- when the browser displays it, the user will see the unescaped text.
Update: #Jordan has further clarified his transformation is client-side -- way after the asp control has evaporated...
In this case the answer is: No, you cannot generate with the asp:TextBox control any markup (node other than a text node) inside the textarea-- it only generates text inside it.
Therefore, you have to generate explicitly the textarea and the <xsl:value-of> on the server.
You can bind the Text property of TextBox control to this value in code-behind. That might work. BTW what is xsl and what you want to be printed.
XSL and ASP.NET aren't really friends.
You can try something like what you'll find here, but if you can - port your xsl to a resource file.
how there,
I've these codes in my DATABASE in other words it's HTML. I tried these stuff:
<div runat="server" id="div1" visible="false">
<asp:Literal ID="literal1" runat="server" Text="" />
</div>
I tried in C# code behind:
div1.InnerText = contents;
div1.InnerHtml = contents
literal1.Text = contents;
But is still doesn't render well. I displays the original values in stead of a table and cells and columns. colours etc. etc....
What am I missing?
All these HTML's are in DABASE.Column e.g. column "Contents"
e.g.
"& lt;p class=& quot;MsoNormal" style= "color: #339966;"><"
;" ;> ;< ;strong > ;&l
ot; > ;& ;nbsp; < ;/span >< ;/p >
Can someone please advice? what I'm I misssing?
I've put (spaces between & and gt above code otherwise it was not showing in stackoverflow.) The HTML sysntaxs are correct because it's created by an HTMLEDITOR.
use
literal1.Text = this.HtmlDecode(contents);
Try response.Write(Contents)
And take a look at HtmlDecode
Are you saying that ;> ;< etc is displaying on the browser, or only when you view the source?