locked
How to create a link in a a gridview where when its clicked data is displayed in text boxe RRS feed

  • Question

  • User1717218719 posted

     I am looking to turn the first column of the gridview into a link that displays the data from row  in a textboxe.

    For example:

    ID RecType Fee
    1 H 0
    2 H 0

    when ID link clicked......

    (TextBox1)

    ID: 1

    RecType: H

    Fee: 0

    How would I make it so that when I click "1" in ID is displays the data I want?

    The code I have tried so far:

    <asp:gridview ID="GridView1" runat="server">
    
        <columns>
                    
              <asp:boundfield datafield="ID" 
                headertext="ID"/>
    
        </columns>
    
    </asp:gridview>
    
    
    <asp:sqldatasource id="OrdersSqlDataSource"  
            selectcommand="SELECT [ID] FROM 808_X"
            connectionstring="server=localhost;database=northwind;integrated security=SSPI"
            runat="server">
          </asp:sqldatasource>

    Monday, April 8, 2019 8:50 AM

Answers

  • User-893317190 posted

    Hi E.RU,

    You could bind click event on the linkbuttons in the id column.

    In the click event, you could find the q,r,s,t ... column's value in the same gridview row and then set textbox's value to the value you get.

    Below is my code.

     public partial class WebForm2 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable table = new DataTable();
                    table.Columns.Add(new DataColumn("id", typeof(int)));
                    table.Columns.Add(new DataColumn("q", typeof(string)));
                    table.Columns.Add(new DataColumn("r", typeof(string)));
                    table.Columns.Add(new DataColumn("s", typeof(string)));
                    table.Rows.Add(1, "q1", "r1", "s1");
                    table.Rows.Add(2, "q2", "r2", "s3");
    
                    table.Rows.Add(3, "q3", "r3", "s3");
                    GridView1.DataSource = table;
                    GridView1.DataBind();
                }
            }
    
            protected void LinkButton1_Click(object sender, EventArgs e)
            {
                GridViewRow row =((LinkButton)sender).NamingContainer as GridViewRow;// get the gridviewrow where linkbutton is
                // here because I only have three line, I use rowIndex to find the textbox and  column index 
                // please change the index according to your situation, because you row index and column index may be different from mine
               (Form.FindControl("TextBox" + (row.RowIndex+1)) as TextBox).Text= row.Cells[row.RowIndex + 1].Text;  
            }
        }

    Aspx, here my textbox's id is regular ,textbox1, textbox2 ,textbox3 because I find the textbox use row index Form.FindControl("TextBox" + (row.RowIndex+1).

    If you use a different way to find the textbox , you could omit it.

    <form id="form1" runat="server">
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" Text='<%#Eval("id") %>' OnClick="LinkButton1_Click"></asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField  DataField="q"/>
                    <asp:BoundField  DataField="r"/>
                    <asp:BoundField  DataField="s"/>
                </Columns>
              
            </asp:GridView>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        </form>

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 9, 2019 3:08 PM

All replies

  • User-2054057000 posted

    Show us the GridView code of the .aspx page to?

    Monday, April 8, 2019 11:15 AM
  • User1717218719 posted
    ID a b Tax d e EffDte g h i j k l m n o p q r s t u v x y
    175 2 E UP 001 AB 160 999999 0 Y                                                           0 0 0 0 0 0 0 0
    176 3 F up 002 AB 161 999999 0 Y 0 0 0 0 0 0 0 0

    (TextBox1) (TextBox2) (TextBox3) (TextBox4)
    (TextBox5) (TextBox6) (TextBox7) (TextBox8)

    This is a sample of what it looks like (not the actual gridview).

    I want to be able to click 175 under the ID column and when I click the link textbox1 displays The text of the column q. Then when I click on the link 176 under the ID column textbox2 displays The text of the column r.

    Monday, April 8, 2019 11:29 AM
  • User-893317190 posted

    Hi E.RU,

    You could bind click event on the linkbuttons in the id column.

    In the click event, you could find the q,r,s,t ... column's value in the same gridview row and then set textbox's value to the value you get.

    Below is my code.

     public partial class WebForm2 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable table = new DataTable();
                    table.Columns.Add(new DataColumn("id", typeof(int)));
                    table.Columns.Add(new DataColumn("q", typeof(string)));
                    table.Columns.Add(new DataColumn("r", typeof(string)));
                    table.Columns.Add(new DataColumn("s", typeof(string)));
                    table.Rows.Add(1, "q1", "r1", "s1");
                    table.Rows.Add(2, "q2", "r2", "s3");
    
                    table.Rows.Add(3, "q3", "r3", "s3");
                    GridView1.DataSource = table;
                    GridView1.DataBind();
                }
            }
    
            protected void LinkButton1_Click(object sender, EventArgs e)
            {
                GridViewRow row =((LinkButton)sender).NamingContainer as GridViewRow;// get the gridviewrow where linkbutton is
                // here because I only have three line, I use rowIndex to find the textbox and  column index 
                // please change the index according to your situation, because you row index and column index may be different from mine
               (Form.FindControl("TextBox" + (row.RowIndex+1)) as TextBox).Text= row.Cells[row.RowIndex + 1].Text;  
            }
        }

    Aspx, here my textbox's id is regular ,textbox1, textbox2 ,textbox3 because I find the textbox use row index Form.FindControl("TextBox" + (row.RowIndex+1).

    If you use a different way to find the textbox , you could omit it.

    <form id="form1" runat="server">
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" Text='<%#Eval("id") %>' OnClick="LinkButton1_Click"></asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField  DataField="q"/>
                    <asp:BoundField  DataField="r"/>
                    <asp:BoundField  DataField="s"/>
                </Columns>
              
            </asp:GridView>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        </form>

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 9, 2019 3:08 PM
  • User1717218719 posted

    Thankyou !!

    Wednesday, April 10, 2019 8:24 AM