简体   繁体   English

从C#中的另一个函数调用一个函数

[英]Call a function from another function in c#

I have a button click event which insert comment text from textbox to database.Now i need to call a function that displays comment after the click event of insert comment button is clicked.Here's the code. 我有一个按钮单击事件,它将注释文本从文本框中插入到数据库中。现在我需要调用单击插入注释按钮的单击事件后显示注释的函数。这是代码。

With this code I insert text to database 使用此代码,我将文本插入数据库

 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 );

}

Now here's the code to display comment from database. 现在,这里是显示数据库注释的代码。

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

I need to call the function binddlcmt() in the last line of the cmt_Click button click event.With the one argument inside the function, i am not sure how do i call the function. 我需要在cmt_Click按钮单击事件的最后一行中调用函数binddlcmt()在函数内部只有一个参数的情况下,我不确定如何调用该函数。

So you want to know how to get the item-index in the button-click event handler? 因此,您想知道如何在按钮单击事件处理程序中获取item-index吗?

Use DataListItem.ItemIndex : 使用DataListItem.ItemIndex

protected void cmt_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    DataListItem dli = (DataListItem)btn.NamingContainer;
    // ...
    bind_dlcmt( dli.ItemIndex );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM