RowEditing、RowUpdatingなどのGridviewイベントが追加されます。 GridViewはUserControl内にあります。 UserControlは、aspxのPlaceholder-Controlに動的に読み込まれます。すべて正常ですが、RowEditingイベントは発生しますが、Rowupdatingイベントは発生しません。 RowEditingイベントはページを読み込み、ページはUsercontrolを再度ロードします。グリッドビューは編集モードですが、更新ボタンをクリックするとGridView RowUpdatingイベントは発生しません。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>
コード:
保護のボイドをPage_Load(オブジェクト送信者、EventArgsの電子) {
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();
}
}