search button load gridview after c# asp.net 2.0
When i click the search button,load the Gridview.At that GridView,i can add,delete and update. When the user click add , the message box pop up "Customer added successfuly" Then i have to click " SearchButton" again.If not i cant see the GridView again. Cause i just wirte the code to load the Gridview by clicking the Searchbutton. How can I fix it? For the Very first time, I just have some dropdownlist and Search button only. Please help me. Thanks
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//Check if Add button clicked
if (e.CommandName == "AddProject")
{
try
{
//Get the values stored in the text boxes
string strProjectName = ((TextBox)GridView1.FooterRow.FindControl("txtPJDESCLONG")).Text;
string strLastUpdate = ((TextBox)GridView1.FooterRow.FindControl("txtLASTUPDATE")).Text;
string strStatus = ((TextBox)GridView1.FooterRow.FindControl("txtSTATUS")).Text;
string strUsername = ((TextBox)GridView1.FooterRow.FindControl("txtUSERNAME")).Text;
string strProjCode = ((TextBox)GridView1.FooterRow.FindControl("txtPJCODE")).Text;
//Prepare the Insert Command of the DataSource control
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(strConn);
string strSQL = "";
if (conn.State == ConnectionState.Closed) { conn.Open(); }
strSQL = "INSERT INTO CTORGPROJ (CTPAPBRCH,CTPAPDIV,CTPAPDEPT,CTPAPSECT,CTPAPSSEC,CTPAPPDIV,CTPAPLOC,CTPAPPDEP,PJCODE,PJDESCLONG,LASTUPDATE,STATUS, " +
"USERNAME) VALUES ('" + cboBranch.SelectedValue + "','" + cboDivision.SelectedValue + "','" + cboDepartment.SelectedValue + "','" + cboSection.SelectedValue + "','" + cboSubSection.SelectedValue + "','" + cboLocation.SelectedValue + "','" + cboPayDivision.SelectedValue + "','" + cboPayDepartment.SelectedValue + "','" + strProjCode + "','" + strProjectName + "','" +
strLastUpdate + "','" + strStatus + "','" + strUsername + "')";
Session.Add("conn", CookieUtil.GetTripleDESEncryptedCookieValue("sConn").Replace("-", ";"));
Session.Add("PjNo", CookieUtil.GetTripleDESEncryptedCookieValue("sPjCode"));
Session.Add("sComCode", CookieUtil.GetTripleDESEncryptedCookieValue("sComCode"));开发者_StackOverflow中文版
string _strConn = Session["conn"].ToString();
_strConn = _strConn.Replace("Provider=SQLOLEDB;", "");
SqlDataSource SqlDataSource1 = new SqlDataSource(_strConn, strSQL);
SqlDataSource1.InsertCommand = strSQL;
SqlDataSource1.Insert();
ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('Customer added successfully');</script>");
GridView1.DataBind();
conn.Close();
conn.Dispose();
}
catch (Exception ex)
{
ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('" + ex.Message.ToString().Replace("'", "") + "');</script>");
}
}
- Have a flag in a view-state to decide if grid view to be shown or not. Initial value will be false.
- Have a method say
ShowGrid
that will add (if needed) your grid, bind the data and make it visible. - On click of search, set the view-state flag true and call
ShowGrid
method. - In
Page_Load
event, check if the view-state flag is true (in post-back condition) and if true then callShowGrid
method.
精彩评论