Access to events? - asp.net

i have a question about generating images at runtime which are also links. Ok, so what i was going to do was create an ImageButton, and then set the onClick event to something such as:
window.open('http://www.themagicfinger.com/')
Which means it would look like:
newImage.Click += (window.open('http://www.themagicfinger.com/'));
But then i realised that this doesnt work, because it looks for a method which matches the event delagate.
So my question is, is this the best way to achieve what i want, and if so, how can i make it work?
Another option that came in my head would be to wrap the image in an tag, but i think this way its better.
Thanks

What you need is OnClientClick:
<asp:ImageButton id="ImageButton1" runat="server" OnClientClick="window.open('http://www.themagicfinger.com/'); return false;" ImageUrl="MyButton.png" />
This property is a string, not event, and you can also assign it from within the code behind:
ImageButton1.OnClientClick = "window.open('http://www.themagicfinger.com/'); return false;";

If you're asking how to use server-side code to tell the client to open a browser window, you can't. The server-side code has no means of telling the client to do that. The only thing you'd be able to do from server-side code is emit client-side that does this. And in that case, why use the server-side code at all?
Using jQuery for example, you can attach the click event like this:
<!-- page header (including loading jQuery), other stuff that's before the image, etc. -->
<img src="/path/to/image" alt="some text" id="imgOpenWindow" />
<!-- the rest of the page -->
<script type="text/javascript">
$(document).ready(function() {
$('#imgOpenWindow').click(function(){
window.open('http://www.themagicfinger.com/');
});
});
</script>
If for some reason you need to use an ImageButton control for this then it would look like this:
<!-- page header (including loading jQuery), other stuff that's before the image, etc. -->
<asp:ImageButton run="server" id="imgOpenWindow" ImageUrl="/path/to/image" AlternateText="some text" />
<!-- the rest of the page -->
<script type="text/javascript">
$(document).ready(function() {
$('#<%= imgOpenWindow.ClientID %>').click(function(){
window.open('http://www.themagicfinger.com/');
});
});
</script>
But I'm not sure what the behavior would be with the post-back. It might override the JavaScript handler (by posting back before your handler is executed) or post-back after the window opens, etc. I would consider that behavior to be undefined and unreliable. An Image control will do the trick, though, if you don't need the button post-back capabilities.

Related

ASP.NET URL Navigation

I have asp.net button and on it's button click I am redirecting it using
Response.Redirect ("SomeURL.aspx");
I am not passing anything to SomeURL.aspx. Can this be achieved without a roundtrip to the server?
You could use an html anchor tag. This is the simplest approach and probably the best since anchors are the proper control to allow navigation.
My link
If you still want to use the asp.net button you could do something like this
<asp:Button runat="server" ID="myButton"
OnClientClick="window.location.href='SomeURL.aspx'; return false;"
Text="Submit"></asp:Button>
You can try with this code - based on Javascript Navigate
window.navigate("SomeURL.aspx");
Sample
<input type="button" value="Navigate to SomeURL" onclick="funcNavigate();">
<script language="JavaScript">
function funcNavigate() {
window.navigate("SomeURL.aspx");
}
</script>
Can this be achieved without a roundtrip to the server?
Not using code behind.
However, you could wire up a client side click handler or use a hyperlink to accomplish the same thing.
<button onclick="window.location='SomeURL.aspx'; return false;">Some URL</a>
or
Some URL
The hyperlink is the simplest answer.

Rich TextBox accepting Script tags also

I have a aspx form where we have used freetextbox as my rich text editor to make entries.
but I am able to enter <script></script> in this reach tags.
how can i validate on client side that it should not accept any scripting which ever it is included in javascript library.
EDIT:
how can i validate it for not allowing script tags into it?
how can we validate it for mandatory field?
How can i manage more than one textareas on my page. but i want only one to be as rich editor and not all???
I had tried some code but it did not helped me for proper validation. below is the snippet.
SCRITPT
<script>
$(document).ready(function () {
var $btn = $("#<%=btnSubmit.ClientID %>");
var $txtEditor = $("#<%=txtEditor.ClientID %>");
$btn.click(function () {
alert($txtEditor.html());
return false;
})
});
</script>
HTML
<div>
<asp:TextBox id="txtEditor" runat="server" TextMode="MultiLine"></asp:TextBox>
</div>
<div>
<asp:Button id="btnSubmit" runat="server" Text="Save" />
</div>
If you do need support HTML in your text, then you probably should integrate some WYSIWYG editor (TinyMCE for example).
If you don't need html there, then just html encode all user input.
I would consider using the HTML agility pack to parse the content and allow only white listed markup.
There are are numerous ways of getting script to execute in arbitrary markup (without necessarily needing <script> tags). Blacklisting isn't a viable solution if you're trying to avoid XSS attacks.

ASP.Net: Get page url from inside a frame

How can I get the top page URL from inside a frame?
(in javascript it's implemented using : window.top.location)
You can't.
The fact that the page is going to be loaded into an iframe in another page is not sent in the request to the server, so it's not possible to get the ULR of the parent page, or even to determine if there is a parent page or not.
If you need that information on the server side, you have to add that information to the request, for example by including the parent page URL as a querystring parameter.
In case people need it, there's a way for this certain purpose. At least I solved somehow.
First, place a button running at server, add a click event and a client-click event.
<asp:Button ID="btnFindParentUrl" runat="server" Text="Get Url!" OnClick="btnFindParentUrl_Click" OnClientClick="fillHidden();" />
Put a hidden textbox inside a div with style="display: none;" (not Visible="false" for button because if you do client can't see and fill it):
<div style="display: none;">
<asp:TextBox ID="txtHiddenUrlField" runat="server" BorderStyle="None" Font-Size="0px" ForeColor="#F6F6F6" Height="0px" Width="0px"></asp:TextBox>
</div>
Now place javascript code of fillHidden() function:
<script type="text/javascript">
function fillHidden() {
document.getElementById('<%= txtHiddenUrlField.ClientID %>').value = parent.document.location.href;
};
</script>
That's all you have to do at client side.
Let's go to the server:
protected void btnFindParentUrl_Click(object sender, EventArgs e)
{
string parentUrl = txtHiddenUrlField.Text;
}
This way, you should be getting parent url from button in an iframe.
Anyone needs help may ask question here or to this code's post in my personal site.
you can do this if the frame + parent are both on the same domain. if so, then you can obtain references to other iframes, frames or pop-ups.
from memory, try window.parent, so yours would be something like window.parent.top.location

asp:linkbutton (navigating to specific page section on post back)

I have some linkbuttons to update my gridview which is in the middle of the page. Everytime I hit edit or delete etc the window scrolls to the top on the page that gets posted back. I want it to stay focused on the grideview. I have tried a javascript function but for some reason it did not work.
(edit: the following works as far as scrolling is concerned but prevents postback)
here is what I tried
<script type="text/javascript" language="javascript">
function goto() {
window.scrollTo(10, 1100);
}
</script>
<asp:LinkButton ID="lbtnGo" runat="server" OnClientClick="javascript:goto();return false;">GO</asp:LinkButton>
source
How can I do this?
Did you try with <%# Page MaintainScrollPositionOnPostback="true" %> in the page declaration?
Regards
Client-side event fires before server-side. So even if you scroll window to correct position - after postback you will be returned to the top. You can add the following code to your server-side LinkButton click event handler:
if (!this.IsStartupScriptRegistered("ScrollToGrid"))
{
String scriptString = "<script language=\"JavaScript\">";
scriptString += "window.scrollTo(10, 1100);";
scriptString += "</script>";
this.RegisterStartupScript("ScrollToGrid", scriptString);
}
this will add javascript block to your page after postback
There are, depending on the .NET framework properties available that can help one out:
ASP.NET 1.x: use SmartNavigation. ASP.NET 2.0: use MaintainScrollPositionOnPostBack. Use an UpdatePanel control to asynchronously update parts of a page
and the best way for this is UpdatePanel control to asynchronously update parts of a page

Including Javascript with a custom control in an ASP.Net website

I have a custom date control which is essentially a text box and the ajaxToolKit calendarExtender. I want to include Javascript in the control and have it work properly no matter what page the control is on. The control is called DateControl.ascx
So I have two Javascript functions, dateEditor_OnShown and dateEditor_OnHiding. They get tied up in the page load of DateControl.ascx via...
CalendarExtender.OnClientShown = "dateEditor_OnShown";
CalendarExtender.OnClientHiding = "dateEditor_OnHiding";
The DateControl tool is used on two separate pages. If I put the straight Javascript directly into the DateControl's HTML it will work only on the default page but crashes when I load up the next page with the control. The error is a js runtime error 'dateEditor_OnHiding' is undefined.
If I try to link to the Javascript file from my DateControl's html via...
<script type="text/javascript" src="../JavaScript/IE6CalendarExtenderFix.js"></script>
... instead of having the Javascript directly in the page, it crashes immediately with the same error. I should note that the path to the js is correct.
The only way I can really get it to work is if I link to the javascript on every page that the control is used.
UPDATE: I feel the need to clarify a little bit. The solutions suggested are much appreciated, but either I am not understanding or they just will not work in my case for whatever reason (quite possibly the former).
So, this is basically what my control looks like...
<div id="CustomDateControl" style="<%# ControlStyle %>">
<div id="TextBox" style="display:inline; white-space:nowrap;">
<asp:TextBox runat="server" ID="txtCalender" Style="<%# TextBoxStyle %>" />
</div>
<div id="Calendar" runat="server">
<ajaxToolkit:CalendarExtender
runat="server"
ID="CalendarExtender"
Format="MM/dd/yyyy"
TargetControlID="txtCalender"
PopupButtonID="CalenderImage" />
</div>
</div>
In the aspx page, with that exact code, if I put the exact javascript in script tags so the page looks about like so...
<script type="text/javascript">
function dateEditor_OnShown(dateControl, emptyEventArgs) {
...
}
function dateEditor_OnHiding(dateControl, emptyEventArgs) {
...
}
</script>
<div id="CustomDateControl" style="<%# ControlStyle %>">
<div id="TextBox" style="display:inline; white-space:nowrap;">
<asp:TextBox runat="server" ID="txtCalender" Style="<%# TextBoxStyle %>" />
</div>
<div id="Calendar" runat="server">
<ajaxToolkit:CalendarExtender
runat="server"
ID="CalendarExtender"
Format="MM/dd/yyyy"
TargetControlID="txtCalender"
PopupButtonID="CalenderImage" />
</div>
</div>
This still crashes when accessing the control in the second page (not the first which is the default page) saying 'dateEditor_OnHiding' is undefined. Now, if I link to a js file with the same code using a relative path as suggested below I still get the same results.
Also, if as suggested below, I override OnPreRender and run RegisterClientScriptInclude, I once again get the same results. The control always works on the default page but never on the second page even though as far as I can tell the script is included in the control.
Any ideas?
append following code in your User Control.
protected override void OnPreRender(EventArgs e)
{
Page.ClientScript.RegisterClientScriptInclude("DateControl", this.ResolveClientUrl("~/JavaScript/IE6CalendarExtenderFix.js"));
base.OnPreRender(e);
}
Problem with control-relative file paths
You are probably having problems with relative paths to your JS file. You are specifying relative path to your custom control. You should probably write user control. Anyway. Your JS file is relative path to your custom control, but not relative to the containing page, so your JS file actually never loads. That's why your event handlers are undefined.
The easiest way would be to use absolute paths. Since you're working with user controls you can easily prepend application root folder.

Resources