vml19
vml19

Reputation: 3864

Call Javascript from GridView TemplateField

I have a GridView, I have coded like the below

<asp:TemplateField HeaderText="Item">
 <ItemTemplate>                                            
   <asp:Button ID="btnItem" OnClientClick="javascript:SearchReqsult(<%#Eval("Id") %>);"
 CssClass="Save" runat="server" /> 
</ItemTemplate>
</asp:TemplateField>

I face a run time error which says that my button control is formatted correctly I suppose the issue with the following

OnClientClick="javascript:SearchReqsult(<%#Eval("Id") %>);"

Any idea?

Upvotes: 2

Views: 12821

Answers (9)

Sandip Patel
Sandip Patel

Reputation: 1

Call javascript function from Template Field

<asp:TemplateField HeaderText="Status" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
    <asp:LinkButton ID="lnkDelete" OnClientClick=<%#String.Format("return dis_status('{0}');", Eval("Status")) %> runat="server" CommandArgument='<%# Eval("AgtId")%>' Text='<%# Eval("Status")%>' CommandName="cmdDelete" />
</ItemTemplate>

Upvotes: 0

Vishal Patel
Vishal Patel

Reputation: 281

Try the following code

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
         Button btn = (Button)e.Row.FindControl("btnItem");
         btn.Attributes.Add("OnClick","SearchReqsult(this.id);");
    } 
 }

Upvotes: 2

Pankaj
Pankaj

Reputation: 10115

Try with String.Format Class

<asp:Button ID="btnCheque" runat="server" 
OnClientClick=<%#String.Format("return SearchReqsult('{0}');", Eval("ID")) %> />

Upvotes: 1

Kaf
Kaf

Reputation: 33839

This is what you need to do

CODE BEHIND:

protected void GridView_RowCreated(object sender,GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (((DataRowView)e.Row.DataItem) !=null)
            ((Button)e.Row.FindControl("btnCheque")).Attributes.Add("onclick","SearchReqsult(" + ((DataRowView)e.Row.DataItem)["productId"].ToString() + ")");

    }
}

HTML:

<asp:Button ID="btnCheque" CssClass="Save" runat="server" Text="My Button" /> 

Upvotes: 1

PraveenVenu
PraveenVenu

Reputation: 8337

Try with single quotes instead of double and string.Format()

<asp:Button ID="btnCheque" OnClientClick=<%# string.Format("javascript:SearchReqsult('{0}');return false;",Eval("id")) %> CssClass="Save" runat="server" />

Upvotes: 0

Neelam
Neelam

Reputation: 1068

Enclose your 'id' field in single quotes.

just replace

 btn.OnClientClick = "javascript:SearchReqsult(" + 
                                 DataBinder.Eval(ev.Row.DataItem, "Id") + ");";

with

 btn.OnClientClick = "javascript:SearchReqsult(" + 
                                 DataBinder.Eval(ev.Row.DataItem, 'Id') + ");";

your code will come as

protected void Page_Load(object sender, EventArgs e)
{
yourGridView.RowDataBound += (s, ev) =>
        {
           if (ev.Row.RowType == DataControlRowType.DataRow)
           {
               var btn= (Button) ev.Row.FindControl("btnCheque");
               btn.OnClientClick = "javascript:SearchReqsult(" + 
                                 DataBinder.Eval(ev.Row.DataItem, 'Id') + ");";
           }
        };
}

Upvotes: 1

Software Engineer
Software Engineer

Reputation: 3956

Try enclosing javascript function call in single quotes:

OnClientClick='javascript:SearchReqsult(<%#Eval("Id") %>);'

Maybe the problem is with missing/wrong quotes.

Upvotes: 1

Kaushik Patel
Kaushik Patel

Reputation: 1

Put Labe l and bind Id in that and make it visible false.

On GridView RowDatabound event add attrinute to control and assign javascript on button onclientclick event

Upvotes: 0

Arion
Arion

Reputation: 31249

Can't you do this is databound. Maybe something like this:

protected void Page_Load(object sender, EventArgs e)
{
    yourGridView.RowDataBound += (s, ev) =>
            {
               if (ev.Row.RowType == DataControlRowType.DataRow)
               {
                   var btn= (Button) ev.Row.FindControl("btnCheque");
                   btn.OnClientClick = "javascript:SearchReqsult(" + 
                                     DataBinder.Eval(ev.Row.DataItem, "Id") + ");";
               }
            };
}

Upvotes: 1

Related Questions