前台部分代码:
<asp:GridView ID="gvDepartment" runat="server" AutoGenerateColumns="False" OnRowCommand="gvDepartment_RowCommand"
OnRowDataBound="gvDepartment_RowDataBound" OnRowEditing="gvDepartment_RowEditing">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<table>
<tr>
<td>
<%#DataBinder.Eval(Container.DataItem,"departmentID") %>
</td>
<td>
<asp:TextBox ID="txtdepatName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"departmentName") %>'></asp:TextBox>
</td>
<td>
<asp:LinkButton ID="LinkButton3" runat="server" CommandName="updata"
CommandArgument='<%#DataBinder.Eval(Container.DataItem,"departmentID") %>'>更新</asp:LinkButton>
<asp:LinkButton ID="LinkButton4" runat="server" CommandName="cancel">取消</asp:LinkButton>
<asp:LinkButton ID="LinkButton5" runat="server" CommandName="del">删除</asp:LinkButton>
</td>
</tr>
</table>
</EditItemTemplate>
<HeaderTemplate>
<table>
<tr>
<td>
部门编号
</td>
<td>
部门名称
</td>
<td>
操作
</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td>
<%#DataBinder.Eval(Container.DataItem,"departmentID") %>
</td>
<td>
<%#DataBinder.Eval(Container.DataItem,"departmentName") %>
</td>
<td>
<asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"departmentID") %>'
CommandName="edit">编辑</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"departmentID") %>'
CommandName="del">删除</asp:LinkButton>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
CS代码:
protected void gvDepartment_RowCommand(object sender, GridViewCommandEventArgs e)
{
switch (e.CommandName)
{
case "updata":
int rowindex = Convert.ToInt32(e.CommandArgument);
int MyKey = Convert.ToInt32(gvDepartment.DataKeys[rowindex].Value);
string name = ((TextBox)gvDepartment.Rows[MyKey].FindControl("txtdepatName")).Text;
strComm = "update department set departmentName='" + name + "' where departmentID='" + e.CommandArgument.ToString()+"'";
SqlCommand comm3 = new SqlCommand(strComm, conn);
try
{
conn.Open();
comm3.ExecuteNonQuery();
}
catch (Exception ex)
{ throw ex;
}
finally
{
conn.Close();
}
DepartmentBand();
break;
}我把红色字体部分改成:string name = ((TextBox)gvDepartment.Rows[0].FindControl("txtdepatName")).Text 可以实现第一行的更新 但其余的就不行
我知道可以用RowUpdating事件可以更新 但是现在想在Rowcommand事件中实现这功能 希望大家帮帮忙
<asp:GridView ID="gvDepartment" runat="server" AutoGenerateColumns="False" OnRowCommand="gvDepartment_RowCommand"
OnRowDataBound="gvDepartment_RowDataBound" OnRowEditing="gvDepartment_RowEditing">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<table>
<tr>
<td>
<%#DataBinder.Eval(Container.DataItem,"departmentID") %>
</td>
<td>
<asp:TextBox ID="txtdepatName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"departmentName") %>'></asp:TextBox>
</td>
<td>
<asp:LinkButton ID="LinkButton3" runat="server" CommandName="updata"
CommandArgument='<%#DataBinder.Eval(Container.DataItem,"departmentID") %>'>更新</asp:LinkButton>
<asp:LinkButton ID="LinkButton4" runat="server" CommandName="cancel">取消</asp:LinkButton>
<asp:LinkButton ID="LinkButton5" runat="server" CommandName="del">删除</asp:LinkButton>
</td>
</tr>
</table>
</EditItemTemplate>
<HeaderTemplate>
<table>
<tr>
<td>
部门编号
</td>
<td>
部门名称
</td>
<td>
操作
</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td>
<%#DataBinder.Eval(Container.DataItem,"departmentID") %>
</td>
<td>
<%#DataBinder.Eval(Container.DataItem,"departmentName") %>
</td>
<td>
<asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"departmentID") %>'
CommandName="edit">编辑</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"departmentID") %>'
CommandName="del">删除</asp:LinkButton>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
CS代码:
protected void gvDepartment_RowCommand(object sender, GridViewCommandEventArgs e)
{
switch (e.CommandName)
{
case "updata":
int rowindex = Convert.ToInt32(e.CommandArgument);
int MyKey = Convert.ToInt32(gvDepartment.DataKeys[rowindex].Value);
string name = ((TextBox)gvDepartment.Rows[MyKey].FindControl("txtdepatName")).Text;
strComm = "update department set departmentName='" + name + "' where departmentID='" + e.CommandArgument.ToString()+"'";
SqlCommand comm3 = new SqlCommand(strComm, conn);
try
{
conn.Open();
comm3.ExecuteNonQuery();
}
catch (Exception ex)
{ throw ex;
}
finally
{
conn.Close();
}
DepartmentBand();
break;
}我把红色字体部分改成:string name = ((TextBox)gvDepartment.Rows[0].FindControl("txtdepatName")).Text 可以实现第一行的更新 但其余的就不行
我知道可以用RowUpdating事件可以更新 但是现在想在Rowcommand事件中实现这功能 希望大家帮帮忙
其實是一樣的道理,你可以單步Debug一下.仔細找找原因在哪.
string name = ((TextBox)gvDepartment.Rows[rowindex].FindControl("txtdepatName")).Text;
我将索引定值可以实现当前行的更新,但是我想要它自己得到索引就不行了 按照3L的是不行的 昨天就试过了 调试出错
异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。
参数名: index源错误:
行 74:
行 75: int rowindex = Convert.ToInt32(e.CommandArgument);
行 76: int MyKey = Convert.ToInt32(gvDepartment.DataKeys[rowindex].Value);
行 77: string name1 = ((TextBox)gvDepartment.Rows[rowindex].FindControl("txtepartName")).Text;
int rowindex = Convert.ToInt32(e.CommandArgument); 获取主键
string name1 = ((TextBox)gvDepartment.Rows["应该是行索引而你现在用的是主键换 e.Item.ItemIndex试试"].FindControl("txtepartName")).Text;
我将CommandArgument=' <%#DataBinder.Eval(Container.DataItem,"departmentID") % 换成了CommandArgument='<%# ((GridViewRow)Container).RowIndex %>'也不不可以 .. 简直没法 不是索引超出范围。必须为非负值并小于集合大小、就是未将对象引用到失利话
我将string name = ((TextBox)gvDepartment.Rows[MyKey].FindControl("txtdepatName")).Text;
改成string name = ((TextBox)gvDepartment.Rows[1].FindControl("txtdepatName")).Text;
点击更新 提示: GridView“gvDepartment”激发了未处理的事件“RowUpdating”。
但是刷新页面竟然更改了,数据库也更新了
但是这个只能改固定的行,还是不知道这么把索引去出来
我在前台添加一个Lable控件
将case "updata":中代码改成
int rowindex = Convert.ToInt32(e.CommandArgument);
Label3.Text =Convert.ToString(rowindex);前台的Lable显示是正确的值
string[] CommandArgumentValue = e.CommandArgument.Split( '|');
取到2个值了,再把值做SQL参数就没问题了
int MyKey = Convert.ToInt32(gvDepartment.DataKeys[rowindex].Value);
string name = ((TextBox)gvDepartment.Rows[MyKey].FindControl("txtdepatName")).Text; 这样写int rowindex = Convert.ToInt32(e.CommandArgument);
int MyKey = Convert.ToInt32(gvDepartment.DataKeys[rowindex].Value);
LinkButton lbtn = e.CommandSource as LinkButton ;
string name;
if( lbtn !=null)
{
TextBox txt = lbtn.NamingContainer.FindControl("txtdepatName") as TextBox;
if(txt!=null)
{
name = txt.Text;
}
}
else
{
return;
}
异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。
我还有个问题 17L的
我将CommandArgument=' <%#DataBinder.Eval(Container.DataItem,"departmentID") % 换成了CommandArgument=' <%# ((GridViewRow)Container).RowIndex %> 这样可以直接取行号 但是在执行SQL语句时where后面departmentID就等于的是行号 不能正确地更新 有没有什么方法在我改变CommandArgument的情况下正确的更新喃 谢谢
switch (e.CommandName)
{
case "updata":
LinkButton lbtn = e.CommandSource as LinkButton ;
string name;
if( lbtn !=null)
{
TextBox txt = lbtn.NamingContainer.FindControl("txtdepatName") as TextBox;
if(txt!=null)
{
name = txt.Text;
}
}
else
{
return;
}
strComm = "update department set departmentName='" + name + "' where departmentID='" + e.CommandArgument +"'";
绑定行号这样写
CommandArgument='<%# Container.DataItemIndex%>'如果你给CommandArgument属性绑定的是当前的行号,哪你需要
gvDepartment.Rows[rowIndex] 定位到GridView的指定行 GridViewRow
然后去查找 指定控件 类似查找txtdepatName有或者 定位到gvDepartment(GridView)所绑定的数据源 去查找对应的departmentID不管以上那种,你都要多次操作,所以相对来说,直接绑定departmentID 是好的选择