2011-12-11 119 views
0

我正在使用ASP.NET 4和MVC並嘗試添加代碼來處理「選擇」鏈接。一旦用戶選擇該行,我需要顯示更多詳細信息的新頁面。我試圖捕獲rowcommand,但我從來沒有碰到過斷點。這是第一頁我當前的GridView控件代碼 -GridView - 句柄選擇鏈接

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<ValidationPortal.Models.Contact>>" %> 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 

    <form id="form1" runat="server"> 

    <h2>Index</h2> 

    <p> 
     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
      DataKeyNames="Id" DataSourceID="SqlDataSource1"> 
      <Columns> 
       <asp:CommandField ShowSelectButton="True" /> 
       <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" 
        ReadOnly="True" SortExpression="Id" /> 
       <asp:BoundField DataField="FirstName" HeaderText="FirstName" 
        SortExpression="FirstName" /> 
       <asp:BoundField DataField="LastName" HeaderText="LastName" 
        SortExpression="LastName" /> 
       <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" /> 
       <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" /> 
      </Columns> 
     </asp:GridView> 
     <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
      ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
      SelectCommand="SELECT * FROM [Contacts]"></asp:SqlDataSource> 

    </p> 
    </form> 

</asp:Content> 

什麼我需要添加,使這項工作?

回答

2

我認爲你需要在網格標記中指定一個OnSelectedIndexChanged事件。您的活動可能只是使用所選項目的ID重定向到您的詳細信息頁面。這將是這個樣子:

網:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="Id" DataSourceID="SqlDataSource1" 
    OnSelectedIndexChanged="SelectEvent"> 
     // rest of grid definitions 
</asp:GridView> 

C#:

protected void SelectEvent(object sender, EventArgs e) 
{ 
    if (GridView1.SelectedIndex >= 0) 
     Response.Redirect("yourpage.aspx?itemid=" + (int)GridView1.SelectedValue); 
} 

就個人而言,你是做我會跳過選擇命令和一個鏈接,可以創建項目模板什麼如圖所示here

0

試試這個方法

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 
     if (e.CommandName == "Select") 
     { 
      // Do something 
     } 
    } 

如果你不想使用行命令,或者你想明確地火rowcommand然後使用下面的代碼段

int GridView1TotalCount = GridView1.Rows.Count; 
     int r = Convert.ToInt32(dslno)-1; 
     if (r != -1) 
     { 
      if (GridView1TotalCount > r) 
      { 

       GridViewRow thisGridView1 = GridView1.Rows[r]; 
       ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + thisGvEmpMaster.RowIndex); 
       // Do something here you have datakey like 
      int dkkey = Convert.ToString(GridView1.DataKeys[thisGridView1.RowIndex].Value); 


      } 
     }