Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
All-Star
31334 Points
5415 Posts
Apr 30, 2012 06:10 AM|LINK
hi, as i have clearly explained that you can use unused server properties that can be accessed in client. so you can also use CssClass like below
<script> function GetCommandName() { var button = document.getElementById('<%= Button1.ClientID %>'); alert(button.title); alert(button.className); return false; } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Button" CommandName="Add" ToolTip="Add" OnClientClick="return GetCommandName()" CssClass="Add" /> </div> </form> (Or) else you can use Custom Control below to acces server-side properties public partial class Button_CommandNameInJS : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { CustomButton customButton = new CustomButton(); customButton.ID = "customButton"; customButton.Text = "Custom"; customButton.CommandName = "TestCommand"; customButton.OnClientClick = "alert(this.CommandName)"; this.form1.Controls.Add(customButton); } } public class CustomButton : Button { protected override void AddAttributesToRender(HtmlTextWriter writer) { Page.ClientScript.RegisterExpandoAttribute(this.ClientID, "CommandName", this.CommandName); base.AddAttributesToRender(writer); } }
Mark my both the replies as answered one
karthicks
All-Star
31334 Points
5415 Posts
Re: How to Access 'commandName' property of a button in Java Script
Apr 30, 2012 06:10 AM|LINK
hi, as i have clearly explained that you can use unused server properties that can be accessed in client. so you can also use CssClass like below
<script> function GetCommandName() { var button = document.getElementById('<%= Button1.ClientID %>'); alert(button.title); alert(button.className); return false; } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Button" CommandName="Add" ToolTip="Add" OnClientClick="return GetCommandName()" CssClass="Add" /> </div> </form> (Or) else you can use Custom Control below to acces server-side properties public partial class Button_CommandNameInJS : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { CustomButton customButton = new CustomButton(); customButton.ID = "customButton"; customButton.Text = "Custom"; customButton.CommandName = "TestCommand"; customButton.OnClientClick = "alert(this.CommandName)"; this.form1.Controls.Add(customButton); } } public class CustomButton : Button { protected override void AddAttributesToRender(HtmlTextWriter writer) { Page.ClientScript.RegisterExpandoAttribute(this.ClientID, "CommandName", this.CommandName); base.AddAttributesToRender(writer); } }Mark my both the replies as answered one
Karthick S