首页 新闻 赞助 找找看

GridView自动加模板之后,RowCommand方法无法执行

0
悬赏园豆:50 [待解决问题]

对一个gridview在后台添加模板列之后,RowCommand方法无法执行到,不知道是什么原因,请各路大神指导。。。
尝试过后台添加生成触发RowCommand事件的模板列(在其中加入控件),也失败了。。。

前台代码:

<asp:GridView ID="GvDemo" runat="server" AutoGenerateColumns="False" Width="800px"
            Font-Size="12px" OnRowDataBound="GvDemo_RowDataBound" OnRowCommand="GvDemo_RowCommand">
            <Columns>
                <asp:BoundField DataField="objectname" HeaderText="评分对象" />
                <asp:TemplateField HeaderText="操作">
                    <ItemTemplate>
                        <asp:LinkButton ID="LbtnEdit" runat="server" Text="编辑" CommandName="MyEdit" CommandArgument='<%# Bind("moid") %>' />
                        <asp:LinkButton ID="LbtnDelete" runat="server" Text="删除" CommandName="MyDelete" CommandArgument='<%# Bind("moid") %>' OnClientClick="return confirm('确定要删除吗?')" />
                        <asp:LinkButton ID="LbtnUpdate" runat="server" Visible="false" Text="更新" CommandName="MyUpdate"
                            CommandArgument='<%# Bind("moid") %>' />
                        <asp:LinkButton ID="LbtnCancel" runat="server" Visible="false" Text="取消" CommandName="MyCancel"
                            CommandArgument='<%# Bind("moid") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <HeaderStyle BackColor="Azure" Font-Size="12px" HorizontalAlign="Center" />
            <RowStyle HorizontalAlign="Center" />
            <PagerStyle HorizontalAlign="Center" />
        </asp:GridView>
View Code

后台代码:

public partial class GridViewDemo : System.Web.UI.Page
    {
        #region 成员

        DemoBLL _bll = new DemoBLL();
        static string mid = "1";

        #endregion

        #region 事件

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GridBind();
                GvDemoBind();
            }
        }

        protected void GvDemo_RowDataBound(object sender, GridViewRowEventArgs e)//数据绑定后,加入控件和绑定对应数据
        {
            DataTable dtMarkObjectList = _bll.GetMarkObjectList(mid);
            int rowCount = dtMarkObjectList.Rows.Count;
            if (rowCount > 0 && e.Row.RowIndex > -1 && e.Row.RowIndex < rowCount)
            {
                string moid = dtMarkObjectList.Rows[e.Row.RowIndex]["id"].ToString();
                DataTable dtMarkRaterTypeList = _bll.GetMarkRaterTypeList(mid);
                int index = 1;
                foreach (DataRow dr in dtMarkRaterTypeList.Rows)
                {
                    string ids = _bll.GetStaffIDs(mid, moid, dr["id"].ToString());
                    string names = _bll.GetStaffNames(ids);

                    HiddenField HidIDs = new HiddenField();
                    Label LblNames = new Label();
                    TextBox TbNames = new TextBox();
                    HidIDs.ID = "HidIDs" + index;
                    HidIDs.Value = ids;
                    LblNames.ID = "LblNames" + index;
                    LblNames.Text = names;
                    TbNames.ID = "TbNames" + index;
                    TbNames.Text = names;
                    TbNames.Visible = false;
                    TbNames.Attributes.Add("onclick", "<script>alert('let it go!');</script>");

                    e.Row.Cells[index].Controls.Add(HidIDs);
                    e.Row.Cells[index].Controls.Add(LblNames);
                    e.Row.Cells[index].Controls.Add(TbNames);

                    index++;
                }

                //LinkButton LbtnEdit = new LinkButton();
                //LinkButton LbtnUpdate = new LinkButton();
                //LinkButton LbtnCancel = new LinkButton();
                //LinkButton LbtnDelete = new LinkButton();

                //LbtnEdit.ID = "LbtnEdit";
                //LbtnEdit.Text = "编辑";
                //LbtnEdit.CommandName = "MyEdit";
                //LbtnUpdate.ID = "LbtnUpdate";
                //LbtnUpdate.Text = "更新";
                //LbtnUpdate.Visible = false;
                //LbtnUpdate.CommandName = "MyUpdate";
                //LbtnUpdate.CommandArgument = moid;
                //LbtnCancel.ID = "LbtnCancel";
                //LbtnCancel.Text = "取消";
                //LbtnCancel.Visible = false;
                //LbtnCancel.CommandName = "MyCancel";
                //LbtnDelete.ID = "LbtnDelete";
                //LbtnDelete.Text = "删除";
                //LbtnDelete.CommandName = "MyDelete";
                //LbtnDelete.CommandArgument = moid;
                //LbtnDelete.OnClientClick = "return confirm('确定要删除吗?" + LbtnEdit.ClientID + "')";

                //e.Row.Cells[index].Controls.Add(LbtnEdit);
                //e.Row.Cells[index].Controls.Add(LbtnUpdate);
                //e.Row.Cells[index].Controls.Add(LbtnCancel);
                //e.Row.Cells[index].Controls.Add(LbtnDelete);
            }
        }

        protected void GvDemo_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //完全执行不到啊。。。丧心病狂
            switch (e.CommandName)
            {
                case "MyEdit":
                    {
                        int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
                        
                        break;
                    }
                case "MyUpdate":
                    break;
                case "MyCancel":
                    break;
                case "MyDelete":
                    break;
            }
        }

        #endregion

        #region 方法

        private void GridBind()//加模板列
        {
            DataTable dtMarkRaterTypeList = _bll.GetMarkRaterTypeList(mid);
            int index = 1;
            foreach (DataRow dr in dtMarkRaterTypeList.Rows)
            {
                TemplateField temp = new TemplateField();
                temp.HeaderText = dr["typename"].ToString() + "(" + dr["ratio"].ToString() + "%)";
                GvDemo.Columns.Insert(index, temp);
                //GvDemo.Columns.Add(temp);
                index++;
            }
            //TemplateField field = new TemplateField();
            //field.HeaderText = "操作";
            //GvDemo.Columns.Add(field);
        }

        private void GvDemoBind()//数据绑定
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("moid");
            dt.Columns.Add("objectname");
            DataTable dtMarkObjectList = _bll.GetMarkObjectList(mid);
            foreach (DataRow dr in dtMarkObjectList.Rows)
            {
                dt.Rows.Add(dr["id"].ToString(), dr["objectname"].ToString());
            }
            GvDemo.DataSource = dt;
            GvDemo.DataBind();
        }

        #endregion

    }
View Code
罗德里亚的主页 罗德里亚 | 初学一级 | 园豆:152
提问于:2014-02-21 12:16
< >
分享
所有回答(3)
0

加断点,页面一加载就开始一步步地跟,看看执不执行这一个方法,再看看是不是生成的Html按钮上根本就没有这个方法,

羽商宫 | 园豆:2490 (老鸟四级) | 2014-02-21 13:23
0
邀月 | 园豆:25475 (高人七级) | 2014-02-21 13:45

去掉后台添加模板的代码后,可以触发的

支持(0) 反对(0) 罗德里亚 | 园豆:152 (初学一级) | 2014-02-21 14:00
0

GridBind()必须在每次Page_Load事件中被调用,而不是仅!PostBack。因为动态创建的控件在PostBack后的Page_Load事件后要还原状态,发现这个控件未被创建,所以它的状态和事件都丢失了。

空明流光 | 园豆:106 (初学一级) | 2014-02-21 20:47

ASP.NET生命周期中Page的ViewState是在Page_Load之后被还原的。所以创建的所有动态控件,想要回发后恢复其状态,必须在Page_Load或Page_Load之前如PreInit中创建。

支持(0) 反对(0) 空明流光 | 园豆:106 (初学一级) | 2014-02-21 20:58
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册