我有一个按钮点击事件,它将注释文本从文本框插入到数据库中。现在我需要调用一个在插入注释按钮的点击事件后显示注释的函数。这是代码。
使用此代码我将文本插入数据库
protected void cmt_Click(object sender, EventArgs e)
{
//DateTime.Now.ToString();
Button btn = (Button)sender;
DataListItem dli = (DataListItem)btn.NamingContainer;
TextBox tx = (TextBox)dli.FindControl("tb_cmt");
Label lb = (Label)dli.FindControl("lbl_sid");
string userid = Session["userid"].ToString();
sq.connection();
SqlCommand cmd = new SqlCommand("insert into comment(ecomment,sid,my_date,reg_id) values(@myecomment,@mysid,@mydate,@reg_id)", sq.con);
cmd.Parameters.AddWithValue("@myecomment", tx.Text);
cmd.Parameters.AddWithValue("@mysid", lb.Text);
cmd.Parameters.AddWithValue("@mydate", DateTime.Now.ToString("h:mm, MMM dd, yyyy"));
cmd.Parameters.AddWithValue("@reg_id", userid);
cmd.ExecuteNonQuery();
sq.con.Dispose();
sq.con.Close();
tx.Text = "";
//bind_dlcmt( 1 );
}
现在这里是显示数据库评论的代码。
public void showdata()
{
string str = gstr;
sq.connection();
SqlCommand cmd = new SqlCommand("select * from sub_catTbl where sid='" + str + "' ", sq.con);
//SqlCommand cmd = new SqlCommand("select * from sub_catTbl", sq.con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
mydatalist.DataSource = dt;
mydatalist.DataBind();
sq.con.Dispose();
sq.con.Close();
foreach (DataListItem dli in mydatalist.Items)
{
bind_dlcmt(dli.ItemIndex);
//blin_dlcmt();
}
}
public void bind_dlcmt(int itm_indx)
{
string str = gstr;
//int sid = Convert.ToInt32(mydatalist.DataKeys[itm_indx]);
sq.connection();
SqlCommand cmd = new SqlCommand("select top 4 * from comment where sid='" + str + "' order by my_date desc", sq.con);
//SqlCommand cmd = new SqlCommand("select * from comment where sid=@sid ", sq.con);
//cmd.Parameters.AddWithValue("@sid", sid);
SqlDataReader sdr = cmd.ExecuteReader();
DataList dl = (DataList)mydatalist.Items[itm_indx].FindControl("dl_cmt");
dl.DataSource = sdr;
dl.DataBind();
}
我需要在binddlcmt()
按钮单击事件的最后一行调用函数cmt_Click
。如果函数内有一个参数,我不知道如何调用该函数。
答案 0 :(得分:0)
所以你想知道如何在按钮点击事件处理程序中获取item-index?
protected void cmt_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
DataListItem dli = (DataListItem)btn.NamingContainer;
// ...
bind_dlcmt( dli.ItemIndex );
}