<body>
<form id="form1" runat="server">
<div>
<div>
<br /><br /><br />
<asp:GridView runat="server" ID="gv" AutoGenerateColumns="false" class="table table-striped table-advance table-hover" BorderWidth="2px">
<Columns>
<asp:BoundField DataField="#" HeaderText="#" HeaderStyle-Height="50px" HeaderStyle-Font-Bold="true" HeaderStyle-Width="50px" HeaderStyle-Font-Size="Medium" HeaderStyle-BackColor="#66ccff" />
<asp:BoundField DataField="Student_id" HeaderText="Student_id" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Size="Medium" HeaderStyle-BackColor="#66ccff" />
<asp:BoundField DataField="RegNo" HeaderText="RegNo" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Size="Medium" HeaderStyle-BackColor="#66ccff" />
<asp:BoundField DataField="Student Name" HeaderText="Student Name" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Size="Medium" HeaderStyle-BackColor="#66ccff" />
<asp:BoundField DataField="PhoneNO" HeaderText="PhoneNO" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Size="Medium" HeaderStyle-BackColor="#66ccff" />
<asp:BoundField DataField="EmailId" HeaderText="EmailId" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Size="Medium" HeaderStyle-BackColor="#66ccff" />
<asp:BoundField DataField="DOB" HeaderText="DOB" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Size="Medium" HeaderStyle-BackColor="#66ccff" />
<asp:BoundField DataField="DOJ" HeaderText="DOJ" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Size="Medium" HeaderStyle-BackColor="#66ccff" />
<asp:TemplateField HeaderText="Marks Obt" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Size="Medium" HeaderStyle-Width="160px" HeaderStyle-BackColor="#66ccff" >
<ItemTemplate>
<asp:TextBox ID="tb1" runat="server" class="form-control-smalltxtBox"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Max Marks" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Size="Medium" HeaderStyle-Width="160px" HeaderStyle-BackColor="#66ccff">
<ItemTemplate>
<asp:TextBox ID="tb2" runat="server" class="form-control-smalltxtBox"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br /><br />
<br /><br /><br />
</div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
</form>
我在
中创建了一个文本框<asp:TemplateField HeaderText="Marks Obt" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Size="Medium" HeaderStyle-Width="160px" HeaderStyle-BackColor="#66ccff" >
<ItemTemplate>
<asp:TextBox ID="tb1" runat="server" class="form-control-smalltxtBox"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
和.cs类
protected void Page_Load(object sender, EventArgs e)
{
if(! IsPostBack)
{
}
DataTable dt = Teacher_DataLayer.getStudent(3);
gvPop(dt);
}
public void gvPop(DataTable eList)
{
try
{
DataTable dtt = new DataTable();
DataRow drw = null;
int i = 0;
string id1 = "t1r";
string id2 = "t2r";
string sN = string.Empty;
string sId = string.Empty;
string sub = string.Empty;
string mG = string.Empty;
string mT = string.Empty;
string ed = string.Empty;
string et = string.Empty;
string eaid = string.Empty;
string regno = string.Empty;
string phoneno = string.Empty;
string emai = string.Empty;
string dt1=string.Empty;
string dt2=string.Empty;
dtt.Columns.Add(new DataColumn("#", typeof(string)));
dtt.Columns.Add(new DataColumn("Student_id", typeof(int)));
dtt.Columns.Add(new DataColumn("RegNo", typeof(string)));
dtt.Columns.Add(new DataColumn("Student Name", typeof(string)));
dtt.Columns.Add(new DataColumn("PhoneNO", typeof(string)));
dtt.Columns.Add(new DataColumn("EmailId", typeof(string)));
dtt.Columns.Add(new DataColumn("DOB", typeof(string)));
dtt.Columns.Add(new DataColumn("DOJ", typeof(string)));
dtt.Columns.Add(new DataColumn("Marks Obt", typeof(string)));
dtt.Columns.Add(new DataColumn("Max Marks", typeof(string)));
foreach (DataRow dr in eList.Rows)
{
int cc = 1;
foreach (object dc in dr.ItemArray)
{
String cd = null;
if (dc == null)
{
cd = "null";
}
else
{
cd = dc.ToString();
}
if (cc == 1)
{
sId = cd;
}
else if (cc == 2)
{
regno = cd;
}
else if (cc == 3)
{
sN = cd;
}
else if (cc == 4)
{
phoneno = cd;
}
else if (cc == 5)
{
emai = cd;
}
else if (cc == 6)
{
dt1 = cd;
}
else if (cc == 7)
{
dt2 = cd;
}
else if (cc == 8)
{
mG = cd;
}
cc = cc + 1;
}
i = i + 1;
drw = dtt.NewRow();
drw["#"] = i;
drw["Student_id"] = Convert.ToInt32(sId);
drw["RegNo"] = regno;
drw["Student Name"] = sN;
drw["PhoneNO"] = phoneno;
drw["EmailId"] = emai;
drw["DOB"] = dt1;
drw["DOJ"] = dt2;
drw["Marks Obt"] = mG;
//drw["Max Marks"] = maxM;
dtt.Rows.Add(drw);
// dtt.Columns.Clear();
}
ViewState["CurrentTable"] = dtt;
gv.DataSource = dtt;
gv.DataBind();
SetPreviousData();
}
catch (Exception ex) { }
}
public void SetPreviousData()
{
try
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt1 = (DataTable)ViewState["CurrentTable"];
if (dt1.Rows.Count > 0)
{
for (int i = 0; i < dt1.Rows.Count; i++)
{
TextBox box1 = (TextBox)gv.Rows[rowIndex].Cells[8].FindControl("tb1");
TextBox box2 = (TextBox)gv.Rows[rowIndex].Cells[9].FindControl("tb2");
box1.Text = dt1.Rows[i]["Marks Obt"].ToString();
//box2.Text = maxM;
rowIndex++;
}
}
}
}
catch (Exception ep) { }
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
DataTable dtN = new DataTable();
DataRow drw = null;
int rowIndex = 0;
//dtN.Columns.Add(new DataColumn("#", typeof(string)));
//dtN.Columns.Add(new DataColumn("Student_id", typeof(int)));
//dtN.Columns.Add(new DataColumn("RegNo", typeof(string)));
//dtN.Columns.Add(new DataColumn("Student Name", typeof(string)));
//dtN.Columns.Add(new DataColumn("PhoneNO", typeof(string)));
//dtN.Columns.Add(new DataColumn("EmailId", typeof(string)));
//dtN.Columns.Add(new DataColumn("DOB", typeof(string)));
//dtN.Columns.Add(new DataColumn("DOJ", typeof(string)));
//dtN.Columns.Add(new DataColumn("Marks Obt", typeof(string)));
//dtN.Columns.Add(new DataColumn("Max Marks", typeof(string)));
if (ViewState["CurrentTable"] != null)
{
DataTable dtV = (DataTable)ViewState["CurrentTable"];
int ww= dtV.Rows.Count;
if (dtV.Rows.Count > 0)
{
for (int i = 0; i < dtV.Rows.Count; i++)
{
string sid = gv.Rows[rowIndex].Cells[1].Text;
int ss = Convert.ToInt32(sid);
string nn= gv.Rows[rowIndex].Cells[3].Text;
string ttt= gv.Rows[rowIndex].Cells[8].Text;
TextBox box1 = (TextBox)gv.Rows[rowIndex].Cells[8].FindControl("tb1");
string mo = box1.Text;
}
}
}
}
catch (Exception ex)
{
}
}
在按钮上单击它无法找到文本框的控件我需要保存在文本框和student_id中输入的数据。
答案 0 :(得分:0)
如果在TemplateField内部创建了一个文本框,则该按钮的click事件不会调用该文本框,因为该控件在网格视图中绑定。
您必须调用行数据绑定事件并从文本框中获取值 as: -
步骤1: - 在网格上添加onrowdatabound =&#34; gv_RowDataBound&#34;
在这一行网格中
第2步: - 在c#end中创建
protected void gv_RowCommand(object sender,GridViewCommandEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
//在这里添加逻辑
}
}
另外,你可以通过jquery和ajax调用来高效地完成这项工作。