使用DropDownList更新DataGridView

时间:2013-06-07 13:58:28

标签: c# asp.net drop-down-menu datagridview

我在修改时更新datagridview时遇到问题。 为了更新列(“批评”),我实现了一个包含2个选项的DropDownList。我可以选择一个选项,但是我点击“更新”按钮没有完成任何更改。

你能帮我解决这个问题吗?

非常感谢!

这是C#代码:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections;
using System.IO.Compression;  

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.BindData();

        }
    }

    private void BindData()
    {
        string query = "select ID,Client,Entite,Date,Source,Domaine,Categorie,SousCategorie,Criticite,Contenu from Results";
        SqlCommand cmd = new SqlCommand(query);
        GridView1.DataSource = GetData(cmd);
        GridView1.DataBind();
    }

    private DataTable GetData(SqlCommand cmd)
    {
        string strConnString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
        using (SqlConnection con = new SqlConnection(strConnString))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    return dt;
                }
            }
        }
    }

    protected void EditCustomer(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindData();
    }

    protected void CancelEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindData();
    }

    protected void RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowState == DataControlRowState.Edit)
        {
            DropDownList drpcategory1 = (DropDownList)e.Row.FindControl("DropDownList1");
            String query = "Select Criticite from Results";
            SqlCommand cmd = new SqlCommand(query);
            DataTable dt = GetData(cmd);
            drpcategory1.DataSource = dt;
            drpcategory1.DataTextField = "Criticite";
            drpcategory1.DataValueField = "Criticite";
            drpcategory1.DataBind();
        }
    }


    private void BindData(string Query)
    {
        string connectionstring = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(connectionstring))
        {
            conn.Open();
            using (SqlCommand comm = new SqlCommand(Query + ";select ID,Client,Entite,Date,Source,Domaine,Categorie,SousCategorie,Criticite,Contenu from Results", conn))
            {
                SqlDataAdapter da = new SqlDataAdapter(comm);
                DataSet ds = new DataSet();
                da.Fill(ds);
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
        }
    }

    protected void RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        Label ID = GridView1.Rows[e.RowIndex].FindControl("ID") as Label;
        TextBox Entite = GridView1.Rows[e.RowIndex].FindControl("txtEntite") as TextBox;
        TextBox Date = GridView1.Rows[e.RowIndex].FindControl("txtDate") as TextBox;
        TextBox Source = GridView1.Rows[e.RowIndex].FindControl("txtSource") as TextBox;
        TextBox Domaine = GridView1.Rows[e.RowIndex].FindControl("txtDomaine") as TextBox;
        TextBox Categorie = GridView1.Rows[e.RowIndex].FindControl("txtCategorie") as TextBox;
        TextBox SousCategorie = GridView1.Rows[e.RowIndex].FindControl("txtSousCategorie") as TextBox;
        TextBox Criticite = GridView1.Rows[e.RowIndex].FindControl("lblCriticite") as TextBox;
        TextBox Contenu = GridView1.Rows[e.RowIndex].FindControl("txtContenu") as TextBox;
        String UpdateQuery = string.Format("UPDATE Results SET Entite='{0}',Date='{1}',Source='{2}',Domaine='{3}',Categorie='{4}',SousCategorie='{5}',Criticite='{6}',Contenu='{7}' WHERE ID = {8}", Entite.Text, Date.Text, Source.Text, Domaine.Text, Categorie.Text, SousCategorie.Text, Criticite.Text, Contenu.Text, ID.Text);
        GridView1.EditIndex = -1;
        BindData(UpdateQuery);
    }
}

这里是aspx默认页面:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

 <asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
    </asp:Content>

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<p>
    Début</p>
<p>
    &nbsp;</p>
<p>
    &nbsp;</p>
<asp:GridView ID="GridView1" runat="server" GridLines="None" AutoGenerateColumns="false"
      AlternatingRowStyle-BackColor="#EEEEEE" EditRowStyle-BorderColor="Red"
    OnRowEditing = "EditCustomer" 
    OnRowDataBound = "RowDataBound"
    OnRowUpdating = "RowUpdating" 
    OnRowCancelingEdit = "CancelEdit">

<Columns>
         <asp:TemplateField Visible="false" HeaderText="ID">
            <ItemTemplate>
              <asp:Label runat="server" ID="ID" Text='<%#Eval("ID")%>' />
            </ItemTemplate>
         </asp:TemplateField>


         <asp:TemplateField HeaderText="Client">
            <ItemTemplate>
              <asp:Label runat="server" ID="Client" Text='<%#Eval("Client") %>' />
            </ItemTemplate>

            <EditItemTemplate>
               <asp:TextBox runat="server" ID="txtClient" Text='<%#Eval("Client") %>' />
            </EditItemTemplate>
        </asp:TemplateField>

         <asp:TemplateField HeaderText="Entite">
            <ItemTemplate>
              <asp:Label runat="server" ID="Entite" Text='<%#Eval("Entite") %>' />
            </ItemTemplate>

            <EditItemTemplate>
               <asp:TextBox runat="server" ID="txtEntite" Text='<%#Eval("Entite") %>' />
            </EditItemTemplate>
        </asp:TemplateField>

         <asp:TemplateField HeaderText="Date">
            <ItemTemplate>
              <asp:Label runat="server" ID="Date" Text='<%#Eval("Date") %>' />
            </ItemTemplate>

            <EditItemTemplate>
               <asp:TextBox runat="server" ID="txtDate" Text='<%#Eval("Date") %>' />
            </EditItemTemplate>
        </asp:TemplateField>

         <asp:TemplateField HeaderText="Source">
            <ItemTemplate>
              <asp:Label runat="server" ID="Source" Text='<%#Eval("Source") %>' />
            </ItemTemplate>

            <EditItemTemplate>
               <asp:TextBox runat="server" ID="txtSource" Text='<%#Eval("Source") %>' />
            </EditItemTemplate>
        </asp:TemplateField>

         <asp:TemplateField HeaderText="Domaine">
            <ItemTemplate>
              <asp:Label runat="server" ID="Domaine" Text='<%#Eval("Domaine") %>' />
            </ItemTemplate>

            <EditItemTemplate>
               <asp:TextBox runat="server" ID="txtDomaine" Text='<%#Eval("Domaine") %>' />
            </EditItemTemplate>
        </asp:TemplateField>

         <asp:TemplateField HeaderText="Categorie">
            <ItemTemplate>
              <asp:Label runat="server" ID="Categorie" Text='<%#Eval("Categorie") %>' />
            </ItemTemplate>

            <EditItemTemplate>
               <asp:TextBox runat="server" ID="txtCategorie" Text='<%#Eval("Categorie") %>' />
            </EditItemTemplate>
        </asp:TemplateField>

         <asp:TemplateField HeaderText="SousCategorie">
            <ItemTemplate>
              <asp:Label runat="server" ID="SousCategorie" Text='<%#Eval("SousCategorie") %>' />
            </ItemTemplate>

            <EditItemTemplate>
               <asp:TextBox runat="server" ID="txtSousCategorie" Text='<%#Eval("SousCategorie") %>' />
            </EditItemTemplate>
        </asp:TemplateField>

         <asp:TemplateField HeaderText="Criticite">
            <ItemTemplate>
              <asp:Label runat="server" ID="lblCriticite" Text='<%#Eval("Criticite") %>' />
            </ItemTemplate>

            <EditItemTemplate>
               <asp:TextBox runat="server" ID="lblCriticite" Text='<%#Eval("Criticite") %>' Visible = "false" />
               <asp:DropDownList ID="DropDownList1" runat = "server"></asp:DropDownList>
            </EditItemTemplate>
        </asp:TemplateField>

         <asp:TemplateField HeaderText="Contenu">
            <ItemTemplate>
              <asp:Label runat="server" ID="Contenu" Text='<%#Eval("Contenu") %>' />
            </ItemTemplate>

            <EditItemTemplate>
               <asp:TextBox runat="server" ID="txtContenu" Text='<%#Eval("Contenu") %>' />
            </EditItemTemplate>
        </asp:TemplateField>

   <asp:TemplateField HeaderText="Action">

      <ItemTemplate>
      <asp:LinkButton ID="btnEdit" Text="Edit" runat="server" CommandName="Edit" />
      </ItemTemplate>

      <EditItemTemplate>
      <asp:LinkButton ID="btnUpdate" Text="Update" runat="server" CommandName="Update" />
      <asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" />
      </EditItemTemplate>

      </asp:TemplateField>

   </Columns>
</asp:GridView>
    </asp:Content>

1 个答案:

答案 0 :(得分:0)

更改以下行

TextBox Criticite = GridView1.Rows[e.RowIndex].FindControl("lblCriticite") as TextBox;

成:

DropDownList Criticite = GridView1.Rows[e.RowIndex].FindControl("DropDownList1") as DropDownList;

并使用Criticite.SelectedValue代替Criticite.Text

此外,您的更新查询存在SQL注入攻击的风险。