如何在Repeater控件中,执行textbox的ontextchanged事件?
页面中,textbox的代码:<asp:TextBoxID="P_Quantity"runat="server"CssClass="quantity_txt"Text='...
页面中,textbox的代码:
<asp:TextBox ID="P_Quantity" runat="server" CssClass="quantity_txt" Text='<%# Eval("Quantity") %>' ontextchanged="P_Q_TextChanged" ToolTip='<%# Eval("SCId") %>' AutoPostBack="true" ></asp:TextBox>
后台,ontextchanged事件代码:
protected void P_Q_TextChanged(object sender, EventArgs e)
{
TextBox textBox = (TextBox)sender;
RepeaterItem repeaterItem = (RepeaterItem)textBox.Parent;
TextBox txtId = (TextBox)repeaterItem.FindControl("P_Quantity");
string str = "Data Source=.;Initial Catalog=EB_SHOP_DB;User ID=sa;Password=123456;";
SqlConnection conn = new SqlConnection(str);
try
{
conn.Open();
str = "update ShoppingCart set Quantity='" + txtId.Text + "',TotalPrice=" + txtId.Text + "*Price where SCId='" + txtId.ToolTip + "'";
SqlCommand cmd = new SqlCommand(str, conn);
cmd.ExecuteNonQuery();
cmd.Dispose();
ShowProductList();
}
catch
{
this.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('修改购买数量失败')</script>");
}
finally
{
conn.Close();
conn.Dispose();
}
}
现在的问题是,我在textbox内输入数据后,ontextchanged事件里的代码并不执行。而且,页面中,textbox的值又会变成我输入之前的数据。请问高手怎么解决。 展开
<asp:TextBox ID="P_Quantity" runat="server" CssClass="quantity_txt" Text='<%# Eval("Quantity") %>' ontextchanged="P_Q_TextChanged" ToolTip='<%# Eval("SCId") %>' AutoPostBack="true" ></asp:TextBox>
后台,ontextchanged事件代码:
protected void P_Q_TextChanged(object sender, EventArgs e)
{
TextBox textBox = (TextBox)sender;
RepeaterItem repeaterItem = (RepeaterItem)textBox.Parent;
TextBox txtId = (TextBox)repeaterItem.FindControl("P_Quantity");
string str = "Data Source=.;Initial Catalog=EB_SHOP_DB;User ID=sa;Password=123456;";
SqlConnection conn = new SqlConnection(str);
try
{
conn.Open();
str = "update ShoppingCart set Quantity='" + txtId.Text + "',TotalPrice=" + txtId.Text + "*Price where SCId='" + txtId.ToolTip + "'";
SqlCommand cmd = new SqlCommand(str, conn);
cmd.ExecuteNonQuery();
cmd.Dispose();
ShowProductList();
}
catch
{
this.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('修改购买数量失败')</script>");
}
finally
{
conn.Close();
conn.Dispose();
}
}
现在的问题是,我在textbox内输入数据后,ontextchanged事件里的代码并不执行。而且,页面中,textbox的值又会变成我输入之前的数据。请问高手怎么解决。 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询