如何获取Gridview中编辑字段的值?
protectedvoidDepartmentGridView_RowEditing(objectsender,GridViewEditEventArgse){Depar...
protected void DepartmentGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
DepartmentGridView.EditIndex = e.NewEditIndex;
BindDepartmentGrid();
}
protected void DepartmentGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string d_name = ((TextBox)DepartmentGridView.Rows[e.RowIndex].FindControl("txtD_Name")).Text;
int d_id = Convert.ToInt16(DepartmentGridView.DataKeys[e.RowIndex].Value);
int success = dbll.UpdateDepartment(d_name,d_id);
if (success == 1)
{
Response.Write("<script>alert('门类更新成功!')</script>");
DepartmentGridView.EditIndex = -1;
BindDepartmentGrid();
}
}
protected void DepartmentGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
Response.Write("<script>alert('取消更新')</script>");
DepartmentGridView.EditIndex = -1;
BindDepartmentGrid();
}
点编辑出现文本框,更新门类名称后点击更新,虽然跳出门类更新成功的提示,但数据根本没有写入数据库,
单步执行时发现d_name未获取到任何值,请高手帮帮忙!
好像找不到 txtD_Name 控件, d_name获取到的值是本来的值,
[e.RowIndex]鼠标放上去可以看到所编辑记录的行号,放到FindControl("txtD_Name")上时没任何显示 展开
{
DepartmentGridView.EditIndex = e.NewEditIndex;
BindDepartmentGrid();
}
protected void DepartmentGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string d_name = ((TextBox)DepartmentGridView.Rows[e.RowIndex].FindControl("txtD_Name")).Text;
int d_id = Convert.ToInt16(DepartmentGridView.DataKeys[e.RowIndex].Value);
int success = dbll.UpdateDepartment(d_name,d_id);
if (success == 1)
{
Response.Write("<script>alert('门类更新成功!')</script>");
DepartmentGridView.EditIndex = -1;
BindDepartmentGrid();
}
}
protected void DepartmentGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
Response.Write("<script>alert('取消更新')</script>");
DepartmentGridView.EditIndex = -1;
BindDepartmentGrid();
}
点编辑出现文本框,更新门类名称后点击更新,虽然跳出门类更新成功的提示,但数据根本没有写入数据库,
单步执行时发现d_name未获取到任何值,请高手帮帮忙!
好像找不到 txtD_Name 控件, d_name获取到的值是本来的值,
[e.RowIndex]鼠标放上去可以看到所编辑记录的行号,放到FindControl("txtD_Name")上时没任何显示 展开
推荐于2018-04-18 · 知道合伙人互联网行家
关注
展开全部
如果是主键,你可以到GridView里面去设定成如:
DataKeyNames="id"
<asp:GridView ID="gdvUserInfo" runat="server" AutoGenerateColumns="False" DataKeyNames="id">
获取的时候:
/// <summary>
/// 删除信息事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gdvUserInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id =gdvUserInfo.DataKeys[e.RowIndex].Value.ToString();
if (info.DeleteUserInfoById(id) > 0)
{
this.lblMessage.Text = "删除成功!";
LoadInfo();
}
else
{
this.lblMessage.Text = "删除失败!";
LoadInfo();
}
}
/// <summary>
/// 编辑事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gdvUserInfo_RowEditing(object sender, GridViewEditEventArgs e)
{
gdvUserInfo.EditIndex = e.NewEditIndex;
LoadInfo();
}
/// <summary>
/// 取消编辑事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gdvUserInfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gdvUserInfo.EditIndex = -1;
LoadInfo();
}
/// <summary>
/// 更新事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gdvUserInfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string userId = gdvUserInfo.DataKeys[e.RowIndex].Value.ToString();
TextBox txtUserId = (TextBox)gdvUserInfo.Rows[e.RowIndex].Cells[0].FindControl("txtUserId");
TextBox txtUserName = (TextBox)gdvUserInfo.Rows[e.RowIndex].Cells[0].FindControl("txtUserName");
TextBox txtPassWord = (TextBox)gdvUserInfo.Rows[e.RowIndex].Cells[0].FindControl("txtPassWord");
if (txtUserName != null && txtPassWord != null)
{
try
{
UserInfo user = info.GetUserInfoById(userId);
user.UserName = txtUserName.Text.ToString();
user.Password = txtPassWord.Text.ToString();
if (info.ModifyUserInfo(user) > 0)
{
this.lblMessage.Text = "修改成功!";
gdvUserInfo.EditIndex = -1;
LoadInfo();
}
else
{
this.lblMessage.Text = "修改失败!";
gdvUserInfo.EditIndex = -1;
LoadInfo();
}
}
catch (Exception)
{
this.lblMessage.Text = "修改失败!";
gdvUserInfo.EditIndex = -1;
LoadInfo();
}
}
}
/// <summary>
///
/// 光棒事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gdvUserInfo_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 0; i < gdvUserInfo.Rows.Count; i++)
{
if (gdvUserInfo.Rows[i].RowType == DataControlRowType.DataRow)
{
gdvUserInfo.Rows[i].Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#9cf'");
gdvUserInfo.Rows[i].Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}
}
DataKeyNames="id"
<asp:GridView ID="gdvUserInfo" runat="server" AutoGenerateColumns="False" DataKeyNames="id">
获取的时候:
/// <summary>
/// 删除信息事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gdvUserInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id =gdvUserInfo.DataKeys[e.RowIndex].Value.ToString();
if (info.DeleteUserInfoById(id) > 0)
{
this.lblMessage.Text = "删除成功!";
LoadInfo();
}
else
{
this.lblMessage.Text = "删除失败!";
LoadInfo();
}
}
/// <summary>
/// 编辑事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gdvUserInfo_RowEditing(object sender, GridViewEditEventArgs e)
{
gdvUserInfo.EditIndex = e.NewEditIndex;
LoadInfo();
}
/// <summary>
/// 取消编辑事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gdvUserInfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gdvUserInfo.EditIndex = -1;
LoadInfo();
}
/// <summary>
/// 更新事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gdvUserInfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string userId = gdvUserInfo.DataKeys[e.RowIndex].Value.ToString();
TextBox txtUserId = (TextBox)gdvUserInfo.Rows[e.RowIndex].Cells[0].FindControl("txtUserId");
TextBox txtUserName = (TextBox)gdvUserInfo.Rows[e.RowIndex].Cells[0].FindControl("txtUserName");
TextBox txtPassWord = (TextBox)gdvUserInfo.Rows[e.RowIndex].Cells[0].FindControl("txtPassWord");
if (txtUserName != null && txtPassWord != null)
{
try
{
UserInfo user = info.GetUserInfoById(userId);
user.UserName = txtUserName.Text.ToString();
user.Password = txtPassWord.Text.ToString();
if (info.ModifyUserInfo(user) > 0)
{
this.lblMessage.Text = "修改成功!";
gdvUserInfo.EditIndex = -1;
LoadInfo();
}
else
{
this.lblMessage.Text = "修改失败!";
gdvUserInfo.EditIndex = -1;
LoadInfo();
}
}
catch (Exception)
{
this.lblMessage.Text = "修改失败!";
gdvUserInfo.EditIndex = -1;
LoadInfo();
}
}
}
/// <summary>
///
/// 光棒事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gdvUserInfo_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 0; i < gdvUserInfo.Rows.Count; i++)
{
if (gdvUserInfo.Rows[i].RowType == DataControlRowType.DataRow)
{
gdvUserInfo.Rows[i].Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#9cf'");
gdvUserInfo.Rows[i].Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}
}
展开全部
可能会有两个问题:1、你在你的数据源绑定上是否有指定update()方法;
2、如果你想获取那个字段,你必须把那个你要的字段列设为模板列;
3、BindDepartmentGrid();这个方法你是干嘛用的?--而且GridView上面那个更新的很大部分都是系统弄好了的!
2、如果你想获取那个字段,你必须把那个你要的字段列设为模板列;
3、BindDepartmentGrid();这个方法你是干嘛用的?--而且GridView上面那个更新的很大部分都是系统弄好了的!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
string left = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
string leftper = left.Substring(0, left.IndexOf("%"));
decimal l = Convert.ToDecimal(leftper);
你没学清楚 是哪列 他不认识
string leftper = left.Substring(0, left.IndexOf("%"));
decimal l = Convert.ToDecimal(leftper);
你没学清楚 是哪列 他不认识
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
TextBox txtName = (TextBox)DepartmentGridView.Rows[e.RowIndex].FindControl("txtD_Name");
string d_name = txtName.Text;
先单步执行 能不能找到 txtD_Name 控件
string d_name = txtName.Text;
先单步执行 能不能找到 txtD_Name 控件
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询