[英]Call a function from another function in c#
我有一個按鈕單擊事件,它將注釋文本從文本框中插入到數據庫中。現在我需要調用單擊插入注釋按鈕的單擊事件后顯示注釋的函數。這是代碼。
使用此代碼,我將文本插入數據庫
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();
}
我需要在cmt_Click
按鈕單擊事件的最后一行中調用函數binddlcmt()
在函數內部只有一個參數的情況下,我不確定如何調用該函數。
因此,您想知道如何在按鈕單擊事件處理程序中獲取item-index嗎?
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.