Call MVC Html helper extension in ASPX page - c#

I am working on the MVC project in which one of the aspx page also exists for SSRS reports. Here, I have a HTML helper written for returning some multi-lingual text value. So, For all the report labels, I need to call HTML helper extension to get the string text. Is there any way that I can call that method from my aspx page??
Note: I cannot do this from Code behind since all the labels exists in aspx that I do not want to migrate it to code behind.
So far I tried
<%# Import Namespace="Vibrant.HtmlHelperExtension" %>
<asp:Label Text="<%# ReturnKeyValue("Brief") %>" meta:resourcekey="lblBrief" ID="lblBrief" CssClass="label"></asp:Label>
The above one is not able to call the method and I am getting The name 'ReturnKeyValue' does not exist in the current context exception.

This is what I found. Maybe you can try.
http://www.asp.net/mvc/overview/older-versions-1/views/creating-custom-html-helpers-cs
HTML Helper is rendered with <%= %> tags instead of <% %> tags. If you
don't include the equal sign, then nothing gets rendered to the
browser.

Related

how to read html file and render it inside an asp.net page

I was wondering if there is any way in C# to create a function which would call a preset html if the requirements are fulfilled.
Something like if url=domainname/test get <P>This is a test page</p>
you can use this command inside aspx page
<!--#include virtual ="test.html" -->
or make a redirect to test.html
Response.Redirect("test.html");
or make a transparent redirect
Server.Transfer("test.html");
or make a function that reads the html file and render it to the page.
txtTest.Text = File.ReadAllText(Server.MapPath("test.html")).ToString();
where txtTest is <asp:Literal runat="server" ID="txtTest"></asp:Literal> literal control on aspx page.
To make the include command inside aspx page you have to write it as
<%
if(Request.Url.ToString().Contains("http://url"))
{
%>
<!--#include virtual ="test.html"-->
<%
}
%>

CKeditor on Textarea implementation

I have implemented a CKeditor from the below link:-
CKEditor But the issue is that, As soon as I register the editor on my page, it gets reflected. I want the same editor just only for my asp.net textbox. What should I do and make change so that It can only be visible to my textbox only. Please help.
See my textbox
<asp:TextBox ID="txtPostdesc" CssClass="form-control" runat="server" ValidationGroup="AddNew" TextMode="MultiLine"></asp:TextBox>
As per the article CKEditor in ASP.Net it described the way of implementing CKEditor with dll.
You would require following things
1. Two dll : CkEditor.dll and CKEditor.NET.dll.
2. CKEditor folder containing all js, css and images.
Register the CKEditor control at the top of your .aspx page such as
<%# Register Assembly="CKEditor.NET" Namespace="CKEditor.NET" TagPrefix="CKEditor" %>
Now you will be able to write the CKEditor server control markup such as below
<CKEditor:CKEditorControl ID="txtPostdesc" BasePath="/ckeditor/" runat="server">
</CKEditor:CKEditorControl>
In above I just change the ID as per your textarea ID. Now you can set and get its content via .Text Property in your code behind file i.e.
string str = txtPostdesc.Text;
Hope above explanation works for you.

Locate HTML Element when it does not have the runat="server" attribute

I have an ASP.NET C# website and I am attempting to retrieve the inner html from a HTML in my page when a button is clicked.
My Problem: I use the function FindControl() but it returns null when it should find the element. Why is this happening, what am I doing wrong?
My HTML page:
<%# Page Title="test" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="test.aspx.cs" Inherits="test" %>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="ActualContent">
<div class="regMainContainer mainContainer container">
<!-- Attempting to get the below div -->
<!-- Knowtice it does not have runat="server",
I would prefer not to do that if I dont need to -->
<div id="courseDiv" class="courseContainer">
</div>
</div>
</asp:content>
My Code that executes when a button is clicked(inside test.aspx.cs file:
HtmlGenericControl d = (HtmlGenericControl)FindControl("courseDiv");
FindControl can only find controls that has runat="server" attributes. The good news is that you can easily change you div to a panel, and add data that way.
the bad news is that this still won't let you access the innerhtml of the div, .net sees it as just a container for other controls.
The best way to do this would be some form of ajax, since your client side script would be able to read that contents and pass it to a server side method
You won't be able to use FindControl for something that's not marked with runat="server". This is because any of the elements not marked with runat="server" get parsed as one giant text literal at runtime. You can add runat="server" to any arbitrary html element, though.

MVC2 C# - Why I cannot assign a Model.ImageUrl to a img src attribute in a UserControl?

I'm starting with MVC 2 in Visual Studio 2010. The thing is I'm working with a MasterPage where I'm rendering an action which returns a UserControl:
Everything works well, I mean the user control has a lot of other labels and it's rendering other properties... So the problem has nothing to do with anything apart the img tag and the src attribute:
Site.master:
<div class="Test">
<% Html.RenderAction("HeaderDetails", "User"); %>
</div>
HeaderDetails.ascx:
<img src="<%= Model.ImageUrl %>" />
For my surprise I cannot use the Model inside that attribute in a usercontrol, I have the same for a Page.aspx that is working.
The same happens with ViewData["ImageUrl"], I just don't have even intellicense in that attribute. It's like: I cannot do that.
Does anyone know why is that?, or how should I do it?
I kind of got lost towards the end of your question, but in regards to the strongly typed Model in the user control, you'll need your page, masterpage and user control to all inherit with the same type arguments
page:
<%# Page Language="C#" MasterPageFile="Your.Master" Inherits="System.Web.Mvc.ViewPage<YourModelType>" %>
master:
<%# Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<YourModelType>" %>
control:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<YourModelType>" %>
The type arg of the control and master can be base types of YourModelType, but obviously will need to have the ImageUrl property
Well, I have solved it by creating a Custom HtmlHelper using Extension Methods going through this link.
The only matter is that I couldn't use the namespace MyMVCApp.Helpers, I had to use the System.Web.MVC one, so I'm not sure that would be the best way to do it.
If you have some input on that I will appreciate it.
So, I was missing the Import to MyMVCApp.Helpers inside the user control :D

Creating clickable links in runtime of c# aspx webform

I'm creating asp links using response.write in c#, the same HyperLink code works smoothly when inserted directly in the asp code, but when i copy/paste it to the response.write("...") it appears as an unclickable black text.
Am i forgetting something?
<asp:HyperLink ID='HyperLink1' runat='server' NavigateUrl='Exibe.aspx'> CLICK HERE </asp:HyperLink>
this exact code above thrown in the aspx source works greatly
response.write("<asp:HyperLink ID='HyperLink1' runat='server' NavigateUrl='Exibe.aspx'> CLICK HERE </asp:HyperLink>");
and this turns into a black text
You cannot insert an asp:Hyperlink tag directly into the response stream like that, as the hyperlink is actually a control that needs to "render" itself (if you replaced that with a normal "a" anchor/hyperlink tag it would work fine).
Instead you need to either create the control and add it to the page programatically, or maybe use a repeater control to render the anchors.
You are trying to do totally different things:
the markup (asp:HyperLink) will be compiled.
the Response.Write("asp:HyperLink") will NOT. It will render text as is, and of course you wont't see any link, in fact you should see the text inside the tag asp:HyperLink (inluding the tag itself in the HTML source).
If you want to create a link dunamically you can do it using code snippets below:
<asp:HyperLink ID='HyperLink1' runat='server' NavigateUrl='<%= GetDynamicUrl() %>'> CLICK HERE </asp:HyperLink>
/// Or plain HTML
<%= GetTheLinkText() %>
If you want to generate a hyperlink dynamically on the server-side like this, you can either use Response.Write with an <a> tag like slugster says, or alternatively consider the ASP:Literal control which renders exactly what you give it even if it contains markup e.g.
In your markup:
<asp:literal runat="server" id="MyLiteral" />
In your code:
string myHTMLFragment;
myHTMLFragment = "Hello. I am a link pointing to StackOverflow";
MyLiteral.Text = myHTMLFragment;

Categories