[英]OnRowUpdating does not work even if i have a !postback in page_load
我是初學者,我正在 asp.net 中創建一個 crud 網頁,但我無法使更新(保存)按鈕工作。 這是我的代碼片段:
Main.aspx 中的保存和取消按鈕
<div height: 334px;" class="gridview">
<asp:GridView ID="RetailInfoGridView"
AutoGenerateColumns="False"
ShowFooter="true"
DataKeyNames="StockKeepingID"
runat="server"
ShowHeaderWhenEmpty="True"
HeaderStyle-ForeColor="White"
AlternatingRowStyle="alt"
EmptyDataText="No Records Found"
AllowSorting="True"
OnRowCommand="RetailInfoGridView_RowCommand"
OnDataBound="RetailInfoGridView_DataBound"
OnRowDataBound="RetailInfoGridView_RowDataBound"
OnRowEditing="RetailInfoGridView_RowEditing"
OnRowCancelingEdit="RetailInfoGridView_RowCancelingEdit"
OnRowUpdating="RetailInfoGridView_RowUpdating"
onRowDeleting="RetailInfoGridView_RowDeleting"
OnSelectedIndexChanged="RetailInfoGridView_SelectedIndexChanged">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Edit" CommandName="Edit" runat="server" Text="Edit" ToolTip="Edit" />
<asp:Button ID="Delete" CommandName="Delete" runat="server" Text="Delete" ToolTip="Delete" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="Save" CommandName="Save" runat="server" Text="Save" ToolTip="Save" />
<asp:Button ID="Cancel" CommandName="Cancel" runat="server" Text="Cancel" ToolTip="Cancel" />
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="Add" CommandName="Add" runat="server" Text="Add" ToolTip="Add" />
</FooterTemplate>
</asp:TemplateField>
<EditItemTemplate>
<asp:Button ID="Save" CommandName="Save" runat="server" Text="Save" ToolTip="Save" />
<asp:Button ID="Cancel" CommandName="Cancel" runat="server" Text="Cancel" ToolTip="Cancel" />
</EditItemTemplate>
...
主.aspx.cs
string connectionString = "Data Source=102000-LSU-2216;Initial Catalog=loginDB;Integrated Security=True";
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
PopulateGridView();
}
}
protected void SearchTextBox_TextChanged(object sender, EventArgs e)
{
/* connect.Open();
string query = "SELECT * FROM RetailInfo WHERE StockKeepingUnit LIKE '%" + SearchTextBox.Text + "%'";
adapter = new SqlDataAdapter(query, connect);
table = new DataTable();
adapter.Fill(table);
RetailInfoGridView.DataSource = table;
RetailInfoGridView.DataBind();
connect.Close();*/
}
protected void SearchButton_Click(object sender, EventArgs e)
{
using (SqlConnection connect = new SqlConnection(connectionString))
{
connect.Open();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM RetailInfo WHERE StockKeepingUnit LIKE '%" + SearchTextBox.Text + "%'", connect);
DataTable table = new DataTable();
adapter.Fill(table);
RetailInfoGridView.DataSource = table;
RetailInfoGridView.DataBind();
}
}
protected void RetailInfoGridView_DataBound(object sender, EventArgs e)
{
}
protected void RetailInfoGridView_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void RetailInfoGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onmouseover"] = "this.style.backgroundColor='aquamarine';";
e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='white';";
e.Row.ToolTip = "Click the link to view Description";
//e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(RetailInfoGridView, "Select$" + e.Row.RowIndex);
}
}
void PopulateGridView()
{
using (SqlConnection connect = new SqlConnection(connectionString))
{
connect.Open();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM RetailInfo", connect);
DataTable table = new DataTable();
adapter.Fill(table);
if (table.Rows.Count > 0)
{
RetailInfoGridView.DataSource = table;
RetailInfoGridView.DataBind();
}
else
{
table.Rows.Add(table.NewRow());
RetailInfoGridView.DataSource = table;
RetailInfoGridView.DataBind();
RetailInfoGridView.Rows[0].Cells.Clear();
RetailInfoGridView.Rows[0].Cells.Add(new TableCell());
RetailInfoGridView.Rows[0].Cells[0].ColumnSpan = table.Columns.Count;
RetailInfoGridView.Rows[0].Cells[0].Text = "No record Found";
}
}
}
protected void RetailInfoGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName.Equals("Add"))
{
using (SqlConnection connect = new SqlConnection(connectionString))
{
connect.Open();
string query = "INSERT INTO RetailInfo(StockKeepingUnit,UniversalProductCode,VendorName,ProductName,ProductDesc,RetailPrice) VALUES (@StockKeepingUnit,@UniversalProductCode,@VendorName,@ProductName,@ProductDesc,@RetailPrice)";
SqlCommand command = new SqlCommand(query, connect);
command.Parameters.AddWithValue("@StockKeepingUnit", (RetailInfoGridView.FooterRow.FindControl("skuTextBoxFooter") as TextBox).Text.Trim());
command.Parameters.AddWithValue("@UniversalProductCode", (RetailInfoGridView.FooterRow.FindControl("upcTextBoxFooter") as TextBox).Text.Trim());
command.Parameters.AddWithValue("@VendorName", (RetailInfoGridView.FooterRow.FindControl("vnTextBoxFooter") as TextBox).Text.Trim());
command.Parameters.AddWithValue("@ProductName", (RetailInfoGridView.FooterRow.FindControl("pnTextBoxFooter") as TextBox).Text.Trim());
command.Parameters.AddWithValue("@ProductDesc", (RetailInfoGridView.FooterRow.FindControl("pdTextBoxFooter") as TextBox).Text.Trim());
command.Parameters.AddWithValue("@RetailPrice", (RetailInfoGridView.FooterRow.FindControl("rpTextBoxFooter") as TextBox).Text.Trim());
command.ExecuteNonQuery();
PopulateGridView();
ScriptManager.RegisterStartupScript(this, typeof(string), "Alert", "alert('New Record Added');", true);
}
}
}
catch (Exception ex)
{
}
}
protected void RetailInfoGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
RetailInfoGridView.EditIndex = e.NewEditIndex;
PopulateGridView();
}
protected void RetailInfoGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
RetailInfoGridView.EditIndex = -1;
PopulateGridView();
}
protected void RetailInfoGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
using (SqlConnection connect = new SqlConnection(connectionString))
{
connect.Open();
string query = "UPDATE RetailInfo SET StockKeepingUnit=@StockKeepingUnit,UniversalProductCode=@UniversalProductCode,VendorName=@VendorName,ProductName=@ProductName,ProductDesc=@ProductDesc,RetailPrice=@RetailPrice WHERE StockKeepingID=@id";
SqlCommand command = new SqlCommand(query, connect);
command.Parameters.AddWithValue("@StockKeepingUnit", (RetailInfoGridView.Rows[e.RowIndex].FindControl("skuTextBox") as TextBox).Text.Trim());
command.Parameters.AddWithValue("@UniversalProductCode", (RetailInfoGridView.Rows[e.RowIndex].FindControl("upcTextBox") as TextBox).Text.Trim());
command.Parameters.AddWithValue("@VendorName", (RetailInfoGridView.Rows[e.RowIndex].FindControl("vnTextBox") as TextBox).Text.Trim());
command.Parameters.AddWithValue("@ProductName", (RetailInfoGridView.Rows[e.RowIndex].FindControl("pnTextBox") as TextBox).Text.Trim());
command.Parameters.AddWithValue("@ProductDesc", (RetailInfoGridView.Rows[e.RowIndex].FindControl("pdTextBox") as TextBox).Text.Trim());
command.Parameters.AddWithValue("@RetailPrice", (RetailInfoGridView.Rows[e.RowIndex].FindControl("rpTextBox") as TextBox).Text.Trim());
command.Parameters.AddWithValue("@id", Convert.ToInt32(RetailInfoGridView.DataKeys[e.RowIndex].Value.ToString()));
command.ExecuteNonQuery();
RetailInfoGridView.EditIndex = -1;
PopulateGridView();
ScriptManager.RegisterStartupScript(this, typeof(string), "Alert", "alert('Record Updated');", true);
}
}
catch (Exception ex)
{
}
}
protected void RetailInfoGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
using (SqlConnection connect = new SqlConnection(connectionString))
{
connect.Open();
string query = "DELETE FROM RetailInfo WHERE StockKeepingID = @id";
SqlCommand command = new SqlCommand(query, connect);
command.Parameters.AddWithValue("@id", Convert.ToInt32(RetailInfoGridView.DataKeys[e.RowIndex].Value.ToString()));
command.ExecuteNonQuery();
PopulateGridView();
ScriptManager.RegisterStartupScript(this, typeof(string), "Alert", "alert('Record Deleted');", true);
}
}
catch (Exception ex)
{
}
}
每當我運行程序時,我都會將值編輯到特定行,然后單擊保存按鈕,它不會執行任何操作。 我讀過其他答案,說代碼中有 !postback 和 DataKeyNames,但我已經在 Main.aspx.cs 和 Main.aspx 上有 DataKeyNames 的回發。 我不知道錯誤是在 .aspx 上還是在 .aspx.cs 上。 我在代碼上做錯了嗎? 任何幫助將不勝感激。 謝謝你。
更新:
我已經解決了這個問題。 而不是編寫“保存”變量:
<EditItemTemplate>
<asp:Button ID="Save" CommandName="Save" runat="server" Text="Save" ToolTip="Save" />
<asp:Button ID="Cancel" CommandName="Cancel" runat="server" Text="Cancel" ToolTip="Cancel" />
</EditItemTemplate>
我將其更改為“更新”:
<EditItemTemplate>
<asp:Button ID="Update" CommandName="Update" runat="server" Text="Update" ToolTip="Update" />
<asp:Button ID="Cancel" CommandName="Cancel" runat="server" Text="Cancel" ToolTip="Cancel" />
</EditItemTemplate>
雖然我不知道它為什么起作用,但它確實起作用了!
如果您知道為什么“保存”不起作用而“更新”起作用,如果您告訴我,我將不勝感激,這樣我才能有所啟發。
感謝所有幫助過的人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.