views:

436

answers:

2

Trying to pop up an IFrame Shadowbox with Jquery from an asp:gridview datarow. I can't get the proper quotes into the string:

<asp:ImageButton ID="btnEdit" runat="server"
    OnClientClick='<%# "javascript:popAccount(\'"+ 
    Eval("id", "Popup.aspx?id={0}")+"\');" %>' />

Parser Error Message: The server tag is not well formed.

Without the escaped single quotes (cannot work, but parses properly):

<asp:ImageButton ID="btnEdit" runat="server"
    OnClientClick='<%# "javascript:popAccount("+ 
    Eval("id", "Popup.aspx?id={0}")+");" %>' />

Client-side HTML, as expected:

onclick="javascript:popAccount(Popup.aspx?id=3ce3b19c-1899-4e1c-b3ce-55e5c02f1);"

How do I get quotes into the Javascript?

Edit: added solution. This not very generic, since the databound types must be known in order to access the id property. The key (as defined in the GrodView's DataKeyNames parameter) does not seem to be exposed in the event argument. But it works.

    protected void editGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        // do not look at header or footer
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton btn = e.Row.FindControl("btnPopup") as ImageButton;
            if (btn != null)
            {
                btn.OnClientClick = 
                "javascript:popAccount('EditAccountPopup.aspx?"+
                Constants.acctidParam+"="+
                ((tb_account)(e.Row.DataItem)).id.ToString()+"');";
            }
        }
+1  A: 

Pick up the ImageButton in the GridView's rowdatabound event in the codebehind, and add the property from there.

But are you sure you need to use a server control? How about just a plain image? If you're "replacing" the click-behaviour of the ImageButton, it doesn't seem like you need it at all.

Update:

For the code-behind solution, I coded up this little sample (in VB.NET, sorry):

  Private Sub gridview1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridview1.RowDataBound
    Select Case e.Row.RowType
      Case DataControlRowType.DataRow
        Dim btnEdit As ImageButton = CType(e.Row.Cells(0).FindControl("btnEdit"), ImageButton)
        Dim strID As String = CType(e.Row.DataItem, Guid).ToString

        btnEdit.Attributes.Add("onclick", String.Format("javascript:popAccount('Popup.aspx?id={0}');", strID))
    End Select
  End Sub

However, I'd still recommend you go with a simpler aproach, jQuery is excellent here. No need for any code in your codebehind:

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
  <title></title>

  <script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.3.2.min.js" type="text/javascript"></script>

  <script type="text/javascript" language="javascript">
    $(document).ready(function() {

      $("img.edit").click(function() {
        var sID = this.id.replace('btnEdit_', '');
        alert(sID); // Add your popup opening logic here...
      });
    });
  </script>

</head>
<body>
  <form id="form1" runat="server">
  <div>
    <asp:GridView ID="gridview2" AutoGenerateColumns="false" runat="server">
      <Columns>
        <asp:TemplateField>
          <ItemTemplate>
            <img id="btnEdit_<%#Container.DataItem %>" class="edit" />
          </ItemTemplate>
        </asp:TemplateField>
      </Columns>
    </asp:GridView>
  </div>
  </form>
</body>
</html>
Jakob Gade
Does it matter for this issue? I have to use the databinding tag either way ...
cdonner
Thanks for the accepted answer. I updated my post to include a suggestion for a solution that uses codebehind, and a simpler version using jQuery.
Jakob Gade
A: 

If you want to keep your code the way it is, why not just do this?

<asp:ImageButton ID="btnEdit" runat="server"
    OnClientClick='<%# "javascript:popAccount(&quot"+ 
    Eval("id", "Popup.aspx?id={0}")+"&quot);" %>' />
nickyt
That would be nice, but does not work in IE7.
cdonner
I tried various document types (strict/transitional) in IE8 compatibility mode and I am getting an exception from this line. It works in Standards mode.
cdonner
A hardcoded function call works fine in all modes in IE8.
cdonner