0

I have a button click event which insert data to database.I need to call a function inside this click event to display the data from database after the button is clicked.

This is the button click event.

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

And i want to call this function at the end of the click event.

 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 Answer 1

2

I think first you should define a static property in your Code Behind file :

//handle the Binding

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

//Handle the function outside of the event handler as you have the datalist in the static variable :

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

//then call the above function in your Event Handler for Button 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(); 
PerformAction();// Call the new function !
}
Sign up to request clarification or add additional context in comments.

10 Comments

You are on the right path by refactoring out the desired logic and calling it from where it is needed but you don't want to use a shared property for the DataList. Shared will get shared across all users so there will be unexpected results if two different users use that at the same time. Figure out the DataList instance at runtime and supply as an argument to the method.
string is not recognized in the PerformAction() function .why?
sorry see edit , i forgot to add a Bracket after the IF statement
@wizpert the PerformAction() function is called from the click event but it dosen't enter inside the if condition.So problem still exists.
@wizpert i am sorry but i am still unable to go inside the if condition..please help
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.