I need to get the Id of the html button in a webmethod ASP.NET C# string.
So far I have the following and it works for me to get the value of the textbox.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="Dashboard.ks.Dashboard.ks.Test" %>
<!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 id="Head1" runat="server">
<title></title>
<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
<script type = "text/javascript">
function ShowCurrentTime() {
$.ajax({
type: "POST",
url: "Test.aspx/myFunction",
data: '{name: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
//alert(response.d);
}
});
}
function OnSuccess(response) {
document.getElementById('<%=Label1.ClientID %>').innerHTML = response.d[0];
document.getElementById('<%=Label2.ClientID %>').innerHTML = response.d[1];
document.getElementById('<%=Label3.ClientID %>').innerHTML = response.d[2];
}
</script>
</head>
<body style = "font-family:Arial; font-size:10pt">
<form id="form1" runat="server">
<div>
Your Name :
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<input id="btnGetTime" type="button" value="Show Current Time"
onclick = "ShowCurrentTime()" />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Aquí estoy Change my mind"></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Text="Aquí estoy Change my mind again"></asp:Label>
<br />
<asp:Label ID="Label3" runat="server" Text="Aquí estoy Change my mind once again"></asp:Label>
</div>
</form>
</body>
</html>
the codebehind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
namespace Dashboard.ks.Dashboard.ks
{
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
//public static string GetCurrentTime(string name)
//{
// return "Hello " + name + Environment.NewLine + "The Current Time is: "
// + DateTime.Now.ToString();
//}
[System.Web.Services.WebMethod]
public static Array myFunction(string name)
{
string[] a = new string[3];
a[0] = "value 1" + name;
a[1] = "value 2" + name;
a[2] = "value 3" + name;
return a;
}
}
}
Suppose the string "name" is being captured in:
data: '{name: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" }',
I want the name to say: btnGetTime
Help with this? How should I modify the Jquery?
So the end result will be:
Value 1btnGetTime Value 2btnGetTime Value 3btnGetTime
Here I did not add more buttons, but given the case that I add 2 or more buttons, it should also work the same for these.
Value 1btnGetTimeB Value 2btnGetTimeB Value 3btnGetTimeB
Value 1btnGetTimeC Value 2btnGetTimeC Value 3btnGetTimeC
If you define multiple buttons
Could you send the name of what launches the action?
You dynamically associate the click to the different buttons, that's why the selector using the
*=
taking all the buttons that have the text you define.Attribute Contains Selector [name*=”value”]