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