2

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?

1
  • try to use this OnClientClick="javascript:SearchReqsult('<%# Eval("Id") %>');" Commented Mar 9, 2012 at 9:51

9 Answers 9

2

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);");
    } 
 }
Sign up to request clarification or add additional context in comments.

1 Comment

ID is the field to bound with button.
1

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") + ");";
               }
            };
}

1 Comment

I do update panel post back using __doPostBack inside javascript:SearchReqsult. But Request.Params.Get("__EVENTARGUMENT") is always null when I click on the button. the ID doesnt get passed code behind
1

Try enclosing javascript function call in single quotes:

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

Maybe the problem is with missing/wrong quotes.

1 Comment

This is not rendering the onclick event of properly. Please check your view source of your page.
1

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') + ");";
           }
        };
}

1 Comment

We can directly move Your above mentioned code to HTML side only.
1

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" /> 

7 Comments

Change "productId" column name to your column name "id". This is a working code example.
Your lazy loading is wrong in this context. DataRowView cannot be null.. Instead you can check ((DataRowView)e.Row.DataItem)["Id"] for null values.
Why you should go for code behind for such a small task? Just need to call the client function on button click. So no need to waste execution time in executing code behind as well...
@PankajGarg DataRowView can be null on post back. Have a look.
DataRowView cannot be null. Reason is, the datasource suppose offers you 10 row, this means that you have view of 10 rows. So itself row cannot be null. BUT the cell value can be null. If row is null, then what are you expecting from your DataSource ? If you are expecting no rows then it will not come to RowboundData for DataItem iteration.
|
1

Try with String.Format Class

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

Comments

0

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

Comments

0

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" />

3 Comments

This is not rendering the onclick event properly. Please check the View source in your page.
I have updated the code. This should work. Please remove the retun false; if you dont need.
Still there is one formatting issue. Single quote is missing around the {0} and single quote not required before your <%#
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.