更新按钮上的GridView消失单击,不更新记录

时间:2017-08-28 14:58:17

标签: c# asp.net sql-server gridview kentico

所以我有一个带有GridView的c#页面。出于某种原因,在添加标签和模板进行编辑后,当我单击编辑列中的更新按钮时,GridView将消失,并且所有记录都不会更改。我知道一旦用户点击更新,我需要在编辑框中对数据进行数据绑定,但我不确定如何。任何人都可以提出任何建议吗?

这是我的代码。

public partial class CMSWebParts_Custom_Development_DevWebPart : 
CMSAbstractWebPart
{

public string paramId;

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ViewState["RefUrl"] = Request.UrlReferrer.ToString();
    }
    string urlString = Request.UrlReferrer.ToString();
    paramId = HttpUtility.ParseQueryString(urlString).Get("Theid");

    DispatchNumberLabel.Text = getDispatchNumber();
    //TerminalLabel.Text = getTerminal();
    DispatchInfoIdLabel.Text = getDispatchInfoId();
    DriverNameLabel.Text = getDriverName();
}

protected string getDispatchInfoId()
{
    string result = DriverDropDownList.SelectedValue;
    return result;
}

protected string getDispatchNumber()
{
    string result = paramId;
    return result;
}

protected string getTerminal()
{
    string result;
    string connectionString = "";
    using (var con = new SqlConnection(connectionString))
    {
        var sql = "Select Terminal from Form_IntranetSharpTransit_DispatchInfo where ([Dispatch] = @Dispatch)";         
        using (var cmd = new SqlCommand(sql, con))
        {
            cmd.Parameters.AddWithValue("@Dispatch", getDispatchNumber());
            con.Open();
            result = (string)cmd.ExecuteScalar();
        }
    }
    return result;
}

protected string getDriverName()
{
    string result;
    string connectionString = "";
    using (var con = new SqlConnection(connectionString))
    {
        var sql = "Select Driver from Form_IntranetSharpTransit_DispatchInfo WHERE ([DispatchInfoID] = @DispatchInfoId)";
        using (var cmd = new SqlCommand(sql, con))
        {
            cmd.Parameters.AddWithValue("@DispatchInfoId", getDispatchInfoId());
            con.Open();
            result = (string)cmd.ExecuteScalar();
        }
    }
    return result;
} 
}

这是我背后的代码。

<%@ Control Language="C#" AutoEventWireup="true" 
CodeFile="~/CMSWebParts/Custom/Development/DevWebPart.ascx.cs" 
Inherits="CMSWebParts_Custom_Development_DevWebPart" %>
<%--<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{

}
</script>--%>Dispatch Number:
<asp:Label ID="DispatchNumberLabel" runat="server"></asp:Label>
<p>
Terminal:
<asp:Label ID="TerminalLabel" runat="server"></asp:Label>
</p>
<p>
Current
Dispatch Info ID:
<asp:Label ID="DispatchInfoIdLabel" runat="server"></asp:Label>
</p>
<p>
&nbsp;Driver Name:
<asp:Label ID="DriverNameLabel" runat="server"></asp:Label>
</p>
<asp:DropDownList ID="DriverDropDownList" runat="server" AutoPostBack="True" 
DataSourceID="DriverListData" DataTextField="Select Driver" 
DataValueField="Column1">
</asp:DropDownList>
<asp:SqlDataSource ID="DriverListData" runat="server" ConnectionString="<%$ 
ConnectionStrings:ConnectionString %>" ProviderName="<%$ 
ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="Select 
'', 'Select Driver' as 'Select Driver'
from Form_IntranetSharpTransit_DispatchInfo
Union
SELECT     DispatchInfoID, Driver + ' - ' + CAST(DispatchInfoID AS 
varchar(50)) AS Expr1
FROM         Form_IntranetSharpTransit_DispatchInfo 
WHERE     (Dispatch = ?) AND (DaysOff NOT LIKE '%' + (SELECT DispatchDay 
FROM Form_IntranetSharpTransit_DailyDispatch Where DailydispatchID = ?) + 
'%') AND (Status LIKE 'Available')">
<SelectParameters>
    <asp:QueryStringParameter Name="?" QueryStringField="Theid" />
    <asp:QueryStringParameter Name="?" QueryStringField="Theid" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
AutoGenerateEditButton="True" DataSourceID="Store1Data" EditIndex="0" 
OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
    <asp:TemplateField HeaderText="Store 1:">
        <EditItemTemplate>
            <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" 
OnTextChanged="TextBox1_TextChanged" Text='<%# Bind("[Store Number]") %>'>
</asp:TextBox>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="Label1" runat="server" Text='<%# Bind("[Store 
Number]") %>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="Store1Data" runat="server" ConnectionString="<%$ 
ConnectionStrings:ConnectionString %>" ProviderName="<%$ 
ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT     
(SELECT     Store
                   FROM          Form_IntranetSharpTransit_TourPlan
                   WHERE      (TourPlanID = 
Form_IntranetSharpTransit_DispatchInfo_1.Store1)) AS 'Store Number', 
Location1, Pro1, Store1Export
FROM         Form_IntranetSharpTransit_DispatchInfo AS 
Form_IntranetSharpTransit_DispatchInfo_1
WHERE     (DispatchInfoID = ?)">
<SelectParameters>
    <asp:ControlParameter ControlID="DriverDropDownList" Name="?" 
PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>

我想要的只是用编辑按钮显示表格。单击编辑按钮时,我希望表格中的标签成为编辑框(如我的模板所示)。在用户修改了编辑框后,他们可以单击更新按钮,将更改提交到数据库并刷新表以显示更改的值。

这似乎非常简单,但我现在已经苦苦挣扎了大约3个月。

请帮忙。

1 个答案:

答案 0 :(得分:0)

这是您尝试实现的最小但又有效的示例。 虽然我必须承认,Microsoft Documentation对于这个片段来说非常方便。

Default.aspx的

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="UpdateGridViewOnPostBack_45921943.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView runat="server" ID="dgv1" AutoGenerateEditButton="true" OnRowEditing="dgv1_RowEditing" OnRowUpdating="dgv1_RowUpdating">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label runat="server" ID="gvCol1Label" Text='<%#Bind("field1") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox runat="server" ID="gvCol1Txtbx" Text='<%# Bind("field1") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Web.UI.WebControls;
using System.Collections.ObjectModel;

namespace UpdateGridViewOnPostBack_45921943
{
    public partial class Default : System.Web.UI.Page
    {
        static ObservableCollection<dgvEntry> dgv1Source;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Not a postback
                dgv1Source = new ObservableCollection<dgvEntry>();
                dgv1Source.Add(new dgvEntry { field1 = "the field coming from the data source" });
                initializeDGV(IsPostBack);
            }
            else
            {
                //is a postback
                if (dgv1Source == null)
                {
                    dgv1Source = new ObservableCollection<dgvEntry>();
                }
                initializeDGV(IsPostBack);
            }
        }

        private void initializeDGV(bool isPostback)
        {
            dgv1.DataSource = dgv1Source;
            dgv1.AutoGenerateColumns = false;
            if (!isPostback)
            {
                dgv1.DataBind();
            }
        }

        protected void dgv1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            dgv1.EditIndex = e.NewEditIndex;
            dgv1.DataBind();
        }

        protected void dgv1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

            GridViewRow row = dgv1.Rows[e.RowIndex];
            dgv1Source[e.RowIndex].field1 = ((TextBox)row.Cells[1].Controls[1]).Text;
            dgv1.EditIndex = -1;
            dgv1.DataBind();
        }
    }

    public class dgvEntry
    {
        public string field1 { get; set; }
        //public string field2 { get; set; }
        //public string field3 { get; set; }
    }
}