【问题标题】:Getting datas from gridview for selected row?从选定行的gridview获取数据?
【发布时间】:2012-10-02 22:33:01
【问题描述】:

我是 asp.net 的新手。我正在做一个项目。因为我使用了一个 gridview 并使用 sqldatasource 从数据库中获取数据。 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>

代码是网格视图的列之一。如果我单击该按钮,我必须将该按钮存储在字符串变量中包含所有数据的行。喜欢 字符串 slno=""; slno=gridview1.cells[0].text; (我不确定它是否正确) 请问有人帮帮我吗??

提前感谢:)

【问题讨论】:

  • 所以工作流程是,当我单击 SendRequest 按钮时,它会转到 sendrequest.aspx 页面。甚至必须获取必须通过该 sendrequest.aspx 页面的单击行数据。有可能吗??

标签: asp.net


【解决方案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 + ".";

}
}

【讨论】:

  • 是的。其仪式 Mr.user829081。现在我必须将这些选定的行数据传递到另一个页面,即 sendrequest.aspx。怎么过.??我必须在 sendrequest 页面上显示这些数据。怎么做??有什么办法吗?
  • 在网格事件中,您将值存储在会话中。之后使用响应重定向去 sendrequest.aspx。最后在此页面中从会话中获取此值。
【解决方案2】:

做这些事情

步骤 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 事件的参数和命令名称。

【讨论】:

  • e.CommandSource 和 e.CommandArgument 的区别是什么??
  • k.谢谢你 Mr.naveen :) 你能再说一件事吗?我已经添加了你建议的代码。它不会出错。我必须将此 ClickedSLNo 值传递给 sendrequest.aspx 页面以显示。如何通过。我试过 Session["slno"]=clickedSLNo.ToString();但它不起作用......还有其他方法吗???
  • 可以使用查询字符串:Response.Redirect(String.Format("sendrequest.aspx?slno={0}", clickedSLNo));
  • k 先生.codingbiz。但我必须将该数据分配给一个变量。像字符串 slno=""; slno=点击的SLNo;怎么办?
  • 使用一个数组来存储你的行单元格的值,然后把这个数组放到会话中。会话必须工作。
猜你喜欢
  • 1970-01-01
  • 2010-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-13
  • 1970-01-01
  • 1970-01-01
  • 2016-03-16
相关资源
最近更新 更多