protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Agree")
{
int iIndex = Convert.ToInt16(e.CommandArgument);
//try
//{
string studentid = GridView1.DataKeys[iIndex].Value.ToString();
using (SqlConnection conn = new SqlConnection(sConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = "update StudentDeferPaymentApplicationTBSet set AllowedDeferedPayment=@allow where StudentID='@sid'";
cmd.Parameters.AddWithValue("@allow", 1);
cmd.Parameters.AddWithValue("@sid", studentid);
cmd.ExecuteNonQuery();
SetBind();
}
}
//}
//catch { }
}
}
<asp:TemplateField HeaderText="审批">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="false"
CommandName="Agree" Text="审批" OnClientClick="return confirm('确定同意审批吗?')" CommandArgument='<%# Container.DataItemIndex %>'/>
</ItemTemplate>
</asp:TemplateField>大家帮看一下哈, 上面这些代码有没有错误呢?
CommandArgument='<%# Eval("StudentID")%>'/>
而我的问题也不在绑定上, 根本是: GridView1_RowCommand里的代码好像从没被执行过一样。
所以,更新操作一直没执行。
我真不知道问题出在哪个环节。
GridViewRow row = this.GridView1.Rows[index];
string studentid = row.Cells[0].Text;
改成
int index = this.GridView1.DataKeys[e.Row.DataItemIndex][0];
但就我的理解, 觉得应该if语句是正确的。
有没有哪里的细节我没不懂的, 哪位高人指出来帮帮忙哈……T
你用的是模板,当然要先找到那个button了
{
if (e.CommandName == "Agree")
{
int iIndex = Convert.ToInt16(e.CommandArgument);
//int index = Convert.ToInt32(e.CommandArgument);
//GridViewRow row = this.GridView1.Rows[index]; //string studentid = row.Cells[0].Text;
//try
//{
string studentid = GridView1.DataKeys[iIndex].Value.ToString();
using (SqlConnection conn = new SqlConnection(sConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = "update StudentDeferPaymentApplicationTBSet set AllowedDeferedPayment=@allow where StudentID=@sid";
cmd.Parameters.AddWithValue("@allow", 1);
cmd.Parameters.AddWithValue("@sid", studentid);
cmd.ExecuteNonQuery();
SetBind();
}
}
//}
//catch { }
}
}
public void SetBind()
{
DataSet ds = new DataSet();
//try
//{
using (SqlConnection conn = new SqlConnection(sConnectionString))
{
//conn.Open();
SqlDataAdapter sda = new SqlDataAdapter("SELECT StudentID, ApplyDate, ApplyCause FROM StudentDeferPaymentApplicationTBSet where AllowedDeferedPayment=0", conn);
sda.Fill(ds);
}
GridView1.DataSource = ds;
GridView1.DataBind();
//}
//catch { }
}
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="StudentID" HeaderText="学生学号" />
<asp:BoundField DataField="ApplyDate" HeaderText="申请日期" />
<asp:BoundField DataField="ApplyCause" HeaderText="申请原因" />
<asp:ButtonField ButtonType="Button" CommandName="Agree" Text="审批" />
</Columns>
</asp:GridView>哪位好心人,再帮帮看看哈…… 我实在是不知道到底错在哪儿啊……?
<Columns>
<asp:BoundField DataField="id"/>
<asp:BoundField DataField="UserName"/>
<asp:BoundField DataField="RealName"/>
<asp:BoundField DataField="Mobile"/>
<asp:BoundField DataField="Email"/>
<asp:BoundField DataField="office_name" />
<asp:ButtonField CommandName="sel" Text="查看" />
<asp:ButtonField CommandName="edit" Text="修改" />
<asp:ButtonField CommandName="qxcom" Text="分配权限" />
<asp:ButtonField CommandName="del" Text="删除" />
</Columns>
</asp:GridView>protected void topicManageGV_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow row = null;
int index = -1;
switch (e.CommandName)
{
case "sel":
index = Convert.ToInt32(e.CommandArgument);
row = this.topicManageGV.Rows[index];
string selId = row.Cells[0].Text;
Response.Redirect("ManagerView.aspx?id=" + selId);
break;
case "edit":
index = Convert.ToInt32(e.CommandArgument);
row = this.topicManageGV.Rows[index];
string editId = row.Cells[0].Text;
Response.Redirect("ManagerEdit.aspx?id=" + editId);
break;
case "del":
index = Convert.ToInt32(e.CommandArgument);
row = this.topicManageGV.Rows[index];
string delId = row.Cells[0].Text;
Graduate.BLL.Manager bll = new Graduate.BLL.Manager();
bll.Delete(int.Parse(delId));
break;
case "qxcom":
index = Convert.ToInt32(e.CommandArgument);
row = this.topicManageGV.Rows[index];
string qxid = row.Cells[0].Text;
Response.Redirect("../system/DealPermission.aspx?id=" + qxid);
break;
}
BindGV();
}
真是郁闷。 刚开始以为有加载 RowCommand事件,实际上是没加载正确,加载的是一个空的RowCommand2事件。真正的RowCommad事件却没加载。