求助:如何获取并修改gridview模板列中textbox的值?

拂晓的风 2008-05-21 10:14:52

后台代码
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.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=sa;database=lizi");
protected void Page_Load(object sender, EventArgs e)
{
this.DBing();
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{

}

private void DBing()
{
SqlDataAdapter da = new SqlDataAdapter("select * from users", con);
DataTable dt = new DataTable();
da.Fill(dt);
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{

}
protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
// switch (e.CommandName)
// {
// case "mycmd":
// //TextBox txt = (TextBox)GridView1.Rows[e.CommandArgument].Cells[2].FindControl("TextBox1");
// TextBox txt = (TextBox)GridView1.SelectedRow.Cells[2].FindControl("TextBox1");

// this.HiddenField1.Value = txt.Text.ToString();
// break;
// }
// //string txt = ((TextBox)(GridView1.Rows[].Cells[2].Controls[0])).Text.ToString().Trim();
// SqlCommand com = new SqlCommand("update users set [count]='" + HiddenField1.Value.ToString() + "' where id=" + e.CommandArgument + "");
// con.Open();
// com.ExecuteNonQuery();
// con.Close();
// this.DBing();


foreach (GridViewRow gvr in GridView1.Rows)
{
Int32 kh_jj = Convert.ToInt32(((TextBox)gvr.Cells[2].FindControl("TextBox1")).Text);
SqlCommand com = new SqlCommand("update users set [count]='" + kh_jj + "' where id=" + e.CommandArgument + "");
con.Open();
com.ExecuteNonQuery();
con.Close();

}
this.DBing();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
foreach (GridViewRow gvr in GridView1.Rows)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{

DataRowView row = (DataRowView)e.Row.DataItem;
string strID = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();//逐行获取得ID
Int32 kh_jj = Convert.ToInt32(((TextBox)gvr.Cells[2].FindControl("TextBox1")).Text);
}
}
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
//switch (e.Item.ItemType)
//{
// case ListItemType.Item:
// case ListItemType.AlternatingItem:
// case ListItemType.EditItem:
// TableCell myTableCell;
// myTableCell = e.Item.Cells[2];
// LinkButton myDeleteButton;
// myDeleteButton = (LinkButton)myTableCell.Controls[1];
// TextBox txt = (TextBox)myTableCell.FindControl("TextBox1");
// break;
// default:
// break;
//}
}
}


前台代码

<%@ 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>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" Height="1px"
OnSelectedIndexChanging="GridView1_SelectedIndexChanging" Width="404px" DataKeyNames="id" OnRowCreated="GridView1_RowCreated" OnRowDataBound="GridView1_RowDataBound">
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<Columns>
<asp:BoundField DataField="id" HeaderText="id" />
<asp:BoundField DataField="name" HeaderText="name" />
<asp:TemplateField HeaderText="count">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("count") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("count") %>'></asp:TextBox>
<asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Eval("id") %>'
CommandName="mycmd" OnCommand="LinkButton1_Command">修改</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="White" ForeColor="#330099" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
</asp:GridView>
<asp:HiddenField ID="HiddenField1" runat="server" />

</div>
</form>
</body>
</html>



gridview里东西都是绑定好的。
数据库里有张users表 有id,name,count三个字段
我想在文本框里输入数字,点击linkbuttun修改数据库中的count字段
小弟实在不知道怎么办好了,请各路大侠帮忙解决,小弟不胜感激!
...全文
650 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ssgtyh011 2010-01-11
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 amandag 的回复:]
HTML code<asp:GridViewID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" OnRowUpdating="GridView1_RowUpdating"><Columns><asp:BoundFieldDataField="id" HeaderText="id"/><asp:BoundFieldDataField="name" HeaderText="name"/><asp:TemplateFieldHeaderText="count"><ItemTemplate><asp:TextBoxID="TextBox2" runat="server" Text='<%#Eval("count") %>'></asp:TextBox><asp:LinkButtonID="LinkButton1" runat="server" CommandName="update">修改</asp:LinkButton></ItemTemplate></asp:TemplateField></Columns></asp:GridView>
C# codeprotectedvoid GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection cn=new SqlConnection("server=.;uid=sa;pwd=sa;database=lizi");
SqlCommand com=new SqlCommand("update users set [count]= @count where id= @id", cn);
com.Parameters.AddWithValue("@count", ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox2"))).Text);
com.Parameters.AddWithValue("@id", GridView1.DataKeys[e.RowIndex].Value.ToString());
cn.Open();
com.ExecuteNonQuery();
cn.Close();
DBing();
}
[/Quote]

找了两天了。终于找到了。这个代码写的非常好,效率也高并且及其简单实用。真的很感谢作者。
amandag 2008-05-21
  • 打赏
  • 举报
回复
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" OnRowUpdating="GridView1_RowUpdating" >
<Columns>
<asp:BoundField DataField="id" HeaderText="id" />
<asp:BoundField DataField="name" HeaderText="name" />
<asp:TemplateField HeaderText="count">
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("count") %>'></asp:TextBox>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="update">修改</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection cn = new SqlConnection("server=.;uid=sa;pwd=sa;database=lizi");
SqlCommand com = new SqlCommand("update users set [count]= @count where id= @id", cn);
com.Parameters.AddWithValue("@count", ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox2"))).Text);
com.Parameters.AddWithValue("@id", GridView1.DataKeys[e.RowIndex].Value.ToString());
cn.Open();
com.ExecuteNonQuery();
cn.Close();
DBing();
}
amandag 2008-05-21
  • 打赏
  • 举报
回复
给你的修改按钮CommandName为update,然后在RowUpdating事件中进行处理

拂晓的风 2008-05-21
  • 打赏
  • 举报
回复
2楼和3楼说的方法我都知道,关键是我不知道怎么个思路?
an3gsonnzhy 2008-05-21
  • 打赏
  • 举报
回复
FindControl("文本框id");找到文本框
treesky 2008-05-21
  • 打赏
  • 举报
回复
用RowEditing,RowUpdeting等事件嘛!
拂晓的风 2008-05-21
  • 打赏
  • 举报
回复

这是前台页面的截图

62,253

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧