2012-06-13 5 views
1

Я хочу, чтобы asp.net gridview со своими рядами можно было щелкнуть.clickable grid view row asp.net

Я хочу вызвать функцию, когда эта строка нажата на основе индекса строки.

я пытался использовать RowDataBound событие, но оно не работало или меня

я использовал следующий код

protected void PeopleGridView_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';"; 
      e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';"; 

      e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.PeopleGridView, "Select$" + e.Row.RowIndex); 
     } 
    } 

, где я буду неправильно?

Я не хочу перенаправлять на любую другую страницу. Я хочу, чтобы заполнить значения в текстовом поле на той же странице

ответ

0

Вы можете сделать функцию в javascript и называть это событием мыши из строки.

Javacript

<script language="javascript" type="text/javascript"> 
    function setStyle(obj) 
    { 
     obj.style.cursor='hand'; 
     obj.style.textDecoration='underline'; 
    } 

    function resetStyle(obj) 
    { 
     this.style.textDecoration='none'; 
    } 
</script> 

Код За

protected void PeopleGridView_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     e.Row.Attributes["onmouseover"] = "setStyle(this);"; 
     e.Row.Attributes["onmouseout"] = "resetStyle(this);"; 

     e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.PeopleGridView, "Select$" + e.Row.RowIndex); 
    } 
} 
0

Попробуйте

<script type="text/javascript" language="javascript"> 
     function call(id) { 
      alert(id); 
      // Do whatever 
     } 
    </script> 

<asp:GridView ID="gvParent" DataKeyNames="ID" runat="server" PageSize="10" AllowPaging="true" 
      PagerSettings-Mode="NextPrevious" AutoGenerateColumns="False" OnRowDataBound="gvParent_RowDataBound"> 
      <Columns> 
       <asp:BoundField DataField="Name" /> 
       <asp:TemplateField> 
        <ItemTemplate> 
         <asp:HiddenField ID="HdnID" runat="server" Value='<%# Eval("ID") %>' /> 
        </ItemTemplate> 
       </asp:TemplateField> 
      </Columns> 
     </asp:GridView> 

код за

protected void gvParent_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      string ID = ((HiddenField)e.Row.FindControl("HdnID")).Value; 

      e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';"; 
      e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';"; 

      e.Row.Attributes["onclick"] = "call(" + ID + ");"; 
     } 
}