GridView中对数据编辑打开一个新页面

1、

GridView对数据的绑定有非常强悍的功能 ,也非常好用,我们经常用它来绑定数据,我们对数据也需要维护,通常是在GridView中放一个模板列,模板列中放一个按钮,点击此按钮,在GridView的RowCommand事件中通过Response.Redrict()进行跳转到另一个页面进行编辑。例如:

 <asp:TemplateField HeaderText="管理">
     <ItemTemplate>

      <asp:ImageButton ID="imgBtnManage" runat="server" CommandName="manage"   ImageUrl="~/Images/manage_25X25.gif"                       CommandArgument='<%# Eval("ProductID") %>' />

  </ItemTemplate>
</asp:TemplateField>

 

然后在Row_Command()事件中操作

 protected void gvProduct_RowCommand(object sender, GridViewCommandEventArgs e)
{
           string strProductId = e.CommandArgument.ToString();

     int index = ((GridViewRow)((ImageButton)(e.CommandSource)).Parent.Parent).RowIndex;//获取当前点击前的行号

    if (e.CommandName == "manage")

    {

      Response.Redirect("xx.aspx?id="+strProductId);

    }

}

 

2、

我们还可以用下面一种方法操作,即在GridView中的模板列中放一个Html控件,如下:放的是一个Image,用它的onclick事件,用js实现,js函数可以用传参的形式传进去,

**注意,此处放置的是Image,是Html控件,如果你要通过数据绑定的方式即<%# Eval("**")%>这种方式对js函数传参数,只能用html控件的事件 ,不能用服务器控件(包括ImageButton的 OnClientClick事件皆不可)

<asp:TemplateField HeaderText="管理">
  <ItemTemplate>
    <img id="imgManage" src="Images/manage_25X25.gif" onclick="NavWindow('<%#Eval("CategoryID") %>','<%#Eval("ProductId") %>')" />
  </ItemTemplate>
</asp:TemplateField>

 如果要用服务器控件 ,且要绑定数据源的字段,则onclick事件,必须在后台通过控件的绑定数据源之后加上去,如:用Attributes.Add

((Button)e.Row.FindControl("btnEdit")).Attributes.Add("onclick", "return UniversalOpenWindowAndBreak(720,640,'NewStudentIssue.aspx?issueId=" + issueId + "');");

3、

也可以用下面的方式传脚本参数,这样可以统一打开对话框或者 窗口的函数

<asp:TemplateField HeaderText="管理">
  <ItemTemplate>
     <img onclick=="UniversalOpenWindow(650,450,'ScanOrgInfo.aspx?classid=<%#Eval("ClassID") %>')" />

  </ItemTemplate>
</asp:TemplateField>

 

还可以这样添加事件,在页面的cs代码中添加客户端的事件

/// <summary>
        /// 对GridView中每一行的linkButton添加客户端的onclick事件,打开一个查看团组的模态窗口
        /// </summary>
        /// <param name="ds">绑定到GridView中的数据源</param>
        private void AddEvent(DataSet ds)
        {
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                foreach (GridViewRow gr in gvClassInfoList.Rows)
                {
                    LinkButton lkBtn = (LinkButton)gr.Cells[15].FindControl("lkBtnLookOrg");//dgOtherList.Items[i].Cells[2].FindControl("lnkPN");
                    //string str = ((Label)gr.Cells[0].FindControl("lblText")).Text;
                    string classid = gr.Cells[0].Text.ToString();
                    lkBtn.Attributes.Add("onclick", "return UniversalOpenWindow(660,450,'NewClass.aspx?classid='"+classid+")");                  
                }
            }
        }

JS可以如下,随你自己写了:

<script type="text/javascript">

function NavWindow(currId,productId)
        {
             var strUrl = "AddProduct.aspx?currId="+currId +"&productId="+productId;//document.getElementById('btnAddProduct').getAttribute("cateid");
            window.open(strUrl);
            event.returnValue=false;//此事是防止客户端事件回发,进行提交PostBack
            return false;
        }

//打开模态与非模态对话框

function UniversalOpenWindow(w,h,fileName)

{
   window.showModalDialog(fileName, argsObjectModalWindow, "dialogWidth:" + w + "px; dialogHeight:" + h + "px; center:Yes; help:No;

     resizable:No; scroll:no; status:No")
}
function UniversalOpenWindowFree(w,h,fileName)

{
   window.showModelessDialog(fileName, argsObjectModalWindow, "dialogWidth:" + w + "px; dialogHeight:" + h + "px; center:Yes; help:No;

    resizable:Yes; scroll:No; status:No")
}

</script>

posted @ 2009-12-30 23:58  Vihone  阅读(1631)  评论(0编辑  收藏  举报