JavaScript Pass button id to an asp.net hiddenfield - javascript

I got a silly question just as the question stated,the failed code shown below:
<script type="text/javascript">
function HISenlarge(id) {
var parent = id;
document.getElementById('HiddenField1').value = parent;
}
</script>
HTML:
<button type="button" id="kkkk" onclick="HISenlarge(this.id)"></button>
<asp:HiddenField ID="HiddenField1" runat="server" value=""/>
VBcode
Protected Sub CommentButton_Click(sender As Object, e As EventArgs) Handles CommentButton.Click
Dim c_id As String
c_id = HiddenField1.Value.ToString

document.getElementById('HiddenField1').value = parent;
This is not a HTML for the HiddenField1.So you should reference it by its ClientID
document.getElementById('<%=HiddenField1.ClientID %>').value= parent;

Related

Why isn't my WebMethod being called from javascript?

I'm working in ASP.Net 4. I have the following within the body tag of my aspx:
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server"/>
<script type="text/javascript">
function Process() {
var CHK = document.getElementById("<%=lstAffEmployees.ClientID%>");
var checkbox = CHK.getElementsByTagName("input");
var counter = 0;
for (var i = 0; i < checkbox.length; i++) {
if (checkbox[i].checked) {
counter++;
var Attorney = label[i].innerHTML;
var cutoff = Attorney.indexOf('[');
var cAttorney = Attorney.substring(0, cutoff - 1);
PageMethods.saveData(checkbox[i].value, cAttorney);
}
}
return true;
}
</script>
In my code-behind, I have:
[WebMethod]
public static void saveData(string attyNo, string attyName)
{
//mycode
}
In debugging, I get to the PageMethods call in the javascript, but then saveData in my code-behind is never called. I don't understand why not. Can anyone help?

Is it possible to get javascript data from web to application

I need to get a list of innerHTML strings from a website (for example facebook) to my .NET application.
Is there a way to get results from the following function into my application (i would put this data into a list) :
var data = new Array();
for(i=0; i<document.getElementsByClassName("class-name").length;i++)
data.push(document.getElementsByClassName("class-name")[i].innerHTML);
The code above outputs exactly the data i need, now my question is if it's possible to somehow get this data into my c# list?
This is how I'd like it to look :
var JS_DATA = data; //get the js array as a variable
static List<string> data = new List<string>(); //make a new list
foreach (string str in JS_DATA)
data.Add(String.Format("{0}", str)); //add the whole js array to the list
Here is an example:
see hidden Field array_store
Client side:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
var array_store;
window.onload = function () {
array_store = document.getElementById("array_store");
document.getElementById("array_disp").innerHTML = array_store.value;
};
function UpdateArray() {
var arr;
if (array_store.value == "") {
arr = new Array();
} else {
arr = array_store.value.split(",");
}
arr.push((arr.length + 1).toString());
array_store.value = arr.join(",");
};
</script>
</head>
<body>
<form id="form1" runat="server">
<span id="array_disp"></span>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="UpdateArray()" />
<input type="hidden" id="array_store" name = "ArrayStore" value = '<%=this.ArrayStore %>' />
</form>
</body>
</html>
C#:
protected string ArrayStore = "";
protected void Page_Load(object sender, EventArgs e)
{
this.ArrayStore = Request.Form["ArrayStore"];
}

jquery inside User Control created dynamically

I've created an User Control where I have a button. And when this button is clicked a jquery function is executed. The jquery function is inside the User Control page.
Now i'm trying to add this User Control dynamically in my page, inside an UpdatePanel.
The problem is that my button is not working.
After i google it, i found out that maybe the problem is in using jquery inside ajax UpdatePanel. After every asynchronous postback the jquery script is lost. So the idea is to rebind my jquery function in every asyn postback. But in the all answers that i found, they kinda suppose that the script manager is in User control and it's not in my case.
Here my jquery fucntion
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#<%= idButtonLeft %>").bind("click", function () {
var options = $("#<%= idListDestinataire %> option:selected");
for (var i = 0; i < options.length; i++) {
var opt = $(options[i]).clone();
$(options[i]).remove();
$("#<%= idListSource %>").append(opt);
}
});
$("#<%= idButtonRight %>").bind("click", function () {
var options = $("#<%= idListSource %> option:selected");
for (var i = 0; i < options.length; i++) {
var opt = $(options[i]).clone();
$(options[i]).remove();
$("#<%= idListDestinataire %>").append(opt);
}
});
});
</script>
the code to add my User control dynamically
Dim CListBox As UserControl = LoadControl("DragDropList.ascx")
CListBox.ID = "CListBox" + i.ToString()
CType(CListBox, DragDropList).idListSource = "ListLeft" + i.ToString()
CType(CListBox, DragDropList).idListDestinataire = "ListRight" + i.ToString()
CType(CListBox, DragDropList).idButtonLeft = "idButtonLeft" + i.ToString()
CType(CListBox, DragDropList).idButtonRight = "idButtonRight" + i.ToString()
UpdatePanel1.ContentTemplateContainer.Controls.Add(CListBox)
Panel1.Controls.Add(CListBox)
in my page i have this
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ImageButton1" />
</Triggers>
</asp:UpdatePanel>
<asp:ImageButton ID="ImageButton1" ImageAlign="Right" EnableViewState="false" ImageUrl=".\image\ajouter.png" runat="server" AlternateText="Ajouter" OnClick="AjouterC"/>
So please any ideas to help.
Thank you
When i changed the trigger on PostBackTrigger, it's working but i want it in asynchrone postback. and still don't know why it's not working!
Use This
<script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
function EndRequestHandler(sender, args) {
if (args.get_error() == undefined) {
$(function () {
//Your script
});
}
}
</script>
This will work on post back that your update panel is giving to you.

jQuery cant get value from textbox

I am trying to get a value from a textbox so that that I can rotate an Image using jQuery
<script src="Scripts/jQueryRotate.2.2.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
var angle = $("input:TextBox1").val();
$("#needle").rotate(angle);
alert(angle);
});
</script>
And I'm populating the textbox as follows
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
WeatherLibrary.WeatherData wLib = new WeatherLibrary.WeatherData();
double dataLatest;
string sensorName;
sensorName = "umtAdjWinDir";
double dir = wLib.GetLatestData(sensorName).Value;
dir = Math.Round(dir, 0);
TextBox1.Text = dir.ToString();
}
</script>
TextBox1 is the the id of the textbox
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="True"></asp:TextBox>
The alert I have given says undefined and the image never gets rotated.
Try
var angle = $("#TextBox1").val();
OR
var angle = $("#<%= TextBox1.ClientID %>").val();
If TextBox1 is the id of the input element you can use var angle = $("#TextBox1").val(); to get the value form the textbox
Try using :
var angle = $('#<%=TextBox1.ClientID%>').val();
try
var angle = $("input#TextBox1").val();
assuming you have an input with id="TextBox1"
I think not sure it may be a problem of master page or user control.
<script src="Scripts/jQueryRotate.2.2.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
var angle = $("#TextBox1").val();
$("#needle").rotate(angle);
alert(angle);
});
</script>
Supposed to be
var angle = $("input[id*='TextBox1']").val(); // For ID
var angle = $("input.TextBox1").val(); // For class
var angle = $("[id*='TextBox1']").val(); // For ID this is sufficient
Need to use attribute ends with $ or attribute contains selector * if you are using ASP.NET controls with runat="server" attribute
$("#TextBox1").val() this code will help u out to get the textbox value..

Javascript Incrementer

<script type="text/javascript">
function Incrementer()
{
debugger;
var txtBox = document.getElementById('MainContent_TextBox1').value;
int i = parseInt(txtBox);
i = i + 1;
var v = i + "";
document.getElementById('MainContent_TextBox1').value = v;
alert(v);
}
</script>
<asp:TextBox ID="TextBox1" runat="server" Text="0"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Up" OnClientClick="Incrementer();"/>
<asp:Button ID="Button2" Text="Down" runat="server"/>
</asp:Content>
I am unable to increment the value in javascript....what is the problem?
There is no int in javascript.
Variables are always declared with var.
see fiddle: http://jsfiddle.net/HMC9V/
int i = parseInt(txtBox); this line is incorrect.
int doesn't used in Javascript for declaring variables.
It should be corrected to var i = parseInt(txtBox);

Categories

Resources