NewBie
NewBie

Reputation: 1844

Bind a value to a javascript method as parameter

I'm getting error on binding a javascript method call using Eval(). Following is the line of code within a repeater control:

<asp:Button ID="View" runat="server" Text="View" CommandName="View" OnClientClick="OpenInNewWindow(<%# Eval("JobID")%>)">
                        </asp:Button>

I need to get the value jobId for that javascript method. Can anyone help me out?

Upvotes: 3

Views: 3089

Answers (5)

sajanyamaha
sajanyamaha

Reputation: 3198

Try this:

<asp:Button ID="AddCartButton" runat="server" Text="Add To Cart" onClick="AddCartButton_Click" 
     OnClientClick='<%# Eval("JobID", "OpenInNewWindow(\"{0}\").ToString()") %>'/>

each single quote disorientate markup parser. Except this one, expression evaluated practically the same way as regular C# expression

Corrected the syntax

Upvotes: 1

Pawan Rai
Pawan Rai

Reputation: 1

Don't bind the javascript function declaratively. Do it in code behind by capturing the itemdatabound event of the corresponding grid. In this event first get the reference to the button then define bind the javascript function as:

    int JobID = DataBinder.Eval(e.Row.DataItem, "JobID").ToString();
    Btn.Attributes.Add("OnClientClick","javascript:OpenInNewWindow(" + JobID + ")");

Hope that helps...

Upvotes: 0

शेखर
शेखर

Reputation: 17604

<asp:Button ID="View" runat="server" Text="View" CommandName="View" OnClientClick="window.open('yourpage.aspx?JobID=<# Eval("JobID")','','width=200,height=100')">
</asp:Button>

Upvotes: 0

Akhil Sekharan
Akhil Sekharan

Reputation: 12693

I guess you are missing quotes around JobID.

Try:

<asp:Button ID="View" runat="server" Text="View" CommandName="View" OnClientClick="OpenInNewWindow('<%# Eval("JobID")%>')">
</asp:Button>

Upvotes: 1

Trisped
Trisped

Reputation: 6005

Either set the OnClientClick property in code behind or in code behind add the line page.DataBind(). When the page is bound it will evaluate JobID and add the value to the page sent to the client.

If you need more info you could check ASP.NET data binding overview or this post on another forum. I usually avoid page binding, instead setting the properties in code behind. While page binding is very useful, I have found it to be less predictable (did they call bind on page load? In a button event?) and sometimes complicated (one variable needs to be bound on page load, but the other after).

Upvotes: 0

Related Questions