2011-02-22 70 views
1

我有一個Default.aspx頁面,其中我綁定了一個Grid。網格中有一個名爲Details的按鈕。在頁面間傳遞一個ID

我也有一個Details.aspx它有一個GridView。如果我點擊Default.aspx中存在的按鈕,將出現一個Details.aspx頁面,該頁面顯示爲空。

單擊特定行的詳細信息按鈕時,會出現該按鈕被點擊的詳細信息。我不知道如何將ID傳遞給Details.aspx

有人可以指導我這個嗎?

Details.aspx

<asp:GridView ID="DetailsGridView" runat="server" BackColor="White" AutoGenerateColumns="false" 
      BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="ID" 
      ForeColor="Black" GridLines="Vertical"> 
      <FooterStyle BackColor="#CCCC99" /> 
     <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" /> 
     <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" /> 
     <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" /> 
     <AlternatingRowStyle BackColor="White" /> 
     </asp:GridView> 

的Default.aspx:

  <Columns> 
       <asp:BoundField HeaderText="First Name" DataField="FirstName" /> 
       <asp:BoundField HeaderText="Last Name" DataField = "LastName" /> 
       <asp:BoundField HeaderText="HomePhoneNumber" DataField="HomePhoneNumber" /> 
       <asp:TemplateField HeaderText="ViewDetails"> 
       <ItemTemplate> 
       <asp:Button ID="Deatils" runat="server" Text="Details" /> 
       </ItemTemplate>  

       </asp:TemplateField> 

       <asp:TemplateField HeaderText="Actions"> 
       <ItemTemplate> 
       <asp:Button ID="Modify" runat="server" Text="Modify" /> 
       <asp:Button ID="Delete" runat="server" Text="Delete" /> 

       </ItemTemplate> 
       </asp:TemplateField>     
      </Columns> 


      <FooterStyle BackColor="#CCCC99" /> 
      <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" /> 
      <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" /> 
      <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" /> 
      <AlternatingRowStyle BackColor="White" /> 
     </asp:GridView> 
    </div> 

的JavaScript

<script type="text/javascript"> 
function viewProfile(index) 
{ 
     var GridID = document.getElementById("PersonGridView"); 
     var row=GridID.rows[parseInt(index)+1]; 
     window.open('Details.aspx?coaid'+row); 
} 
</script> 

背後的Default.aspx的代碼:

protected void PersonGridView_RowDataBound(object sender, GridViewRowEventArgs e) 
     { 
      if (e.Row.RowType == DataControlRowType.DataRow) 
      { 
       var DataKeys = PersonGridView.DataKeys[e.Row.RowIndex]; 
       DataKeys.Value.ToString(); 
       var ID = DataKeys["ID"].ToString(); 
       Button btnDetails = (Button)e.Row.FindControl("Deatils"); 
       Button btnModify = (Button)e.Row.FindControl("Modify"); 
       Button btnDelete = (Button)e.Row.FindControl("Delete"); 
       btnModify.CommandName = "Modify"; 
       btnDelete.CommandName = "Delete"; 
       btnDetails.CommandName = "Deatils"; 
       btnDelete.CommandArgument = btnModify.CommandArgument = btnDetails.CommandArgument = string.Format("{0}", ID); 
       btnDetails.Attributes["onclick"] = string.Format("viewProfile({0}); return false;", e.Row.RowIndex); 
      } 
     } 
+0

你可以將它作爲表單請求或查詢字符串 – kobe 2011-02-22 22:01:29

+0

@gov,我實際上不知道從哪裏通過。請您詳細說明一下? – 2011-02-22 22:03:49

回答

2

編輯:我修改我的例子,不使用HyperLink,而是將建立一個LinkButton,可以瀏覽到您的Details.aspx頁面傳遞ID值的查詢字符串:

Default.aspx的:

<asp:TemplateField> 
    <ItemTemplate> 
     <asp:LinkButton CommandName="Details" CommandArgument='<%# Eval("ID") %>' Text="Details" runat="server" /> 
    </ItemTemplate> 
</asp:TemplateField> 

Default.aspx.cs:

protected void PersonGridView_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    if (e.CommandName == "Details") 
    { 
     Server.Transfer("Details.aspx?ID=" + e.CommandArgument.ToString()); 
    } 
} 
+0

如果我有一個按鈕? :/ – 2011-02-22 22:34:29

+0

我修改了我的示例以使用GridView和LinkBut​​ton的一部分。至於實際的按鈕,我認爲你將不得不使用一些JavaScript和一個隱藏的領域。 – CAbbott 2011-02-23 14:57:26

+0

@CAbbott,@Abid Ali - LinkBut​​ton和Button都有CommandName和CommandArgument參數。這個代碼是可以互換的。 – 2011-02-23 15:05:21

0

在網格上,當你點擊按鈕

做以下

public protected sub Button_Click(EventArgs e) 
{ 

if (grid.SelectedItem != null) 
{ 

var selectedRow = grid.SelectedItem ; 

// do what ever you want to Selected Row that may be binding it to another Form etc. 
} 

執行相同的OnSelectedEvent,如果你想改爲這樣做。

public protected sub OnSelectedEvent_Click(EventArgs e) 
{ 

if (grid.SelectedItem != null) 
{ 

var selectedRow = grid.SelectedItem ; 

// do what ever you want to Selected Row that may be binding it to another Form etc. 
} 
+0

沒有OnSelectedEvent顯示在網格事件中:/:s – 2011-02-22 22:19:22

+0

@CAbbott您的代碼很有幫助..謝謝。但..通過做href,名字消失和詳細信息href出現..另一個問題是我沒有在我的網格中顯示我的ID列。 – 2011-02-22 22:28:15