asp.net C# passing value from gridview to texbox on button click

I have a simple gridview with button for each row, and I want to pass or display the value of user_full_name_ar in a label, I tried doing this using javascript function as shown below but it doesn’t show the data it shows null,

gridview code:

<asp:Label ID="Label5" Text='transfer text here' runat ="server" />

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="user_name" DataSourceID="SqlDataSource1" Height="100px" Width="383px">
    <Columns>
        <asp:BoundField DataField="user_name" HeaderText="user_name" ReadOnly="True" SortExpression="user_name" />
        <asp:BoundField DataField="user_full_name_ar" HeaderText="user_full_name_ar" SortExpression="user_full_name_ar" />

        <asp:TemplateField HeaderText="user_full_name_ar"  SortExpression="user_full_name_ar">
           <ItemTemplate>
                <asp:Label ID="Label4" Text='<%# Session["lang"].ToString() == "en"? Eval("user_full_name_en") : Eval("user_full_name_ar")  %>' runat ="server" />
           </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField>
           <ItemTemplate>
            <asp:Button ID="ButtonSearch" runat="server" text="select"  OnClientClick ="txt();" CommandArgument ='<%# Bind("user_name") %>' />
            </ItemTemplate>
        </asp:TemplateField>

    </Columns>
</asp:GridView>

javascript function to display value of label4 in gridview to label5 outside gridview :

<script  type ="text/javascript" >
    function txt() {

        var t = document.getElementById("Label4");
        document.getElementById("Label5") = t.value;
    }
</script>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

you can use a CommandField in gridview

 <asp:Label ID="Label5" Text='transfer text here' runat ="server" />

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="user_name" DataSourceID="SqlDataSource1" Height="100px" Width="383px">
    <Columns>
    <asp:CommandField HeaderText="select" SelectText="select " ShowSelectButton="True">
                          </asp:CommandField>
        <asp:BoundField DataField="user_name" HeaderText="user_name" ReadOnly="True" SortExpression="user_name" />
        <asp:BoundField DataField="user_full_name_ar" HeaderText="user_full_name_ar" SortExpression="user_full_name_ar" />

              <asp:TemplateField HeaderText="user_full_name_ar"  SortExpression="user_full_name_ar">
                   <ItemTemplate>
                        <asp:Label ID="Label4" Text='<%# Session["lang"].ToString() == "en"? Eval("user_full_name_en") : Eval("user_full_name_ar")  %>' runat ="server" />
                   </ItemTemplate>
                   </asp:TemplateField>

        <asp:TemplateField>
       <ItemTemplate>
<asp:Button ID="ButtonSearch" runat="server" text="select"  OnClientClick ="txt();" CommandArgument ='<%# Bind("user_name") %>' />
         </ItemTemplate>
         </asp:TemplateField>

    </Columns>
</asp:GridView>

after in selectedindexchenge event write code below

Label5.Text = GridView1.SelectedRow.Cells[2].Text;


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x