locked
c# get gridview row value RRS feed

  • Question

  • User1821839919 posted
       <asp:TemplateField HeaderText="A" HeaderStyle-BackColor="#0033cc" HeaderStyle-ForeColor="White" ItemStyle-Width="7%">
                    <ItemTemplate>
                        <asp:Label runat="server" ID="ONE" Text='<%#Eval("a") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="B" HeaderStyle-BackColor="#0033cc" HeaderStyle-ForeColor="White" ItemStyle-Width="15%">
                    <ItemTemplate>
                         <asp:Label runat="server" ID="TWO" Text='<%#Eval("b") %>' />
                    </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>
      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);
    
            }
        }

    why result i get when click the button always empty?

    Thursday, November 10, 2016 1:27 AM

Answers

  • User-183374066 posted

    Above code returns you the row where button clicked

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
          <Columns>
              <asp:TemplateField HeaderText="A" HeaderStyle-BackColor="#0033cc" HeaderStyle-ForeColor="White" ItemStyle-Width="7%">
                    <ItemTemplate>
                        <asp:Label runat="server" ID="ONE" Text='<%#Eval("a") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="B" HeaderStyle-BackColor="#0033cc" HeaderStyle-ForeColor="White" ItemStyle-Width="15%">
                    <ItemTemplate>
                         <asp:Label runat="server" ID="TWO" Text='<%#Eval("b") %>' />
                    </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>
          </Columns>
      </asp:GridView>
     protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    GridView1.DataSource = new List<MyClass>()
                    {
                        new MyClass { a="1", b="1" },
                        new MyClass { a="2", b="2" }
                    };
    
                    GridView1.DataBind();
                }
            }
    
            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.Text + SECOND.Text + "');", true);
    
                }
            }
        }
    
        public class MyClass
        {
            public string a { get; set; }
            public string b { get; set; }
    
        }

    I just change the code i little bit 

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

    See details about name container

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 10, 2016 2:27 AM

All replies

  • User-1838255255 posted

    Hi kiang_yit,

    According to your description and code, I make a modify based on your code. Here is modified code:

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

    Best Regards,

    Eric Du

    Thursday, November 10, 2016 2:23 AM
  • User-183374066 posted

    Above code returns you the row where button clicked

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
          <Columns>
              <asp:TemplateField HeaderText="A" HeaderStyle-BackColor="#0033cc" HeaderStyle-ForeColor="White" ItemStyle-Width="7%">
                    <ItemTemplate>
                        <asp:Label runat="server" ID="ONE" Text='<%#Eval("a") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="B" HeaderStyle-BackColor="#0033cc" HeaderStyle-ForeColor="White" ItemStyle-Width="15%">
                    <ItemTemplate>
                         <asp:Label runat="server" ID="TWO" Text='<%#Eval("b") %>' />
                    </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>
          </Columns>
      </asp:GridView>
     protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    GridView1.DataSource = new List<MyClass>()
                    {
                        new MyClass { a="1", b="1" },
                        new MyClass { a="2", b="2" }
                    };
    
                    GridView1.DataBind();
                }
            }
    
            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.Text + SECOND.Text + "');", true);
    
                }
            }
        }
    
        public class MyClass
        {
            public string a { get; set; }
            public string b { get; set; }
    
        }

    I just change the code i little bit 

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

    See details about name container

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 10, 2016 2:27 AM