如何在GridView中获取文本框的值? [英] How to fetch the value of textbox in gridview?

查看:84
本文介绍了如何在GridView中获取文本框的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下:

My code follows:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >


<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField DataField="StudentId" HeaderText="StudentId" />
                <asp:BoundField DataField="StudentName" HeaderText="StudentName" />
                
                <asp:TemplateField HeaderText="Marks">
               
                    <ItemTemplate>
                        
                         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="MarksObtn">
                    <ItemTemplate>
                         <br />
                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
          <br />
        <br />
        <br />
                               
                 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
            Text="Submit" Width="205px" /></div>
    </form>
</body>
</html>


背后的代码:


Code behind:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindGrid();
    }

    public void BindGrid()
    {
        string constring = "Data Source=BIZ99921-46A973;Initial Catalog=cities;Integrated Security=True";
        string sql = "select StudentId,StudentName from student";
        SqlConnection con = new SqlConnection(constring);
        DataTable amol = new DataTable();
        SqlDataAdapter sda = new SqlDataAdapter(sql, con);
        sda.Fill(amol);
        GridView1.DataSource = amol;
        GridView1.DataBind();
     }
   protected void Button1_Click(object sender, EventArgs e)
    { 
string t = (TextBox)GridView1.Rows[0].Cells[3].Controls[0].Text;
          
            Response.Write(t);
   }
}



当我单击按钮时,我想在字符串t中获取文本框的值.



When I click the button, I want to fetch the value of the textbox in the string t. How can I do it?

推荐答案

写道:​​

字符串t =(TextBox )GridView1.Rows [0] .Cells [3] .Controls [0] .Text;

string t = (TextBox)GridView1.Rows[0].Cells[3].Controls[0].Text;



这很丑陋,但是如果在方括号周围放置方括号,则看起来应该可以使用,以便在检查Text属性之前应用它.当然,如果您始终检查第一行的文本,那么具有网格的意义是什么?



This is ugly but it looks like it should work, if you put brackets around the cast, so that it is applied before you check the Text property. Of course, what''s the point of having a grid if you always check the text of the first row ?


代码在下面,

code is below,

((TextBox)(GridView1.Rows[0].Cells[3].Controls[1])).Text


这篇关于如何在GridView中获取文本框的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆