Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Apr 30, 2012 06:10 AM by karthicks
Member
27 Points
60 Posts
Apr 27, 2012 11:48 AM|LINK
How to Access 'commandName' property of a button in Java Script ?
All-Star
31376 Points
5421 Posts
Apr 27, 2012 12:32 PM|LINK
hi, you can't access 'commandName' in javascript bcos it will not rendered to client
anyhow alternatively you can do like below, here i have used ToolTip, like this you can other unused properties such as CssClass etc.,
<script> function GetCommandName() { var button = document.getElementById('<%= Button1.ClientID %>'); alert(button.title); 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()" /> </div> </form> </body>
Apr 30, 2012 05:23 AM|LINK
Thanks for your reply.
Actually i have to show some default tooltip for my imagebutton control. Besides i have to bind some data (say some id), which shold be available in java script.
What to do? In which properties we can bind data, and that is available in JS.
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
sfiros2003
Member
27 Points
60 Posts
How to Access 'commandName' property of a button in Java Script
Apr 27, 2012 11:48 AM|LINK
How to Access 'commandName' property of a button in Java Script ?
karthicks
All-Star
31376 Points
5421 Posts
Re: How to Access 'commandName' property of a button in Java Script
Apr 27, 2012 12:32 PM|LINK
hi, you can't access 'commandName' in javascript bcos it will not rendered to client
anyhow alternatively you can do like below, here i have used ToolTip, like this you can other unused properties such as CssClass etc.,
<script>
function GetCommandName() {
var button = document.getElementById('<%= Button1.ClientID %>');
alert(button.title);
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()" />
</div>
</form>
</body>
Karthick S
sfiros2003
Member
27 Points
60 Posts
Re: How to Access 'commandName' property of a button in Java Script
Apr 30, 2012 05:23 AM|LINK
Thanks for your reply.
Actually i have to show some default tooltip for my imagebutton control. Besides i have to bind some data (say some id), which shold be available in java script.
What to do? In which properties we can bind data, and that is available in JS.
karthicks
All-Star
31376 Points
5421 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