GridView的自带编辑及查看功能

Gridview显示部分 

<asp:GridView ID="gvSatisfactionItem" runat="server" AutoGenerateColumns="False" 
                BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px"
                CellPadding="4" ForeColor="Black" GridLines="Vertical" DataKeyNames="ID"
                            onrowcancelingedit="gvSatisfactionItem_RowCancelingEdit"
                            onrowdatabound="gvSatisfactionItem_RowDataBound"
                            onrowediting="gvSatisfactionItem_RowEditing"
                            onrowupdating="gvSatisfactionItem_RowUpdating"
                            onselectedindexchanging="gvSatisfactionItem_SelectedIndexChanging" >
               
                <FooterStyle BackColor="#CCCC99" />
                <RowStyle HorizontalAlign="center" />
                <Columns>
                    <asp:TemplateField HeaderStyle-Width="20px">
                    <HeaderTemplate>
                    <asp:CheckBox ID="cbAll" runat="server" />
                    </HeaderTemplate>
                    <ItemTemplate>
                    <asp:CheckBox ID="cbItem" runat="server" />                  
                    </ItemTemplate>

                    <HeaderStyle Width="20px"></HeaderStyle>
                    </asp:TemplateField>
                    <asp:BoundField DataField="ID" HeaderText="編號" ReadOnly="true"  HeaderStyle-Width="30px"  HeaderStyle-Font-Names="宋體" HeaderStyle-Font-Size="Larger">
                    <HeaderStyle Font-Names="宋體" Font-Size="Larger" Width="30px"></HeaderStyle>
                    <ControlStyle Width="30px" />
                    </asp:BoundField>                                       
                    <asp:BoundField DataField="TitleName" HeaderText="調查標題"
                        HeaderStyle-Width="200px"  HeaderStyle-Font-Names="宋體" HeaderStyle-Font-Size="Larger">
                    <HeaderStyle Font-Names="宋體" Font-Size="Larger" Width="200px"></HeaderStyle>
                    <ControlStyle Width="200px" />
                    </asp:BoundField>
                                       
                    <%--<asp:TemplateField HeaderText="調查標題">
                        <ItemTemplate>
                            <asp:HyperLink ID="HyperLink3" runat="server" NavigateUrl='<%# "satisfiedsurvey.aspx?id="+Eval("ID", "{0}") %>'
                              Text='<%# Bind("TitleName") %>'></asp:HyperLink>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="TBTitleName" Text='<%# Bind("TitleName") %>' runat="server" Width="150px" />
                        </EditItemTemplate>
                        <ItemStyle Width="150px" />
                    </asp:TemplateField>--%>
                                       
                    <asp:TemplateField HeaderText="所屬主題">
                        <ItemTemplate>
                            <%# Eval("Title")%>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:HiddenField ID="HDFID" runat="server" Value='<%# Eval("ID") %>' />
                            <asp:DropDownList ID="DDLID" runat="server" Width="150px" />
                        </EditItemTemplate>
                        <ItemStyle Width="150px" />
                    </asp:TemplateField>
                   
                    <asp:BoundField DataField="Desc" HeaderText="備註"
                        HeaderStyle-Width="50px"  HeaderStyle-Font-Names="宋體" HeaderStyle-Font-Size="Larger">
                    <HeaderStyle Font-Names="宋體" Font-Size="Larger" Width="50px"></HeaderStyle>
                    <ControlStyle Width="50px" />
                    </asp:BoundField>
                   
                    <asp:CommandField HeaderText="編輯" ShowSelectButton="true" SelectText="查看" ShowEditButton="True" EditText="編輯" UpdateText="更新 " CancelText="取消" />
                    
                </Columns>              
                     
                <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
                <SelectedRowStyle BackColor="#FF99FF" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#81DC25" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="#DDF7D9" />
            </asp:GridView>

 

后台代码部分

protected void gvSatisfactionItem_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //選中列表項操作
            try
            {
                if (e.Row.RowType == DataControlRowType.Header)
                {
                    CheckBox cbAll = e.Row.FindControl("cbAll") as CheckBox;
                    cbAll.Attributes.Add("onClick", "checkAll();");
                }
            }
            catch (Exception ex)
            {

                Response.Write("<script>alert('" + ex.ToString() + "')</script>");
            }

            //編輯時綁定資料欄位
            if (((DropDownList)e.Row.FindControl("DDLID")) != null)
            {
                DropDownList ddlID = (DropDownList)e.Row.FindControl("DDLID");

                //  生成 DropDownList 的值,綁定資料

                DataSet ds = objSatisfactionClass.getDcTitleInfo();
                ddlID.DataSource = ds.Tables[0].DefaultView;
                ddlID.DataTextField = "TitleName";
                ddlID.DataValueField = "ID";
                ddlID.DataBind();
                //

                //  選中 DropDownList
                ddlID.SelectedValue = ((HiddenField)e.Row.FindControl("HDFID")).Value;
                //
            }

           
        }

        protected void gvSatisfactionItem_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.gvSatisfactionItem.EditIndex = -1;
            int ID = Int32.Parse(Request.QueryString["ID"].ToString());
            this.gvSatisfactionItemDataBind(ID);
        }

        protected void gvSatisfactionItem_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.gvSatisfactionItem.EditIndex = e.NewEditIndex;
            int ID = Int32.Parse(Request.QueryString["ID"].ToString());
            this.gvSatisfactionItemDataBind(ID);
        }

        protected void gvSatisfactionItem_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            //獲取相應的值
            int id = Convert.ToInt32(this.gvSatisfactionItem.DataKeys[e.RowIndex].Values[0].ToString());
            //string titleName = ((TextBox)this.gvSatisfactionItem.Rows[e.RowIndex].FindControl("TBTitleName")).Text.ToString().Trim();
            string titleName = ((TextBox)(this.gvSatisfactionItem.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
            int flag = Convert.ToInt32(((DropDownList)this.gvSatisfactionItem.Rows[e.RowIndex].FindControl("DDLID")).SelectedValue);
            string desc = ((TextBox)(this.gvSatisfactionItem.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim();
            //更新資料庫
            objSatisfactionClass.updateItemTitle(id,titleName,flag,desc);
            //刷新頁面
            this.gvSatisfactionItem.EditIndex = -1;
            int ID = Int32.Parse(Request.QueryString["ID"].ToString());
            this.gvSatisfactionItemDataBind(ID);
        }

        protected void imgDel_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                int rowCount = this.gvSatisfactionItem.Rows.Count;
                string sql = "delete tbldcTitle where ID in (";

                for (int i = 0; i < rowCount; i++)
                {
                    CheckBox cbID = (CheckBox)this.gvSatisfactionItem.Rows[i].Cells[0].FindControl("cbItem");
                    if (cbID != null && cbID.Checked)
                    {
                        int id = Convert.ToInt32(this.gvSatisfactionItem.Rows[i].Cells[1].Text.ToString());
                        sql += id + ",";
                    }
                }

                int num = sql.Length;
                sql = sql.Remove(num - 1);//把最後的一個逗號去掉
                sql += ")";
                //Response.Write(sql);
                SqlHelper.ExecuteNonQuery(sql);
                int ID = Int32.Parse(Request.QueryString["ID"].ToString());
                this.gvSatisfactionItemDataBind(ID);
            }
            catch (Exception ex)
            {

                Response.Write("<script>alert('" + ex.ToString() + "')</script>");
            }
        }

        protected void imgAdd_Click(object sender, ImageClickEventArgs e)
        {
            this.Panel1.Visible = true;
        }

        protected void btsubmit_Click(object sender, EventArgs e)
        {
            //獲取相應變數的值
            string titleName = this.txtTitleName.Text.Trim();
            DateTime endTime = Convert.ToDateTime(this.txtEndTime.Text);
            string desc = this.txtDesc.Text.Trim();         
           
            Response.Write("<script lauguage = javascript>window.alert('添加成功!');window.location.href='satisfiedsurvey.aspx';</script>");
           
        }

        protected void btreset_Click(object sender, EventArgs e)
        {
            this.txtTitleName.Text = "";
            this.txtEndTime.Text = "";
            this.txtDesc.Text = "";
        }

        protected void gvSatisfactionItem_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            this.gvSatisfactionItem.SelectedIndex = e.NewSelectedIndex;
            int id = Convert.ToInt32(this.gvSatisfactionItem.DataKeys[e.NewSelectedIndex].Values[0].ToString());
            ConstClass.idItems = id;
            this.Panel2.Visible = true;
            this.gvSatisfactionItemsDataBind(id);
        }

posted on 2010-04-14 12:40  peter_zhang  阅读(1841)  评论(1编辑  收藏  举报