2011-05-07 45 views
2

我有一個背後的c#代碼的aspx頁面的gridview。當用戶點擊GridView行時,如何調用函數後面的代碼?

當用戶點擊某一行的任何地方時,我可以如何運行代碼後面的代碼?現在,我使用選擇按鈕。但是用戶必須點擊該按鈕。我希望用戶能夠點擊某一行的任何位置,以顯示位於主GridView旁邊的另一個GridView中的詳細信息。

有關如何做到這一點的一些想法?

謝謝!

回答

2

試試這個

<asp:GridView runat="server" ID="GridView1" DataKeyNames="ID" AutoGenerateColumns="False" 
     Font-Names="Tahoma" Font-Size="Small" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" 
     OnRowDataBound="GridView1_RowDataBound"> 
     <Columns> 
      <asp:TemplateField HeaderText="Row"> 
       <ItemTemplate> 
        <%# Container.DataItemIndex+1 %> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:BoundField DataField="FirstName" HeaderText="First Name" /> 
      <asp:BoundField DataField="LastName" HeaderText="Last Name" /> 
      <asp:CommandField ShowSelectButton="true" ButtonType="Link" Visible="false" SelectText="Enroll" /> 
     </Columns> 
    </asp:GridView> 


protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      GridView1.DataSource = Enumerable.Range(1, 10).Select(a => new 
      { 
       ID = a, 
       FirstName = String.Format("First {0}", a), 
       LastName = String.Format("Last{0}", a) 
      }); 
      GridView1.DataBind(); 
     } 
    } 
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) 
    { 

     //Bind other grid 

     Response.Write(GridView1.SelectedIndex+1); 

    } 
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      //add css to GridViewrow based on rowState 
      e.Row.CssClass = e.Row.RowState.ToString(); 
      //Add onclick attribute to select row. 
      e.Row.Attributes.Add("onclick", String.Format("javascript:__doPostBack('GridView1','Select${0}')", e.Row.RowIndex)); 
     } 
    } 
+1

感謝的人,它的工作! – ThdK 2011-05-07 14:56:42

相關問題