Fire javascript on button click
I'm working on adding some analytics code into our e-commerce website. What I'm doing is writing some constructing the javascript in the a string builder and then setting that js in a literal in the aspx side. The trick is I only want this literal to populate when the add to cart button is clicked. Easy enough, build the string in the button click event and set the literal text right? Well I've tried that and can't get it to work. The button click event still needs to retain its previous actions in addition to this new one.
When I test, I cannot see the string being set. Am I missing something here?
Here my button code in the aspx page along with my literal that sits directly beneath it:
<asp:ImageButton ID="btnCartMain" runat="server" ImageUrl="~/Images/Buttons/btn_AddToCartFlat.gif" CommandName="AddToCart" AlternateText="Add to cart" OnClick="GroupedAddToCartClick" ImageAlign="AbsMiddle" />
<asp:Literal ID="litSCscAdd" runat="server"></asp:Literal>
Here is the click handler (with new string at bottom):
prot开发者_高级运维ected void GroupedAddToCartClick(object sender, EventArgs e)
{
int itemId = Convert.ToInt32(ddItems.SelectedValue);
ProductItem prodItem = null;
_prod.ProductItems.TryGetValue(itemId, out prodItem);
if (ddShipTo.SelectedValue == string.Empty)
rfName.Enabled = true;
if (ProcessAddToCartClick(ddShipTo.SelectedValue,
ddShipTo.SelectedItem.Text,
Convert.ToInt32(txtQuantity.Text),
prodItem, ddItems.SelectedIndex, string.Empty))
{
btnCartMain.ImageUrl = "~/Images/Buttons/btn_AddedToCart.gif";
}
//---- SITECATALYST -- add to cart event ///
System.Text.StringBuilder sbSCseventscadd = new System.Text.StringBuilder();
sbSCseventscadd.Append(String.Format("<script type=\"text/javascript\">"));
sbSCseventscadd.Append(String.Format("s.events=\"scAdd\""));
sbSCseventscadd.Append("</script>");
this.litSCscAdd.Text = sbSCseventscadd.ToString();
}
Well, I ran the following code and it works fine here.
protected void GroupedAddToCartClick(object sender, EventArgs e)
{
System.Text.StringBuilder sbSCseventscadd = new System.Text.StringBuilder();
sbSCseventscadd.Append(String.Format("<script type=\"text/javascript\">"));
sbSCseventscadd.Append(String.Format("s.events=\"scAdd\""));
sbSCseventscadd.Append("</script>");
this.litSCscAdd.Text = sbSCseventscadd.ToString();
}
First I tried setting a plain string to the literal and it works like a charm. With JS I get this aspx:
<input type="image" name="btnCartMain" id="btnCartMain" src="Images/Buttons/btn_AddToCartFlat.gif" alt="Add to cart" align="absmiddle" style="border-width:0px;" />
<script type="text/javascript">s.events="scAdd"</script>
Which is what you want, right?
Of course, I get a runtime error that s is not defined.
So there must be another issue somewhere? Is the button_click really executed?
Apart from that, using a StringBuilder here is plain overhead.
If I am following correctly you are trying to add some client side functionality to an existing button when certain criteria are met. But at the same time you need the buttons predefined functionality (a post back?) to remain in effect?
One way I have accomplished this type of behavior before was to place a transparent div over the top of the button. Give the div a click event which calls the extension function and then the click event of the under lying button. Then you can hide or show the div based on whatever criteria. If the div is shown the extra client side code executes then passes the click event to the button, if the div is hidden then the button receives the click directly.
Not saying this is necessarily the best option for your scenario but it is one that should work.
Instead of firing the javascript serverside, couldn't you just set the onclientclick
property of the button and add your javascript there (remembering to return true)?
I finally got it - sort of. I ended up just executing it in the postback after the button is clicked. Not the best way to do it but since its the only control on the page that causes a postback it doesnt matter.
精彩评论