Necesito obtener de manera string webmethod ASP.NET C# el Id del boton html.
Hasta el momento tengo lo siguiente y me funciona para obtener el valor del 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>
El 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;
}
}
}
Supongamos que el string "name" que está siendo capturado en:
data: '{name: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" }',
quiero que el name diga: btnGetTime
Ayuda con esto?, como debo modificar el Jquery?
O sea que el resultado final será:
Value 1btnGetTime Value 2btnGetTime Value 3btnGetTime
Aqui no agregué mas botones, pero dado el caso que agregue 2 o mas botones, tambien debe funcionar igual para con estos.
Value 1btnGetTimeB Value 2btnGetTimeB Value 3btnGetTimeB
Value 1btnGetTimeC Value 2btnGetTimeC Value 3btnGetTimeC