如何在asp.net中调用另一个函数内的函数

时间:2014-08-11 15:01:53

标签: c# asp.net

我有一个按钮点击事件,它将数据插入数据库。我需要调用此click事件中的一个函数,以便在单击按钮后显示数据库中的数据。

这是按钮点击事件。

protected void cmt_Click(object sender, EventArgs e)
{

    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 = "";
    show_comment(); 
}

我想在点击事件结束时调用此函数。

 protected void mydatalist_ItemDataBound(object sender, DataListItemEventArgs e)
 {
if (e.Item.ItemType == ListItemType.Item)
{
    DataList dl = e.Item.FindControl("dl_cmt") as DataList;

    string str = gstr;
    sq.connection();
    SqlCommand cmd = new SqlCommand("select top 4 * from comment where sid='" + str + "' order by my_date desc", sq.con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);

    dl.DataSource = ds;
    dl.DataBind();
}
}

1 个答案:

答案 0 :(得分:2)

我认为首先应该在Code Behind文件中定义一个静态属性:

//处理绑定

protected void mydatalist_ItemDataBound(object sender, DataListItemEventArgs e)
 {
if (e.Item.ItemType == ListItemType.Item)
{
    Session["dlGlobal"] = e.Item.FindControl("dl_cmt") as DataList;
}
}

//在静态变量中拥有数据列表时,处理事件处理程序之外的函数:

public void PerformAction()
{
if(Session["dlGlobal"]!=null)
 {
DataList dlGlobal=Session["dlGlobal"] as DataList;
       string str = gstr;
    sq.connection();
    SqlCommand cmd = new SqlCommand("select top 4 * from comment where sid='" + str + "' order by my_date desc", sq.con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);

    dlGlobal.DataSource = ds;
    dlGlobal.DataBind();
}
}

//然后在事件处理程序中调用上面的函数,单击按钮

protected void cmt_Click(object sender, EventArgs e)
{

    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 = "";
    show_comment(); 
PerformAction();// Call the new function !
}