尝试在C#应用程序中创建数据网格,当您单击“编辑”按钮时,整个网格将消失。我有这个工作片刻,但无法取回它。任何人都可以在我的代码中看到任何错误吗?
这是ASPX:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="True" CodeBehind="frmViewLoadHistory.aspx.cs" Inherits="PBR.ViewLoadHistory" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<br />
<div id="divGrid" style='position:absolute; width:920px; height:400px; overflow:auto'>
<asp:DataGrid ID="DataGrid_Roster" runat="server"
AllowPaging="True" AllowSorting="True" CellPadding="4" ForeColor="#333333" GridLines="None"
OnCancelCommand="DataGrid_Roster_CancelCommand"
OnUpdateCommand="DataGrid_Roster_UpdateCommand"
OnEditCommand="DataGrid_Roster_EditCommand">
<AlternatingItemStyle Font-Bold="False" Font-Italic="False"
Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
<EditItemStyle BackColor="#999999" Font-Bold="False" Font-Italic="False"
Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="False" Font-Italic="False"
Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="False" Font-Italic="False"
Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
<PagerStyle BackColor="#284775" Font-Bold="False" Font-Italic="False"
Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
<SelectedItemStyle BackColor="#E2DED6" Font-Bold="False" Font-Italic="False"
Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
<Columns>
<asp:EditCommandColumn ButtonType="PushButton" CancelText="Cancel"
EditText="Edit" UpdateText="Update"></asp:EditCommandColumn>
</Columns>
</asp:DataGrid>
</div>
<br />
</asp:Content>
代码隐藏:
public string stringSelectedValue { get; set; }
string str2 = System.Configuration.ConfigurationManager.ConnectionStrings["RosterConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
Show_Data();
}
}
public void Show_Data()
{
SqlConnection sqlconnectionStatus2 = new SqlConnection(str2);
string sqlquery2;
sqlquery2 = "SELECT * FROM [tblcensus]";
SqlConnection con2 = new SqlConnection(str2);
SqlCommand cmd2 = new SqlCommand(sqlquery2, con2);
SqlDataAdapter adapter2 = new SqlDataAdapter(cmd2);
// Fill the DataSet.
DataSet ds2 = new DataSet();
adapter2.Fill(ds2, "dailyview");
// Perform the binding.
DataGrid_Roster.DataSource = ds2;
DataGrid_Roster.DataBind();
}
protected void DataGrid_Roster_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid_Roster.EditItemIndex = e.Item.ItemIndex;
DataGrid_Roster.DataBind();
}
protected void DataGrid_Roster_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
// The quantity is in the 7th column.
TableCell quantityCell = e.Item.Cells[6];
// The TextBox is the 0th element of the Controls collection.
TextBox quantityBox = (TextBox)quantityCell.Controls[0];
// Extract the quantity from the box.
int quantity = System.Int32.Parse(quantityBox.Text);
// Use quantity to update the data source.
// Switch out of edit mode.
DataGrid_Roster.EditItemIndex = -1;
DataGrid_Roster.DataBind();
}
protected void DataGrid_Roster_CancelCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid_Roster.EditItemIndex = -1;
DataGrid_Roster.DataBind();
}
我可以发誓当我工作时我不必担心IsPostBack部分,但是在抛出错误之前它似乎没有处理任何东西。现在我没有收到任何错误,但DataGrid根本没有出现。
答案 0 :(得分:2)
在EditCommand事件处理程序中,调用Show_Data方法,而不是在网格上调用Databind。
DataGrid_Roster.EditItemIndex = e.Item.ItemIndex;
Show_Data();
数据不会在回发之间缓存,因此每次都需要获取数据,或者您需要设置其他缓存机制。