我的gridview的GridView1_RowCommand没有触发

xiaozi141 2009-10-17 11:23:08
刚刚解决玩gridview的翻页问题,发现点击GridView中的linkbutton也没效果了,追踪了一下GridView1_RowCommand事件根本没有触发。。。
public partial class dzEmployee : System.Web.UI.Page
{
dzDB.Sql mySql = new dzDB.Sql();
public DataSet myDataSet = new DataSet();
public static string sqlstr = "";
public static string table = "";

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
table = "Sys_Employee";
sqlstr = "select eId,eName,eSex,eSection,ePosition,eProId,eTele from Sys_Employee";
GridView_DataBind(sqlstr, table);
DroplistdownBind(DropDownList1, "Flag", "0", "Sys_Dictionary", "Id", "DictionaryName");
}
}

private void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl = e.Row.FindControl("Label3") as Label;
Label lbl2 = e.Row.FindControl("Label4") as Label;
Label lbl3 = e.Row.FindControl("Label5") as Label;
SqlDataReader myReader = mySql.myReader("select DictionaryName from Sys_Dictionary where Id='" + lbl.Text + "'");
if (myReader.Read())
{
lbl.Text = myReader.GetString(0);
}
myReader = mySql.myReader("select PositionName from Sys_DicPosition where Id='" + lbl2.Text + "'");
if (myReader.Read())
{
lbl2.Text = myReader.GetString(0);
}
myReader = mySql.myReader("select ItemPlace from Sys_Item where ItemID='" + lbl3.Text + "'");
if (myReader.Read())
{
lbl3.Text = myReader.GetString(0);
}
myReader.Dispose();
}
}

private void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "btn")
{
string eId = e.CommandArgument.ToString();
Response.Redirect("EmployEdit.aspx?eId=" + eId);
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView_DataBind(sqlstr, table);
}

private void GridView_DataBind(string sqlstr,string table)
{
myDataSet = mySql.createDataSet(sqlstr, table);
GridView1.DataSource = myDataSet.Tables[table].DefaultView;
GridView1.RowDataBound += new GridViewRowEventHandler(GridView1_RowDataBound);
// GridView1.DataKeys = "eId";
GridView1.AllowPaging = true;
GridView1.PageSize = 10;
GridView1.DataBind();
GridView1.RowCommand += new GridViewCommandEventHandler(GridView1_RowCommand);
}
protected void Button1_Click(object sender, EventArgs e)
{
string eName = TextBox1.Text;
try
{
sqlstr = "select eId,eName,eSex,eSection,ePosition,eProId,eTele from Sys_Employee where eName like '%" + eName + "%'";
table = "Sys_Employee";
GridView_DataBind(sqlstr, table);
}
catch
{

}
}
请问是为什么,以前都能触发的。。。
...全文
242 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
十八道胡同 2009-10-17
  • 打赏
  • 举报
回复
你的前台没有添加GridView1_RowDataBound事件,要添加下
happy664618843 2009-10-17
  • 打赏
  • 举报
回复
把GridView中所有事件 删除 重新点击Row_Command事件
xiaozi141 2009-10-17
  • 打赏
  • 举报
回复
前台添加OnRowCommand="GridView1_RowCommand"运行出错

错误 25 “dzEmployee.GridView1_RowCommand(object, System.Web.UI.WebControls.GridViewCommandEventArgs)”不可访问,因为它受保护级别限制 d:\Employee\dzEmployee.aspx 1 1 d:\Employee\
ivws_19 2009-10-17
  • 打赏
  • 举报
回复
在前台重新添加一下rowcommand事件
xiaozi141 2009-10-17
  • 打赏
  • 举报
回复
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" AutoGenerateColumns="False" OnPageIndexChanging="GridView1_PageIndexChanging">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" CssClass="gridview"/>
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:TemplateField HeaderText="员工编号">
<ItemTemplate>
<asp:LinkButton ID="LinkButton3" CommandArgument=<%# DataBinder.Eval(Container.DataItem,"eId") %> CommandName="btn" runat="server"
><%# DataBinder.Eval(Container.DataItem,"eId") %></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="员工姓名">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text=<%# DataBinder.Eval(Container.DataItem,"eName") %>></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="性别">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text=<%# DataBinder.Eval(Container.DataItem,"eSex") %>></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="所属部门">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text=<%# DataBinder.Eval(Container.DataItem,"eSection") %>></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="所在职位">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text=<%# DataBinder.Eval(Container.DataItem,"ePosition") %>></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="所在项目">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text=<%# DataBinder.Eval(Container.DataItem,"eProId") %>></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="联系电话" >
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text=<%# DataBinder.Eval(Container.DataItem,"eTele") %>></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
chenjianyong94 2009-10-17
  • 打赏
  • 举报
回复
前台设计页面贴出来。。。好分析问题之所在。。。。
ivws_19 2009-10-17
  • 打赏
  • 举报
回复
databind事件里这句:GridView1.RowCommand += new GridViewCommandEventHandler(GridView1_RowCommand);
干什么用的,必要吗?前台没定义事件?
chenjianyong94 2009-10-17
  • 打赏
  • 举报
回复
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" AutoGenerateColumns="False" OnPageIndexChanging="GridView1_PageIndexChanging">

他没有 Row_Command 事件 : 添加上

<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" AutoGenerateColumns="False" OnPageIndexChanging="GridView1_PageIndexChanging"

OnRowCommand="GridView1_RowCommand" >

后台页面添加上:

把private 改成 protected

void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)


private void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "btn")
{
string eId = e.CommandArgument.ToString();
Response.Redirect("EmployEdit.aspx?eId=" + eId);
}
}


{

}

62,046

社区成员

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

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

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

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