0
if (e.Row.RowType == DataControlRowType.DataRow)
{
   // if no link are presen the cell, 
   // we add the functionnality to select the row on the cell with a click
   cell.Attributes.Add("onclick", "x();");              

   // here we add the command to postback when the user click somewhere in the cell
   cell.Style.Add(HtmlTextWriterStyle.Cursor, "pointer");
   cell.Attributes.Add("title", "Select");
}

Actually when I call the function x which is created in c# is not executing but wen I declare in javascript it is executing what is the problem? plz let me know

7
  • where is x defined? did you really mean to surround it with **? Commented Feb 22, 2011 at 13:14
  • 1
    Hey, welcome to StackOverflow (S.O.) people here are very helpful, but you'll need to put a little more effort into asking your question in a way that people will want to and be able to answer. Could you give a little more context for your question, and perhaps show what function x contains. Try reading tinyurl.com/so-hints for some good ideas! Again, welcome! Commented Feb 22, 2011 at 13:15
  • 1
    @Marc, @roryf, I suspect that the asterisks were an attempt to make x() bold. Which in fact worked in the original version of the question but it was edited and the asterisks weren't removed. I suspect this is a classic "trying to call a C# function from a javscript onclick event client-side" question. Commented Feb 22, 2011 at 13:15
  • actually x is a function i didnt put here Commented Feb 22, 2011 at 13:16
  • @Cpfohl @user628347 look again; ** was in the original version Commented Feb 22, 2011 at 13:20

4 Answers 4

2

You are adding an onclick attribute to the HTML, this is only supposed to call a javascript function, NOT a server-side c# function.

It's been a while since I've used WebForms but as far as I remember the 'cell' object doesn't have a server-side click event. You will have to add a Button/LinkButton or something else and attach an event handler to that.

Sign up to request clarification or add additional context in comments.

2 Comments

so what i should do inorder to call the c# function?
See edit, or better still see Tim Schmelter's more complete answer instead.
1

I assume that the whole source-code is in a RowCreated-handler of a GridView and should allow to select a row via row-click. If i'm correct try this instead(converted from VB):

aspx:

<asp:GridView ID="GridView1" runat="server" OnRowCreated="GridView1_RowCreated" onselectedindexchanged="GridView1_SelectedIndexChanged" onselectedindexchanging="GridView1_SelectedIndexChanging" />

Codebehind

private void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow) {
        e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
        e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
        e.Row.ToolTip = "click to select row";
        e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
    }
}

Have a look here for further informations regarding the SelectedIndexChanged- and SelctedIndexChanging-Events from GridView.

Comments

0

The only way to call a server-side function from the client is with:

  • A post
  • AJAX (XmlHttpRequest)
  • A postback

Hope this helped.

Comments

0

You can call a code behind page method with Ajax and Javascript by decorating the methods with a WebMethod attribute. There is a tutorial here to do it

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.