Why c# function is not working
if (e.Row.RowType == DataControlRowType.D开发者_如何学CataRow)
{
// 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
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.
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.
The only way to call a server-side function from the client is with:
- A post
- AJAX (XmlHttpRequest)
- A postback
Hope this helped.
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
精彩评论