添加了RowEditing,RowUpdating等Gridview事件。 GridView位于UserControl中。 UserControl将在aspx上的Placeholder-Control中动态加载。一切都很好,RowEditing事件提升但Rowupdating事件不会触发。 RowEditing事件加载页面,页面加载Usercontrol,现在gridview处于编辑模式,但单击更新按钮时不会触发GridView RowUpdating事件。
示例代码:
<asp:TemplateField HeaderText="Product ID">
<ItemTemplate>
<%# Eval("pid") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtpid" runat="Server" Text='<%# Eval("pid") %>' Columns="30"></asp:TextBox>
<asp:RequiredFieldValidator ID="req1" runat="Server" Text="*" ControlToValidate="txtpid"></asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Produt Name">
<ItemTemplate>
<%# Eval("pname") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtpname" runat="Server" TextMode="MultiLine" Rows="10" Columns="50"
Text='<%# Eval("pname") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="req2" runat="Server" Text="*" ControlToValidate="txtpname"></asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<%# Eval("price") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtprice" runat="Server" TextMode="MultiLine" Rows="10" Columns="50"
Text='<%# Eval("price") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" VerticalAlign="Top" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:Label ID="lblMessage" runat="Server" ForeColor="Red"></asp:Label>
代码背后的代码:
protected void Page_Load(object sender,EventArgs e) {
Page pageHolder = new Page();
UserControl userControl = (UserControl)pageHolder.LoadControl("~/WebUserControl2.ascx");
userControl.Controls.Add(GridView1);
holder2.Controls.Add(userControl);
if (!IsPostBack)
{
BindData();
}
}
/// <summary>
/// Bind data to the grid
/// </summary>
private void BindData()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlDataAdapter dAd = new SqlDataAdapter("select * from product", conn);
DataSet dSet = new DataSet();
try
{
dAd.Fill(dSet,"product");
GridView1.DataSource = dSet.Tables["product"].DefaultView;
GridView1.DataBind();
}
catch (Exception ee)
{
lblMessage.Text = ee.Message.ToString();
}
finally
{
dSet.Dispose();
dAd.Dispose();
conn.Close();
conn.Dispose();
}
}
/// <summary>
/// fires when edit link is clicked
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void EditRecord(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData();
}
/// <summary>
/// fires when cancel link is clicked
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void CancelRecord(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindData();
}
/// <summary>
/// fires when update link is clicked
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void UpdateRecord(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
//int autoid = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
TextBox tPageName = (TextBox)row.FindControl("txtpid");
TextBox tPageDesc = (TextBox)row.FindControl("txtpname");
TextBox dActive = (TextBox)row.FindControl("txtprice");
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
SqlCommand dCmd = new SqlCommand();
try
{
conn.Open();
dCmd.CommandText = "spUpdateData";
//dCmd.CommandType = CommandType.StoredProcedure;
// dCmd.Parameters.Add("@AutoID", SqlDbType.Int).Value = autoid;
dCmd.CommandText = @"insert into [product](pid,pname,price)values(@pid,@pname,@price)";
dCmd.Parameters.Add("@pid", SqlDbType.Char, 10).Value = tPageName.Text.Trim();
dCmd.Parameters.Add("@pname", SqlDbType.VarChar, 50).Value = tPageDesc.Text.Trim();
dCmd.Parameters.Add("@price", SqlDbType.VarChar,50).Value = dActive.Text.Trim();
dCmd.Connection = conn;
dCmd.ExecuteNonQuery();
lblMessage.Text = "Record Updated successfully.";
// Refresh the data
GridView1.EditIndex = -1;
BindData();
}
catch (SqlException ee)
{
lblMessage.Text = ee.Message;
}
finally
{
dCmd.Dispose();
conn.Close();
conn.Dispose();
}
}
答案 0 :(得分:1)
我在Gridview中看不到您的更新按钮。
好吧无论如何,只需确保在更新按钮上有 CommandName =“更新”,以便 RowUpdating 事件正确触发。