datagrid当前页编辑

所需效果图:

datagrid当前页编辑

点击新增:

 datagrid当前页编辑

点击上传文件:

datagrid当前页编辑

点击编辑:

datagrid当前页编辑

点击删除:

datagrid当前页编辑 

前台:

                        <asp:DataGrid ID="DataGrid1" runat="server" Width="100%" PageSize="4" CssClass="datagrid1" AutoGenerateColumns="False"  AllowPaging="True" DataKeyField="Id" CellPadding="3" CellSpacing="1"  GridLines="None" OnItemCommand="DataGrid1_ItemCommand" 
                            OnPageIndexChanged="DataGrid1_PageIndexChanged" OnItemDataBound="DataGrid1_ItemDataBound"
>
                            <PagerStyle CssClass="dg_page" HorizontalAlign="Center" Mode="NumericPages"></PagerStyle>
                            <HeaderStyle CssClass="Header_co" HorizontalAlign="Center"></HeaderStyle>
                            <ItemStyle HorizontalAlign="Center" />
                            <Columns>
                                <asp:TemplateColumn HeaderText="序号">
                                     <ItemStyle Width="25%" Wrap="false"/>
                                    <ItemTemplate>
                                        <asp:Label ID="Label6" runat="server" Text="<%# Container.ItemIndex+1 %>"></asp:Label>   <%--自定义序号 , DataKeyField="Id" 可绑定id-->
                                    </ItemTemplate>                                    
                                </asp:TemplateColumn>
                                <asp:TemplateColumn HeaderText="四色图底图名称">
                                     <ItemStyle Width="25%" Wrap="false"/>
                                    <ItemTemplate>
                                        <asp:Label ID="Label1" runat="server" Text='<%#Eval("四色图底图名称") %>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="EditName" runat="server" Text='<%#Eval("四色图底图名称") %>'></asp:TextBox>
                                        <asp:Label ID="Label3" runat="server" Text='<%#Eval("四色图底图名称") %>' Visible="false"></asp:Label>
                                    </EditItemTemplate>
                                    <FooterTemplate>
                                        <asp:TextBox ID="InsertName" runat="server" Text=""></asp:TextBox>
                                    </FooterTemplate>
                                </asp:TemplateColumn>
                                <asp:TemplateColumn HeaderText="四色图底图路径">
                                     <ItemStyle Width="25%" Wrap="false"/>
                                    <ItemTemplate>
                                        <asp:HyperLink ID="HyperLink4" runat="server" Text="查看附件" Target="_blank"></asp:HyperLink>
                                        <asp:Label ID="Label2" runat="server" Text='<%#Eval("四色图底图路径") %>' Visible="false"></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <a class="btn_addPic" href="javascript:void(0);"><span><em></em>上传文件</span>
                                            <input class="icon_pencil" id="file4" title="支持.doc,.docx,.xls,.xlsx,.zip,.rar,.png,.jpeg,.gif,.jpg,.txt格式,文件小于5M" οnchange="show4()" tabindex="3" type="file" size="3 " name="pic" runat="server" /></a>
                                        <input type="text" id="Txt4"  class="icon_slice"/>
                                    </EditItemTemplate>
                                    <FooterTemplate>
                                        <a class="btn_addPic" href="javascript:void(0);"><span><em></em>上传文件</span>
                                            <input class="icon_pencil" id="Insertfile4" title="支持.doc,.docx,.xls,.xlsx,.zip,.rar,.png,.jpeg,.gif,.jpg,.txt格式,文件小于5M" οnchange="show4()" tabindex="3" type="file" size="3 " name="pic" runat="server" /></a>
                                         <input type="text" id="Txt5"  class="icon_slice"/>
                                    </FooterTemplate>

                                </asp:TemplateColumn>
                                <asp:TemplateColumn HeaderText="操作">
                                    <ItemStyle Width="25%" Wrap="false"/>
                                    <ItemTemplate>
                                        <asp:LinkButton ID="LinkButton1" runat="server" CommandName="edit" CssClass="btn-bj">编辑</asp:LinkButton>
                                        <asp:LinkButton ID="LinkButton2" runat="server" CommandName="delete" CssClass="btn-sc" OnClientClick="javascript:return confirm('是否确认删除?');">删除</asp:LinkButton>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:LinkButton ID="LinkButton1" runat="server" CommandName="update" OnClientClick="javascript:return confirm('是否确认更新?');">更新</asp:LinkButton>
                                        <asp:LinkButton ID="LinkButton2" runat="server" CommandName="cancel">取消</asp:LinkButton>
                                    </EditItemTemplate>
                                    <FooterTemplate>
                                        <asp:LinkButton ID="LinkButton1" runat="server" CommandName="insert" OnClientClick="javascript:return confirm('是否确认新增?');">新增</asp:LinkButton>
                                        <asp:LinkButton ID="LinkButton2" runat="server" CommandName="cancel">取消</asp:LinkButton>
                                    </FooterTemplate>
                                </asp:TemplateColumn>
                            </Columns>
                        </asp:DataGrid>
                        <div class="data_t">
                            <asp:Button ID="btnAdd" runat="server" Text="新增" CssClass="so" OnClick="btnAdd_Click" />
                        </div>

后台:

        protected void DataGrid1_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            string filesx = ".png,.jpeg,.jpg";
            int docSize = 20;
            TextBox EditName = e.Item.FindControl("EditName") as TextBox;
            HtmlInputFile file4 =e.Item.FindControl("file4") as HtmlInputFile;
            string sql = "";
            if (e.CommandName == "update")
            {
                HtmlInputText Txt4 = e.Item.FindControl("Txt4") as HtmlInputText;
                string fujian_path4 = string.Empty;
                string fujian_name4 = string.Empty;
                string name4 = string.Empty;
                HyperLink HyperLink4 = e.Item.FindControl("HyperLink4") as HyperLink;
                if (file4.PostedFile.ContentLength > 0)
                {
                    name4 = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file4.PostedFile.FileName);
                    if (filesx.IndexOf(Path.GetExtension(file4.PostedFile.FileName)) < 0)
                    {
                        UtilityFunctions.Success(this, "请选择以下图片类型:.png,.jpeg,.jpg");
                        return;
                    }
                    if (file4.PostedFile.ContentLength > docSize * 1024 * 1024)
                    {
                        UtilityFunctions.Success(this, "上传文件必须小于20M!");
                        return;
                    }
                    string fileDir = Server.MapPath("~/location/companyPic4/");
                    if (!Directory.Exists(fileDir))
                    {
                        Directory.CreateDirectory(fileDir);
                    }
                    fujian_name4 = name4;
                    fujian_path4 = Server.MapPath("~/location/companyPic4/") + fujian_name4;
                    file4.PostedFile.SaveAs(fujian_path4);
                    fujian_path4 = "location/companyPic4/" + fujian_name4;
                }
                sql = "update 风险分区四色图底图 set 四色图底图名称='" + EditName.Text.Trim() + "'";
                if (file4.PostedFile.FileName != null && file4.PostedFile.FileName!="")
                {
                    sql += ",四色图底图路径='" + fujian_path4.ToString() + "'";
                }
                sql += " where id='" + DataGrid1.DataKeys[e.Item.ItemIndex] + "'";
                CommonApp.ExecuteNonQuery(sql);
                UtilityFunctions.Success(this, "更新成功!");
                DataGrid1.EditItemIndex = -1;
                DataGrid1.ShowFooter = false;
                btnAdd.Visible = true;
            }
            else if (e.CommandName == "insert")
            {
                TextBox Insertid = e.Item.FindControl("Insertid") as TextBox;
                TextBox InsertName = e.Item.FindControl("InsertName") as TextBox;
                HtmlInputFile Insertfile4 = (HtmlInputFile)e.Item.FindControl("Insertfile4") as HtmlInputFile;

                string fujian_path4 = string.Empty;
                string fujian_name4 = string.Empty;
                string name4 = string.Empty;
                HyperLink HyperLink4 = e.Item.FindControl("HyperLink4") as HyperLink;
                if (Insertfile4.PostedFile.ContentLength <= 0 && Insertfile4.PostedFile.FileName.Length == 0)
                {
                    UtilityFunctions.Success(this, "请上传附件!");
                    return;
                }
                else
                {
                    name4 = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(Insertfile4.PostedFile.FileName);
                    if (filesx.IndexOf(Path.GetExtension(Insertfile4.PostedFile.FileName)) < 0)
                    {
                        UtilityFunctions.Success(this, "请选择以下图片类型:.png,.jpeg,.jpg");
                        return;
                    }
                    if (Insertfile4.PostedFile.ContentLength > docSize * 1024 * 1024)
                    {
                        UtilityFunctions.Success(this, "上传文件必须小于20M!");
                        return;
                    }
                    string fileDir = Server.MapPath("~/location/companyPic4/");
                    if (!Directory.Exists(fileDir))
                    {
                        Directory.CreateDirectory(fileDir);
                    }
                    fujian_name4 = name4;
                    fujian_path4 = Server.MapPath("~/location/companyPic4/") + fujian_name4;
                    Insertfile4.PostedFile.SaveAs(fujian_path4);
                    fujian_path4 = "location/companyPic4/" + fujian_name4;
                    // Txt4.Text= fujian_path4.ToString();
                }
                string id = CommonApp.Pkey("风险分区四色图底图", "id");
                sql = "insert into 风险分区四色图底图(id,companyid,四色图底图名称,四色图底图路径) values('" + id.ToString() + "','" + Session["companyid"].ToString() + "','" + InsertName.Text.Trim() + "','" + fujian_path4.ToString().Trim() + "')";
                int num = CommonApp.ExecuteNonQuery(sql);
                if (num > 0)
                {
                    UtilityFunctions.Success(this, "新增成功!");
                    DataGrid1.ShowFooter = false;
                }
                else
                {
                    UtilityFunctions.Success(this, "新增失败!");
                }
                btnAdd.Visible = true;
            }
            else if (e.CommandName == "edit")
            {
                DataGrid1.EditItemIndex = e.Item.ItemIndex;
                DataGrid1.ShowFooter = false;
                btnAdd.Visible = false;
            }
            else if (e.CommandName == "cancel")
            {
                DataGrid1.EditItemIndex = -1;
                DataGrid1.ShowFooter = false;
                btnAdd.Visible = true;
            }
            else if (e.CommandName == "delete")
            {
                Label Label6 = e.Item.FindControl("Label6") as Label;
                sql = "delete from 风险分区四色图底图 where id='" + Label6.Text.Trim() + "'";
                CommonApp.ExecuteNonQuery(sql);
                UtilityFunctions.Success(this, "删除成功!");
            }
            bindData();
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            DataGrid1.ShowFooter = true;
            btnAdd.Visible = false;
        }

        protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
        {

            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Label Label2 = e.Item.FindControl("Label2") as Label;
                HyperLink HyperLink4 = e.Item.FindControl("HyperLink4") as HyperLink;
                if (Label2.Text.Trim() != "")
                {
                    HyperLink4.NavigateUrl = "~/" + Label2.Text.Trim();
                }
            }
        }

        protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e) //绑定页
        {
            DataGrid1.CurrentPageIndex = e.NewPageIndex;
            bindData();
        }