GridView UPDATE语句问题

时间:2014-08-03 15:22:30

标签: c# sql asp.net gridview updatepanel

除了更新客户外,我的所有gridview功能都有效(插入,删除)。我无法弄清楚为什么我的更新声明不起作用。当我在方法上设置断点时,单击"更新"按钮,浏览器中没有任何反应,它也没有碰到我的断点。有没有人在我的代码中看到任何可能存在问题的内容?我将包含我的ASP.NET gridview代码,以及与gridview一起使用的方法。

ASP.NET:

<div id="dvGrid" style="padding: 2px">
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">

        <ContentTemplate>

            <asp:GridView ID="GridView1" DataKeyNames="PersonID" runat="server" AutoGenerateColumns="False"
                Font-Names="Arial" Font-Size="10pt" ShowFooter="true" OnRowEditing="EditCustomer"
                OnRowUpdating="UpdateCustomer" OnRowCancelingEdit="CancelEdit" Height="95px">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <%# Container.DataItemIndex + 1 %>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="First Name">
                        <ItemTemplate>
                            <asp:Label ID="lblFirstName" runat="server" Height="20px" Text='<%# Eval("FirstName")%>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtFirstName" runat="server" Text='<%# Eval("FirstName")%>'></asp:TextBox>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txtFirstName" Width="60px" MaxLength="15" runat="server"></asp:TextBox>
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Last Name">
                        <ItemTemplate>
                            <asp:Label ID="lblLastName" runat="server" Text='<%# Eval("LastName")%>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtLastName" runat="server" Text='<%# Eval("LastName")%>'></asp:TextBox>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txtLastName" Width="60px" MaxLength="15" runat="server"></asp:TextBox>
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Age">
                        <ItemTemplate>
                            <asp:Label ID="lblAge" runat="server" Text='<%# Eval("Age")%>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtAge" runat="server" Text='<%# Eval("Age")%>'></asp:TextBox>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txtAge" Width="30px" MaxLength="2" runat="server"></asp:TextBox>
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Activity Card">
                        <ItemTemplate>
                            <asp:Label ID="lblActivityCard" runat="server" wrap="true" Text='<%# Eval("ActivityCard")%>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtActivityCard" runat="server" Text='<%# Eval("ActivityCard")%>'></asp:TextBox>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txtActivityCard" Width="50px" MaxLength="7" runat="server"></asp:TextBox>
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Cell Phone">
                        <ItemTemplate>
                            <asp:Label ID="lblCell" runat="server" Text='<%# Eval("Cell#")%>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtCell" runat="server" Text='<%# Eval("Cell#")%>'></asp:TextBox>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txtCell" Width="70px" MaxLength="10" runat="server"></asp:TextBox>
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Emergency Phone">
                        <ItemTemplate>
                            <asp:Label ID="lblEmergency" runat="server" Text='<%# Eval("Emergency#")%>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtEmergency" runat="server" Text='<%# Eval("Emergency#")%>'></asp:TextBox>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txtEmergency" Width="70px" MaxLength="10" runat="server"></asp:TextBox>
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Boat Type">
                        <ItemTemplate>
                            <asp:Label ID="lblBoat" runat="server" Text='<%# Eval("CraftType")%>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtBoat" runat="server" Text='<%# Eval("CraftType")%>'></asp:TextBox>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txtBoat" Width="80px" runat="server"></asp:TextBox>
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Initials">
                        <ItemTemplate>
                            <asp:Label ID="lblInitials" runat="server" Text='<%# Eval("Initials")%>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtInitials" runat="server" Text='<%# Eval("Initials")%>'></asp:TextBox>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txtInitials" Width="30px" MaxLength="2" runat="server"></asp:TextBox>
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Group #">
                        <ItemTemplate>
                            <asp:Label ID="lblGroup" runat="server" Text='<%# Eval("Group#")%>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtGroup" runat="server" Text='<%# Eval("Group#")%>'></asp:TextBox>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txtGroup" MaxLength="2" Width="20px" runat="server"></asp:TextBox>
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:LinkButton ID="lnkRemove" runat="server" CommandArgument='<%# Eval("PersonID")%>'
                                OnClientClick="return confirm('Are you sure you want to delete?')" Text="Delete"
                                OnClick="DeleteCustomer"></asp:LinkButton>
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:Button ID="btnAdd" runat="server" Text="Submit" OnClick="AddNewCustomer" />
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:CommandField ShowEditButton="True" />
                </Columns>
                <EmptyDataTemplate>
                    <span style="font-size: 13px; font-weight: bold; color: Blue;">No records are available
                        matching your selected filter. Please fill out the form below to add the first customer
                        .. </span>
                </EmptyDataTemplate>
            </asp:GridView>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="GridView1" />
        </Triggers>
    </asp:UpdatePanel>

C#Code-Behind:

    private void BindData()
    {
        string strQuery = "SELECT Person.PersonID, Person.FirstName AS FirstName, Person.LastName AS LastName, Person.Emergency# AS Emergency#, Person.Cell# AS Cell#, Person.Age AS Age, " +
                             "Person.ActivityCard AS ActivityCard, Person.CraftType AS CraftType, Person.Initials AS Initials, Person.Group# AS Group# " +
                             "FROM Person INNER JOIN " +
                             "TripSchedule ON Person.PersonID = TripSchedule.PersonID where TripSchedule.Date = @Datee and " +
                             "TripSchedule.Time = @Timee and TripSchedule.TripType = @Typee order by Person.Group#;";

        SqlCommand cmd = new SqlCommand(strQuery);

        cmd.Parameters.AddWithValue("@Datee", TextBox1.Text);
        cmd.Parameters.AddWithValue("@Timee", ddlTripTime.SelectedItem.ToString());
        cmd.Parameters.AddWithValue("@Typee", ddlTripType.SelectedItem.ToString());
        string trip = ddlTripType.SelectedItem.ToString();

        if (trip == "CANOE / KAYAK")
        {
            GridView1.HeaderStyle.BackColor = System.Drawing.Color.FromName("Yellow");
            GridView1.RowStyle.BackColor = System.Drawing.Color.FromArgb(255, 255, 0);
            GridView1.AlternatingRowStyle.BackColor = System.Drawing.Color.FromArgb(255, 250, 205);
        }
        GridView1.DataSource = GetData(cmd);
        GridView1.DataBind();
    }

    private DataTable GetData(SqlCommand cmd)
    {
        DataTable dt = new DataTable();
        SqlConnection conn = new SqlConnection(strConnString);
        SqlDataAdapter adapter = new SqlDataAdapter();
        cmd.CommandType = CommandType.Text;
        cmd.Connection = conn;
        adapter.SelectCommand = cmd;
        adapter.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 UpdateCustomer(object sender, GridViewUpdateEventArgs e)
    {
        string nFirstName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtFirstName")).Text;
        string nLastName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtLastName")).Text;
        string nEmergency = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEmergency")).Text;
        string nCell = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtCell")).Text;
        string nAge = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtAge")).Text;
        string nActivityCard = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtActivityCard")).Text;
        string nBoat = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtBoat")).Text;
        string nInitials = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtInitials")).Text;
        string nGroup = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtGroup")).Text;
        long personID = long.Parse(GridView1.DataKeys[e.RowIndex].Values["PersonID"].ToString());
        SqlConnection con = new SqlConnection(strConnString);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "update Person set FirstName=@FirstName, LastName=@LastName, " +
        "Emergency#=@Emergency, Cell#=@Cell, Age=@Age, ActivityCard=@ActivityCard, Initials=@Initials, CraftType=@Boat, Group#=@Group " +
        "where PersonID=@Pid;" +
        "SELECT Person.PersonID AS PersonID, Person.FirstName AS FirstName, Person.LastName AS LastName, Person.Emergency# AS Emergency#, Person.Cell# AS Cell#, Person.Age AS Age, " +
        "Person.ActivityCard AS ActivityCard, Person.CraftType AS CraftType, Person.Initials AS Initials, Person.Group# AS Group# " +
        "FROM Person INNER JOIN " +
        "TripSchedule ON Person.PersonID = TripSchedule.PersonID where TripSchedule.Date = @Date and " +
        "TripSchedule.Time = @Time and TripSchedule.TripType = @Type order by Person.Group#;";
        cmd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = nFirstName;
        cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = nLastName;
        cmd.Parameters.Add("@Emergency", SqlDbType.NChar).Value = nEmergency;
        cmd.Parameters.Add("@Cell", SqlDbType.NChar).Value = nCell;
        cmd.Parameters.Add("@Age", SqlDbType.NChar).Value = nAge;
        cmd.Parameters.Add("@ActivityCard", SqlDbType.NChar).Value = nActivityCard;
        cmd.Parameters.Add("@Initials", SqlDbType.NChar).Value = nInitials;
        cmd.Parameters.Add("@Boat", SqlDbType.VarChar).Value = nBoat;
        cmd.Parameters.Add("@Group", SqlDbType.VarChar).Value = nGroup;
        cmd.Parameters.AddWithValue("@Date", TextBox1.Text);
        cmd.Parameters.AddWithValue("@Time", ddlTripTime.SelectedItem.ToString());
        cmd.Parameters.AddWithValue("@Type", ddlTripType.SelectedItem.ToString());
        cmd.Parameters.AddWithValue("@Pid", personID);
        GridView1.EditIndex = -1;
        GridView1.DataSource = GetData(cmd);
        GridView1.DataBind();

    }

0 个答案:

没有答案
相关问题