How drag & drop and resize comnponents dynamically ASP.NET? - c#

I want to drag and drop simple div with text, and also resize it, when I drop it.
I have to use jQuery, but I never works with it.
Right know I have like this code in Head content:
<script src="Scripts/jquery.ui.draggable.js" type="text/javascript"></script>
<script src="Scripts/jquery.ui.core.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#d1").draggable();
});
</script>
also
<div id="d1" style="width:100px; height:100px;">
<p>Move This Div</p>
</div>
At first I want try to move this div, but can't move it, I don't know why?After that I want to try drop it on table, and resize it.Please can u provide some usefull examples or your own examples.Thank u
P.S.Actually I should move controls of ASP.NET Web Form on table

you need to load jquery library first, so the code will look something like that:
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.ui.core.js" type="text/javascript"></script>
<script src="Scripts/jquery.ui.draggable.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#d1").draggable();
});
</script>

Here is a small example:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="Stylesheet" href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/themes/redmond/jquery-ui.css" />
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.0.min.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.22/jquery-ui.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#d1").draggable();
$("#droppable").droppable({
drop: function (event, ui) {
$(this).width(ui.draggable.width());
$(this).height(ui.draggable.height());
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="d1" style="width:100px; height:100px; border: solid 1px silver;">
<p>Move This Div</p>
</div>
<table border="1">
<tr>
<td id="droppable" style="width: 200px; height: 200px;">
One
</td>
</tr>
</table>
</form>
</body>
</html>

Related

ReferenceError: $ is not defined on jquery.are-you-sure.js [duplicate]

This question already has answers here:
JQuery - $ is not defined
(36 answers)
Closed 8 years ago.
I am a beginner in jquery
I have test.aspx page :
<%# Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
<script type="text/javascript" src="jquery-1.8.2.min"></script>
<script type="text/javascript" src="jquery.are-you-sure.js"></script>
<script>
$(function () {
$('form').areYouSure();
$('form').areYouSure({ 'message': 'Your profile details are not saved!' });
});
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<!DOCTYPE html>
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"/>
</div>
</form>
</body>
</html>
I get error:
ReferenceError: $ is not defined (Line #5)
I am tired to find the solution.
The error suggesting that you don't have jquery-1.8.2.min file at the same label where your page is.
Like suppose you have file test.aspx on root folder then your script file should on root.
And if you script file is inside a folder then you need to specify the folder name
<script type="text/javascript" src="yourFolder/jquery-1.8.2.min"></script>
I update the code, I was missing folder name.
jquery.are-you-sure.js plugin run correctly now
<%# Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test</title>
<script type="text/javascript" src="Scripts/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="JS/jquery.are-you-sure.js"></script>
<script type="text/javascript" >
$(function () {
$('form').areYouSure();
$('form').areYouSure({ 'message': 'Your profile details are not saved!' });
});
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"/>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"/>
</form>
</body>
</html>

How I can make my Div draggable with jQuery in ASP.NET

I have a ASP.NET Application with a master site and content sites. in one of this content sites I have a Div with Controls and I want to try it make draggable with jQuery but I don't know how I can to this in ASP.NET because of the Control Id in asp.
here is my code:
master site:
...
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="Styles/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="Script/jquery-1.8.2.js" type="text/javascript"></script>
<script src="Script/jquery-ui.js" type="text/javascript"></script>
<script>
$(function () {
$("#create_box").draggable();
});
</script>
</head>
<body>
<form id="mainform" runat="server">
<div class="gastzugang">
<asp:ContentPlaceHolder ID="lw_header" runat="server">
<!--Header-->
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="lw_content" runat="server">
<!--Content-->
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="lw_footer" runat="server">
<!--Footer-->
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
My Content Site:
<%# Page Title="" Language="C#" MasterPageFile="~/master.Master" AutoEventWireup="true" CodeBehind="CreateUser.aspx.cs" Inherits="lw_gastzugang.CreateUser" %>
<asp:Content ID="Content2" ContentPlaceHolderID="lw_content" runat="server">
<div class="createuser">
<div class="create_box">
<div class="newUser">
Benutzer Anlegen <br/>
<br/>
//Here are my Controls
<br/>
<asp:Button ID="btnAnlegen" runat="server" Text="Benutzer anlegen"
onclick="btnAnlegen_Click" />
</div>
</div>
</div>
</asp:Content>
I want to this:
http://jqueryui.com/droppable/
Just use jQuery UI draggable.
Make
ClientIDMode="static" in the asp.net Tag
Or Use
<script type="text/javascript">
$('#'+'<%=lw_content.ClientID%>').draggable();
</script>
You could take a look at hammer.js which covers a 'drag' style event:
https://github.com/EightMedia/hammer.js
The drag example there is at:
http://eightmedia.github.com/hammer.js/drag/
Now I don't know too much about ASP.NET but what I would try is applying the drag behaviour to anything with a .draggable class.
Hope that helps a little!

html5 canvas tag - 2d context rendering not working

I implemented the HTML5 canvas tag in asp.net2010 to draw graph bars but its not working below is the code that I implement in my .aspx page its showing empty page.
I am using browsers like (updated version of Chrome, Firefox, IE9)
<%# Page Language="C#" AutoEventWireup="true" CodeFile="CanvasChart.aspx.cs" Inherits="CanvasChart" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/RGraph.bar.js" type="text/javascript"></script>
<script type="text/javascript">
window.onload = function () {
var data = [280, 45, 133];
var bar = new RGraph.Bar('testroundchart', data);
bar.Set('chart.labels', ['Richard', 'Alex', 'Nick']);
bar.Set('chart.gutter.left', 45);
bar.Set('chart.background.barcolor1', 'white');
bar.Set('chart.background.barcolor2', 'white');
bar.Set('chart.background.grid', true);
bar.Set('chart.colors', ['red']);
bar.Draw();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<canvas id="testroundchart" runat ="server" width="800" height="400"></canvas>
</div>
</form>
</body>
Thanks
Bhanu Prakash Inturi
You have to add two more script tags (RGraph.common.core.js and excanvas.original.js) to make your code work:
<script src="libraries/RGraph.common.core.js" ></script>
<!--[if lt IE 9]><script src="excanvas/excanvas.original.js"></script><![endif]-->
The second script tag is only needed for Internet Explorer.

JQuery Menu in Master Page Reloading in Every Child Page

I have a Jquery SooperFish Menu in my master page.Onclick on any SubMenu option it is reloading the Menu. Is there any way to restict reloading Menu every time in Child Page?
To decrease the Database lookups.
EDIT:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="elogis.master.cs" Inherits="elogis.elogis" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="stylesheet" type="text/css" href="Styles/sooperfish.css" media="screen"/>
<link rel="stylesheet" type="text/css" href="Styles/sooperfish-theme-large.css"
media="screen"/>
<script src="Jquery/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="Jquery/jquery.easing-sooper.js" type="text/javascript"></script>
<script src="Jquery/jquery.sooperfish.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$('ul.sf-menu').sooperfish();
});
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SooperFish spoorfishMenu = new SooperFish();
Literal1.Text = spoorfishMenu.ExecuteXSLTransformation();
}
}
It looks as if the onClick postback, if you're writing it yourself (JS) add:
return false;
at the end-
onClick="doSomething();return false;"
Unless you are using UpatePanel's or another AJAX framework, each post-back is a new HTTP request, meaning the menu has to reload because the page is reloaded.
You have several options for caching, search SO or take a look at the link below to get started:
ASP.NET Caching

Why isn't this script running from the masterpage?

I have a Page that is associated with a master page. In the master page, I put the css links in the head section and I put the jquery script tag and the script that contains the function to toggle the grid, but when it is not working. Looks like it is not even calling showhide when I click on an item.
Here is a snippet of the master page:
<head runat="server">
<title></title>
<link href="MainLayout.css" rel="stylesheet" type="text/css" />
<link href="ajaxtab.css" rel="stylesheet" type="text/css" />
<link href="dialog.css" rel="stylesheet" type="text/css" />
<link href="grid.css" rel="stylesheet" type="text/css" />
<link href="pager.css" rel="stylesheet" type="text/css" />
<link href="subgrid.css" rel="stylesheet" type="text/css" />
<script src="jquery-1.3.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
//master: id of div element that contains the information about master data
//details: id of div element wrapping the details grid
function showhide(master, detail) {
//First child of master div is the image
var src = $(master).children()[0].src;
//Switch image from (+) to (-) or vice versa.
if (src.endsWith("plus.png"))
src = src.replace('plus.png', 'minus.png');
else
src = src.replace('minus.png', 'plus.png');
//Set new image
$(master).children()[0].src = src;
//Toggle expand/collapse
$(detail).slideToggle("normal");
}
</script>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
Here is the div that contains the showhide function in the onclick event in the aspx page:
<div class="searchgroup"
id='<%#String.Format("master{0}",Container.DataItemIndex)%>'
onclick='showhide(<%#String.Format("\"#master{0}\"",Container.DataItemIndex)%>
,
<%#String.Format("\"#detail{0}\"",Container.DataItemIndex) %>)'>
<asp:Image ID="imgCollapsible"
CssClass="first"
ImageUrl="plus.png"
Style="margin-right: 5px;"
runat="server" />
<span class="searchheader"><%#Eval("Business")%></span>
</div>
Here is html it generates for the master and detail div:
//master div
<div class="searchgroup"
id='master0'
onclick='showhide("#master0","#detail0")'>
<img id="ctl00_ContentPanel_gvMaster_ctl02_imgCollapsible"
class="first" src="plus.png"
style="border-width:0px;margin-right: 5px;" />
<span class="searchheader">ABC</span>
</div>
//details div
<div id='detail0' class="searchdetail">
<div>
<table class="searchgrid"
id="ctl00_ContentPanel_gvMaster_ctl02_gvDtails">
<tr>
<th>Status</th>
<tr class="searchrow">
<td>2</td>
</tr>
</table>
</div>
</div>
I could not get the JQuery working and I was running out of time, so for now I decided to use the collapsible panel externder from the ajax control toolkit. When I get time, I will investigate the JQuery Issue, Thanks for all your suggestions so far. If anyone has any more, please let me know.
Your javascript is throwing an error at src.endsWith("plus.png") because there is no built in endsWith function in js. replace that with src.substr(-8) == "plus.png" instead and it works:
<script type="text/javascript">
//master: id of div element that contains the information about master data
//details: id of div element wrapping the details grid
function showhide(master, detail) {
//First child of master div is the image
var src = $(master).children()[0].src;
//Switch image from (+) to (-) or vice versa.
if (src.substr(-8) == "plus.png")
src = src.replace('plus.png', 'minus.png');
else
src = src.replace('minus.png', 'plus.png');
//Set new image
$(master).children()[0].src = src;
//Toggle expand/collapse
$(detail).slideToggle("normal");
}
</script>
EDIT - Working example:
MasterPage.master
<%# Master Language="C#" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
//master: id of div element that contains the information about master data
//details: id of div element wrapping the details grid
function showhide(master, detail) {
//First child of master div is the image
var src = $(master).children()[0].src;
//Switch image from (+) to (-) or vice versa.
if (src.substr(-8) == "plus.png")
src = src.replace('plus.png', 'minus.png');
else
src = src.replace('minus.png', 'plus.png');
//Set new image
$(master).children()[0].src = src;
//Toggle expand/collapse
$(detail).slideToggle("normal");
}
</script>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
Default2.aspx
<%# Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div class="searchgroup" id='master0' onclick='showhide("#master0","#detail0")'>
<img id="ctl00_ContentPanel_gvMaster_ctl02_imgCollapsible" class="first" src="plus.png" style="border-width:0px;margin-right: 5px;" />
<span class="searchheader">ABC</span>
</div>
<div id='detail0' class="searchdetail">
<div>
<table class="searchgrid"
id="ctl00_ContentPanel_gvMaster_ctl02_gvDtails">
<tr>
<th>Status</th>
</tr>
<tr class="searchrow">
<td>2</td>
</tr>
</table>
</div>
</div>
</asp:Content>
" Looks like it is not even calling showhide when I click on an item." If you simplify the function with just an alert() function, does it work.
The code looks fine to me. Maybe there is some id mismatch.
You may check if jquery's path is correct in your page (that which uses this master page).
To be sure if its calling showhide() you can use Firebug in Firefox with a breakpoint in the function.
Also, it will give you more clues to debug and guess what is going wrong.
The code seems to be ok to me, too.
just a thought, if you right click view source on the browser, do you see the jquery path correctly? my experience tells me you need to resolve javascript urls on master pages (it works fine for css, but not js files)
<script src="<%= ResolveUrl("~") %>jquery-1.3.2.min.js" type="text/javascript"></script>

Categories