GridView模板列,如何在后台代码中添加客户端事件,执行sql函数,格式化数据

1、在GridView中经常会有这样的操作,比如要删除一条记录,点击一个个ImageButton 或者 LinkButton 首先用脚本确认是否删除,然后确定之后,再执行这个ImageButton或者LinkButton的OnClick事件,执行删除,具体的操作如下:

<asp:GridView ID="gvClassInfoList" runat="server" Width="100%"  AutoGenerateColumns="False" DataKeyNames="ClassID"   > 
       <RowStyle CssClass="dataTable1Row" />
       <Columns>
       <asp:TemplateField HeaderText="班级ID" Visible="False">//这一列可以同样是BoundField列,主键列
             <ItemTemplate>
           <asp:Label ID="lblClassId" runat="server" Text='<%# Eval("ClassID") %>' Visible="false" ></asp:Label>
             </ItemTemplate>                        
       </asp:TemplateField>
       <asp:BoundField DataField="CLASSNO" HeaderText="班级代码" SortExpression="IS_REPLY">
          <ItemStyle Wrap="True" ForeColor="Red" HorizontalAlign="Center" Width="5%" />
          <HeaderStyle Wrap="False" />
       </asp:BoundField>

   <asp:BoundField DataField="PROGRAMNAME" HeaderText="培训项目"  SortExpression="PROGRAMNAME" />
       <asp:BoundField DataField="CLASSTYPE" HeaderText="类别 " SortExpression="CLASSTYPE"></asp:BoundField>
       <asp:BoundField DataField="TRAINEENO" HeaderText="总人数" SortExpression="TRAINEENO"></asp:BoundField>
       <asp:BoundField DataField="STARTDATE" HeaderText="理论开始时间" SortExpression="STARTDATE" HtmlEncode=false                                  DataFormatString="{0:yyyy-MM-dd}"></asp:BoundField>
       <asp:BoundField DataField="ENDDATE" HeaderText="理论结束时间" SortExpression="ENDDATE" HtmlEncode=false                       DataFormatString="{0:yyyy-MM-dd}"></asp:BoundField>//格式化数据
      <asp:BoundField DataField="DAYS" HeaderText="时长" SortExpression="DAYS"></asp:BoundField>
      <asp:BoundField DataField="FROMNO" HeaderText="延续自" SortExpression="FROMNO"></asp:BoundField>                    
      <asp:BoundField DataField="ATTENDNO" HeaderText="延续至" SortExpression="ATTENDNO"></asp:BoundField>                    
      <asp:BoundField DataField="WITHNO" HeaderText="合班" SortExpression="WITHNO"></asp:BoundField>                    
      <asp:BoundField DataField="ClassRoom" HeaderText="教室号" SortExpression="ClassRoom"></asp:BoundField>                    
      <asp:BoundField DataField="Receptionist" HeaderText="接待员"  SortExpression="Receptionist"></asp:BoundField>                    
      <asp:BoundField DataField="Remark" HeaderText="备注" SortExpression="Remark"></asp:BoundField>
      <asp:TemplateField HeaderText="团组资料">
            <ItemTemplate>
                <div>                                
                   <asp:Label ID="lblOrgName" runat="server" Text='<%#Eval("OrgNameAndTraineeNo") %>'></asp:Label>//由sql函数生成的字段

                </div>
            </ItemTemplate>
      </asp:TemplateField>
      <asp:TemplateField HeaderText="管理">
             <ItemTemplate>
                  <div>
                     <nobr>                                   
                          <asp:LinkButton ID="lkBtnModify" runat="server" >修改</asp:LinkButton>
                          <asp:LinkButton ID="lkBtnReset" runat="server" OnClientClick='javascritp:return confirm("确定要修改此班级状态吗?");'                            onclick="lkBtnReset_Click" >取消</asp:LinkButton>//执行客户端事件与onclick事件
                          <asp:LinkButton ID="lkBtnLookOrg" runat="server" >查看团组</asp:LinkButton>
                          <asp:LinkButton ID="lkBtnAddOrg" runat="server" >添加团组</asp:LinkButton>                                   
                     </nobr>
                  </div>
            </ItemTemplate>
        </asp:TemplateField>
      </Columns>
      <HeaderStyle BorderStyle="None" CssClass="dataTable1HeaderCellWithSorting" BorderWidth="5px" />
       <AlternatingRowStyle BorderStyle="None" CssClass="dataTable1Row" />
</asp:GridView>

 

//2、以下为模板列中lkBtnReset的onclick事件

/// <summary>
        /// 将班级信息的ColName1即显示状态修改为0,不显示
        /// </summary>       
        protected void lkBtnReset_Click(object sender, EventArgs e)
        {
            GridViewRow gvr = (sender as LinkButton).NamingContainer as GridViewRow;
            string key = gvClassInfoList.DataKeys[gvr.RowIndex].Value.ToString();//获取gridview的主键
            IClass bll = BLLAccess.CreateClass();
            ClassBasic obj = bll.GetModelByClassID(key);
            obj.ColName1 = "0";
            string classId = bll.UpdateClass(obj);
            if (!string.IsNullOrEmpty(classId))
            {
                AlertMessage("修改成功!");
            }
            else
            {
                AlertMessage("修改失败!");
            }
        }

 

 

//3、在后台代码中添加客户端事件

/// <summary>
/// 对GridView中每一行的linkButton添加客户端的onclick事件,打开一个查看团组的模态窗口
/// </summary>
/// <param name="ds">绑定到GridView中的数据源</param>
private void AddEvent(DataSet ds)
{
    try
    {
        if (ds != null && ds.Tables[0].Rows.Count > 0)
        {
            foreach (GridViewRow gr in gvClassInfoList.Rows)
            {
                LinkButton lkBtn = (LinkButton)gr.Cells[15].FindControl("lkBtnLookOrg");
                LinkButton lkBtnAddOrg = (LinkButton)gr.Cells[15].FindControl("lkBtnAddOrg");
                LinkButton lkBtnModify = (LinkButton)gr.Cells[15].FindControl("lkBtnModify");

                //string str = ((Label)gr.Cells[0].FindControl("lblText")).Text;                     
                string classid = ((Label)gr.Cells[0].FindControl("lblClassId")).Text;

      //下面为添加客户端的脚本事件,打开对话框。函数在javascript中
                lkBtn.Attributes.Add("onclick", "return UniversalOpenWindow(760,550,'ScanOrgInfo.aspx?classid=" + classid + "');");
                lkBtnAddOrg.Attributes.Add("onclick", "return UniversalOpenWindow(760,550,'NewClassOrganization.aspx?classid=" + classid + "');");
                lkBtnModify.Attributes.Add("onclick", "return UniversalOpenWindow(760,550,'NewClass.aspx?classid=" + classid + "');");
            }
        }
    }
    catch (Exception e)
    {
        throw e;
    }
}

 

 //分页的sql与执行sql函数,GetOrgNameAndTraineeNos函数在SQL SERVER系列中

/// <summary>
/// 获取分页的sql语句
/// </summary>
/// <param name="startIndex">起始记录下标,默认从0开始</param>
/// <param name="endindex">本页记录的最后一个下标</param>
/// <param name="sqlWhere">查询条件</param>
/// <returns>返回条件查询字符串</returns>
private string GetPagingString(int startIndex, int endIndex, string sqlWhere)
{
    string sqlWhere1 = " WHERE 1=1 And c.ColName1='1' ";//ColName=1即为可显示的数据
    string strSql = "begin "
            + " WITH list AS ( "
            + " SELECT ROW_NUMBER() OVER (ORDER BY c.ClassNo DESC)AS Row, c.ClassID,c.CLASSNO,c.ProgramName,c.ClassType,"
            + " c.TraineeNo,c.StartDate,c.EndDate,c.Days,c.FromNo,c.AttendNo,c.WithNo,c.ClassRoom,c.Receptionist,c.Remark,"
            + " dbo.GetOrgNameAndTraineeNos(c.ClassID) as OrgNameAndTraineeNo "//执行sql函数,拼接该班级ID下的所以团组及人数
            + " from ClassBasic c ";

    if (!string.IsNullOrEmpty(sqlWhere))
    {
        sqlWhere1 += sqlWhere;
        strSql += sqlWhere1 + " )"
            + "  SELECT * FROM list WHERE Row between " + startIndex + " and " + endIndex + ""
            + " end";
    }
    else
    {
        strSql += " )"
            + "  SELECT * FROM list WHERE Row between " + startIndex + " and " + endIndex + ""
            + " end";
    }
    return strSql;
}

 

 

 

你可能感兴趣的:(GridView)