2012-10-12 2 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> 

코드는 격자보기의 열 중 하나입니다. 내가 그 버튼을 클릭하면, 그 버튼을 저장할 줄 문자열 변수의 모든 datas를 포함합니다. like string slno = ""; slno = gridview1.cells [0]. 텍스트; (나는 그것이 정확하지 않거나 확실하지 않다) plz 누군가는 저를 돕는다 ??

+0

때문에 작업 흐름, 입니다. 심지어 sendrequest.aspx 페이지를 통과해야하는 클릭 된 행 데이터를 가져와야합니다. 가능합니까 ?? – cgsabari

답변

0

: 사전에

덕분에이

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; 
} 
을 수행에서 코드 숨김 해당 이벤트 내부 당신의 GridView
2 단계로 이런 일을

1 단계 와이어 최대 RowCommand 이벤트 녹화를 수행합니다


업데이트 :

e.CommandSource 정의

명령의 원본을 나타내는 System.Object 클래스의 인스턴스입니다.


IButtonControl.CommandArgument 정의

CommandArgument 속성과 명령 이벤트로 전파되는 인수 및 명령 이름을 지정하는 명령 이름 속성을 구현해야합니다은 IButtonControl 인터페이스를 구현하는 제어 할 수 있습니다.

+0

와트는 e.CommandSource와 e.CommandArgument 사이에서 다른가요 ?? – cgsabari

+0

k. 감사합니다 Mr.naveen :) 한가지 더 말할 수 있습니다. 나는 제안 된 코드를 추가했다. 오류가 발생하지 않습니다. 이 clickedSLNo 값을 sendrequest.aspx 페이지에 전달해야합니다. 패스하는 법. 시도했습니다. 세션 [ "slno"] = clickedSLNo.ToString(); 하지만 작동하지 않습니다 ... 다른 방법이 있습니까 ??? – cgsabari

+0

querystring을 사용할 수 있습니다 :'Response.Redirect (String.Format ("sendrequest.aspx? slno = {0}", clickedSLNo)); ' – codingbiz

1

msdn에서 확인해야합니다. 내가이 페이지를 sendrequest.aspx 간다있는 sendRequest 버튼을 클릭하면 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

예. 그 의례 Mr.user829081. 이제 선택한 행 데이터를 다른 페이지 인 sendrequest.aspx로 전달해야합니다. 패스하는 법. ?? 나는 그 데이터를 sendrequest 페이지에 표시해야한다. 수행하는 방법?? 이 방법이 있습니까? – cgsabari

+0

그리드 이벤트에서 값을 세션에 저장합니다. 그런 다음 sendrequest.aspx로 이동하여 응답을 리디렉션합니다. 마지막으로이 페이지에서이 값을 세션에서 가져옵니다. – user829081

관련 문제