2012-10-12 155 views
1

我是新來的asp.net。我正在做一個項目。因爲我使用了一個gridview並使用sqldatasource從數據庫中獲取數據。 gridview的代碼是從gridview獲取選定行的數據?

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataSourceID="Library" CellPadding="4" ForeColor="#333333" 
    GridLines="None" OnRowDataBound="GridView1_RowDataBound" > 
    <RowStyle BackColor="#EFF3FB" /> 
    <Columns> 
     <asp:BoundField DataField="Sl_No" HeaderText="Sl_No" SortExpression="Sl_No" /> 
     <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" /> 
     <asp:BoundField DataField="Author" HeaderText="Author" 
      SortExpression="Author" /> 
     <asp:BoundField DataField="Publication" HeaderText="Publication" 
      SortExpression="Publication" /> 
     <asp:BoundField DataField="Available" HeaderText="Status" 
      SortExpression="Available" /> 
     <asp:TemplateField HeaderText="Availability"> 
      <ItemTemplate> 
       <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Available") %>' /> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:BoundField DataField="RIUserTaken" HeaderText="RIUserTaken" 
      SortExpression="RIUserTaken" Visible="False" /> 
     <asp:TemplateField HeaderText="Taken By" ShowHeader="False"> 
      <ItemTemplate> 
       <asp:Label ID="Label4" runat="server" Text='<%# Eval("RIUserTaken", "{0}") %>'></asp:Label> 
       <asp:Button ID="SendRequest" runat="server" Text="Button" Visible="False" 
        onclick="SendRequest_Click" CommandName="SendRequestCmd" /> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Taken Date" InsertVisible="False" 
      ShowHeader="False"> 
      <ItemTemplate> 
       <asp:Label ID="Label3" runat="server" Text='<%# Eval("TakenDate") %>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> 
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> 
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
    <EditRowStyle BackColor="#2461BF" /> 
    <AlternatingRowStyle BackColor="White" /> </asp:GridView> 

代碼是網格視圖的列中的一個。如果我點擊那個按鈕,我必須存儲該按鈕包含所有數據在字符串變量中的行。像 string slno =「」; slno = gridview1.cells [0] .text; (我不知道這是否正確) PLZ任何人都可以幫助我?

在此先感謝:)

+0

這樣的工作流程是, 當我點擊按鈕sendRequest將它轉到頁sendrequest.aspx。甚至必須獲取必須通過sendrequest.aspx頁面的點擊行數據。 是否有可能? – cgsabari

回答

0

做這些事情

步驟1線了RowCommand EVNT到你的GridView
步驟2.內部的事件在代碼隱藏,這樣做

if(e.CommandName.Equals("SendRequestCmd")) 
{ 
    var clickedRow = ((Button)e.CommandSource).NamingContainer as GridViewRow; 
    // now access the cells like this 
    var clickedSLNo = clickedRow.cells[0].Text; 
} 


更新:

e.CommandSource定義

System.Object類的一個實例,它表示命令的來源。


IButtonControl.CommandArgument定義

實現了IButtonControl接口必須實現CommandArgument屬性和CommandName屬性來表示傳播到Command事件的參數和命令名稱的控制。

+0

wat是e.CommandSource和e.CommandArgument之間的區別? – cgsabari

+0

k。謝謝你Mr.naveen :)你可以再說一件事。我已經添加了你建議的代碼。它不會產生錯誤。我必須將此ClickedSLNo值傳遞給sendrequest.aspx頁面才能顯示。如何通過。我試過 Session [「slno」] = clickedSLNo.ToString(); 但它不工作... 是否有任何其他方式? – cgsabari

+0

你可以使用querystring:'Response.Redirect(String.Format(「sendrequest.aspx?slno = {0}」,clickedSLNo));' – codingbiz

1

從msdn,你應該檢查這個。 GridView.SelectedIndexChanged event

void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e) 
{ 
// Get the currently selected row using the SelectedRow property. 
GridViewRow row = CustomersGridView.SelectedRow; 

// Display the company name from the selected row. 
// In this example, the third column (index 2) contains 
// the company name. 
MessageLabel.Text = "You selected " + row.Cells[2].Text + "."; 
} 

void CustomersGridView_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e) 
{ 

// Get the currently selected row. Because the SelectedIndexChanging event 
// occurs before the select operation in the GridView control, the 
// SelectedRow property cannot be used. Instead, use the Rows collection 
// and the NewSelectedIndex property of the e argument passed to this 
// event handler. 
GridViewRow row = CustomersGridView.Rows[e.NewSelectedIndex]; 

// You can cancel the select operation by using the Cancel 
// property. For this example, if the user selects a customer with 
// the ID "ANATR", the select operation is canceled and an error message 
// is displayed. 
if (row.Cells[1].Text == "ANATR") 
{ 

    e.Cancel = true; 
    MessageLabel.Text = "You cannot select " + row.Cells[2].Text + "."; 

} 
} 
+0

ya。它的儀式先生用戶829081。現在我必須將這些選定的行數據傳遞到另一個sendrequest.aspx頁面。如何通過。我必須在sendrequest頁面上顯示這些數據。怎麼做?? 任何方式來這個? – cgsabari

+0

在網格事件中,您將值存儲在會話中。之後使用響應重定向去sendrequest.aspx。 最後在本頁面中從會話中獲取此值。 – user829081