This article is a mirror article of machine translation, please click here to jump to the original article.

View: 17292|Reply: 4

[Solved] How to get the Text property of a TextBox in the GridView template column

[Copy link]
Posted on 5/16/2015 9:45:57 AM | | | |
The Text attribute of the btnStart button is bound by Eval, how to get the current Text value of the button when clicking this button?

1.png (31.48 KB, Number of downloads: 492)





Previous:ASP.NET Detailed explanation of the verification controls
Next:In the dead of night, I always want to say something to someone.
Posted on 5/16/2015 12:06:45 PM |
I wrote it to you personally!!

Front Desk Code:
  1. <body>
  2.     <form id="form1" runat="server">
  3.     <div>
  4.         <asp:GridView ID="gvtest" runat="server" AutoGenerateColumns="False">
  5.             <Columns>
  6.                 <asp:TemplateField HeaderText="测试">
  7.                     <ItemTemplate>
  8.                         <asp:TextBox ID="txtname" runat="server" Text='<%# Eval("name") %>'></asp:TextBox>
  9.                     </ItemTemplate>
  10.                 </asp:TemplateField>
  11.             </Columns>
  12.         </asp:GridView>
  13.         <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
  14.         <br />
  15.     </div>
  16.     </form>
  17. </body>
Copy code
Background Code:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;

  7. namespace TestWeb
  8. {
  9.     public partial class zhi : System.Web.UI.Page
  10.     {
  11.         
  12.         public class jihe
  13.         {
  14.             public string name { set; get; }
  15.             public string pwd { set; get; }
  16.         }
  17.         protected void Page_Load(object sender, EventArgs e)
  18.         {
  19.             if (!IsPostBack) {

  20.                 List<jihe> list = new List<jihe>();
  21.                 jihe j = new jihe();
  22.                 j.name = "a";
  23.                 j.pwd = "123";
  24.                 list.Add(j);
  25.                 gvtest.DataSource = list;
  26.                 gvtest.DataBind();
  27.             }
  28.         }

  29.         protected void Button1_Click(object sender, EventArgs e)
  30.         {
  31.             string i=(gvtest.Rows[0].Cells[0].FindControl("txtname") as TextBox).Text;
  32.             Response.Write(i);
  33.         }
  34.     }
  35. }
Copy code
The most critical sentence is:
string i=(gvtest. Rows[0]. Cells[0]. FindControl("txtname") as TextBox). Text;

Posted on 5/16/2015 12:07:40 PM |
If the problem has been solved, please change the category to self-solved, and if you don't understand, please continue to ask.
Posted on 5/16/2015 12:30:23 PM |
If you get the selection, change the 0 in the rows to n
 Landlord| Posted on 5/16/2015 2:20:22 PM |
Xiao Zhazha Posted on 2015-5-16 12:07
If the problem has been solved, please change the category to self-solved, and if you don't understand, please continue to ask.

Where is it changed to solved?
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com