c#从gridview获取值

时间:2016-11-10 01:09:03

标签: c# asp.net gridview

           <asp:TemplateField HeaderText="Employee ID" HeaderStyle-BackColor="#0033cc" HeaderStyle-ForeColor="White" ItemStyle-Width="10%">
                <ItemTemplate>
                    <asp:Label runat="server" ID="ONE" Text='<%#Eval("REG_ID") %>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Name" HeaderStyle-BackColor="#0033cc" HeaderStyle-ForeColor="White" ItemStyle-Width="20%">
                <ItemTemplate>
                    <asp:Label runat="server" ID="TWO" Text='<%#Eval("Name") %>' />
                </ItemTemplate>
            </asp:TemplateField>
   <asp:TemplateField HeaderText="View" HeaderStyle-BackColor="DarkBlue" HeaderStyle-ForeColor="White" ItemStyle-Width="5%">
                <ItemTemplate>
                    <asp:LinkButton ID="report" class="btn btn-warning btn-sm" Text="Report to HR" runat="server" OnClick="report_Click" />
                </ItemTemplate>
            </asp:TemplateField>

以上是我的GridView的代码。我想在单击按钮时从GridView获取A和B的值。有任何想法如何执行此操作?

  protected void report_Click(object sender, EventArgs e)
{
    GridViewRow theClickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
    if (theClickedRow != null)
    {
        Label FIRST = theClickedRow.FindControl("ONE") as Label;
        Label SECOND = theClickedRow.FindControl("TWO") as Label;
        ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + FIRST + SECOND + "');", true);

    }
}

结果我总是得到:system web ui controlsystem web ui control

1 个答案:

答案 0 :(得分:0)

FIRSTSECOND是Label对象。当您致电"alert('" + FIRST + SECOND + "');"时,您正在打印实际对象而不是这些控件的值。

标签控件具有.Text属性,允许您访问文本内容值。

您的代码应更改为以下内容:

ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + FIRST.Text + SECOND.Text + "');", true);