CommandArgument用法

CommandArgument用法

 
1.绑定数据库中一个主键
前台代码:
<ItemTemplate>
                        <asp:ImageButton ID="ibtnUpdate" runat="server" CommandArgument='<%# Eval("studentNum")%>'
                            CommandName="edit" />
                    </ItemTemplate>
就可以通过GridView的RowCommand事件获得此行的主键,进而获得此行数据。
protected void gvSelectTask_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string temp = e.CommandArgument.ToString().Trim();
            //string[] num = temp.Split(',');
            string tag = e.CommandName.ToString().Trim();
            //if (tag == "edit")
            //{
            //    CreativeStudio.Common.alert.GoHref("selectTaskEdit.aspx?studentN="+ num[0] +"&teacherN="+num[1]+"");
           // }
           // else if (tag == "delete") //删除选课记录的时候没有必要删除学生记录
           // {
            //    sTaskBLL.Delete(num[0]);
               // Bind();
           // }
        }


2.如果涉及到主键是两个字段或者要同时绑定两张表的主键那么用法为
前台代码:
<ItemTemplate>
                        <asp:ImageButton ID="ibtnUpdate" runat="server" CommandArgument='<%# Eval("studentNum")+","+Eval("teacherNum")%>'
                            CommandName="edit" />
                    </ItemTemplate>
同样可以获得表中一行数据,或者同时获得两张表的各一行数据。
protected void gvSelectTask_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string temp = e.CommandArgument.ToString().Trim();
            string[] num = temp.Split(',');
            string tag = e.CommandName.ToString().Trim();
            //if (tag == "edit")
            //{
            //    CreativeStudio.Common.alert.GoHref("selectTaskEdit.aspx?studentN="+ num[0] +"&teacherN="+num[1]+"");
           // }
           // else if (tag == "delete") //删除选课记录的时候没有必要删除学生记录
           // {
            //    sTaskBLL.Delete(num[0]);
               // Bind();
           // }
        }
http://www.cnblogs.com/forever4444/archive/2009/07/19/1526638.html
原文网址,写的不错
posted @ 2016-04-22 11:20  梅花雨  阅读(2586)  评论(0编辑  收藏  举报