Becky Bloomwood

Becky Bloomwood

  • NA
  • 119
  • 284.4k

Click button outside grid view and display the grid view upon load?

Feb 9 2011 8:02 AM

Hi, I am doing a online web application whereby user is able to enter in new template as well as search for template. The Add template is located outside the grid view and if user clicks on the button, it will load the grid view that is populated with data as there is a row of empty fields at the bottom. However when I click on the Add Template button, the grid view does not appear.
This is my business logic that I used it to search for records as well to click on the Add Template button to add in new records:
 

using
System;
using
System.Collections.Generic;
using
System.Data;
using
System.Data.SqlTypes;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
com.vrm.database;
using
com.vrm.com;
namespace
StarTrack
{
public partial class ContractTemplate : System.Web.UI.Page
{
private vrm_database vrmdb = new vrm_database();
private String viewStateGVName = "gvTemplate";


//set the index for the columns of the gridview in the aspx that required formatting of text
private const int GVCHKBOX = 0;
private const int GVEDITBTN = 1;
private const int GVVENDORBRN = 2;
private const int GVMATERIALGRP = 4;
private const int GVINSAP = 8;
private const int GVSTARHUBCONTACTPERSON = 9;
private const int GVDELETEFLAG = 16;
private const int GVBLOCKED = 17;
//set the const for the ID of the GV Checkbox column
private const string GVCHECKBOXCOLID = "selected";

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Set the sortExpression
ViewState[
this.ToString() + "_SortExpression"] = "TemplateName";
ViewState[
this.ToString() + "_SortDirection"] = "ASC";



}
}
protected void btnClear_Click(object sender, EventArgs e)
{
gvTemplate.Visible =
false;
tbSearchTID.Text =
"";
tbSearchTName.Text =
"";

}
protected void btnSearch_Click(object sender, EventArgs e)
{
BindGrid(
true);
gvTemplate.Visible =
true;
}
protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk;
foreach (GridViewRow rowItem in gvTemplate.Rows)
{
chk = (
CheckBox)(rowItem.Cells[0].FindControl("selected"));
chk.Checked = ((
CheckBox)sender).Checked;
}
}
private void lblerror(string p)
{
throw new NotImplementedException();
}
//Bind the GridView to with the Database returned records
private void BindGrid(bool Reload)
{

DataTable dtTemplateRecords = null;

if (Reload)
//get from database and bind to GV
{
dtTemplateRecords = vrmdb.Get_TemplateRecords(tbSearchTID.Text, tbSearchTName.Text).Tables[0];
ViewState[viewStateGVName] = dtTemplateRecords;
}
else
{
//vrmdb.Get_TemplateRecords(tbSearchTName.Text)
//retrive the view state object data table from previous retrival
dtTemplateRecords = ViewState[viewStateGVName]
as DataTable;
}
if (dtTemplateRecords != null)
{
gvTemplate.Columns[GVCHKBOX].Visible =
true;
gvTemplate.DataSource = ViewState[viewStateGVName];
gvTemplate.AllowSorting =
true;
gvTemplate.DataBind();
}
else
{
dtTemplateRecords.Rows.Add(dtTemplateRecords.NewRow());
ViewState[viewStateGVName] = dtTemplateRecords;
gvTemplate.AllowSorting =
false;
gvTemplate.DataSource = ViewState[viewStateGVName];
gvTemplate.DataBind();
//hide the checkbox and edit columns
gvTemplate.Columns[GVCHKBOX].Visible =
false;
gvTemplate.Columns[GVEDITBTN].Visible =
false;

int TotalColumns = gvTemplate.Rows[0].Cells.Count;
gvTemplate.Rows[0].Cells.Clear();
gvTemplate.Rows[0].Cells.Add(
new TableCell());
gvTemplate.Rows[0].Cells[0].ColumnSpan = TotalColumns;
gvTemplate.Rows[0].Cells[0].Text =
"No Record Found";
}


}
protected void gvTemplate_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
GridView header = (GridView)sender;
GridViewRow gvr = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
TableCell tCell = new TableCell();
tCell.Text =
"Contract Management Templates";
tCell.ColumnSpan = 9;
tCell.HorizontalAlign =
HorizontalAlign.Left;
tCell.CssClass =
"trWithBorder";
gvr.Cells.Add(tCell);
// Add the Merged TableCell to the GridView Header
Table tbl = gvTemplate.Controls[0] as Table;
if (tbl != null)
{
tbl.Rows.AddAt(0, gvr);
}
}

}
protected void gvTemplate_Sorting(object sender, GridViewSortEventArgs e)
{
GetSortDirection(e.SortExpression);
BindGrid(
false);
}
private void GetSortDirection(string sColumn)
{
//set sort direction to asc
string sSortDirection = "ASC";
string sSortExpression = ViewState[this.ToString() +
"_SortExpression"] as string;
if (sSortExpression != null)
{
//check same column is being sorted
if (sSortExpression == sColumn)
{
string sLastDirection = ViewState[this.ToString() +
"_SortDirection"] as string;
if ((sLastDirection != null) && (sLastDirection == "ASC"))
{
sSortDirection =
"DESC";
}
}
}
//save new values in view
ViewState[
this.ToString() + "_SortDirection"] = sSortDirection;
ViewState[
this.ToString() + "_SortExpression"] = sColumn;
}


protected void gvTemplate_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AddNew"))
{
TextBox txtTemplateID = (TextBox)gvTemplate.FooterRow.FindControl("txtTemplateID");
TextBox txtTemplateName = (TextBox)gvTemplate.FooterRow.FindControl("txtTemplateName");
TextBox txtFileName = (TextBox)gvTemplate.FooterRow.FindControl("txtFileName");
TextBox txtFilePath = (TextBox)gvTemplate.FooterRow.FindControl("txtFilePath");

Console.WriteLine(txtTemplateID.Text);
Console.WriteLine(txtTemplateName.Text);
Console.WriteLine(txtFileName.Text);
Console.WriteLine(txtFilePath.Text);



vrmdb.Insert_TemplateRecords(txtTemplateID.Text, txtTemplateName.Text, txtFileName.Text, txtFilePath.Text);

BindGrid(
true);
Response.Redirect(
"ContractTemplate.aspx");
}
}

protected void gvTemplate_RowEditing(object sender, GridViewEditEventArgs e)
{
gvTemplate.EditIndex = e.NewEditIndex;
BindGrid(
true);
}
protected void gvTemplate_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtTemplateID = (TextBox)gvTemplate.Rows[e.RowIndex].FindControl("txtTemplateID");
TextBox txtFilePath = (TextBox)gvTemplate.Rows[e.RowIndex].FindControl("txtFilePath");



vrmdb.Update_TemplateRecords(txtTemplateID.Text,txtFilePath.Text);
gvTemplate.EditIndex = -1;
BindGrid(
true);
}
protected void gvTemplate_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvTemplate.EditIndex = -1;
BindGrid(
true);
}
protected void gvTemplate_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
vrmdb.Delete_TemplateRecords(gvTemplate.DataKeys[e.RowIndex].Values[0].ToString());
BindGrid(
true);
}
protected void AddBtn_Click(object sender, EventArgs e)
{
DataTable dtTemplateRecords = null;
if (dtTemplateRecords != null)
{
if (dtTemplateRecords.Rows.Count > 0)
{
ViewState[viewStateGVName] = dtTemplateRecords;
gvTemplate.DataSource = ViewState[viewStateGVName];
gvTemplate.DataBind();
}

}



}
}
}

This is the code that I use it for the Add Template btn such that when click on it will display the grid view:  

 

protected void AddBtn_Click(object sender, EventArgs e)   
    {   

       DataTable dtTemplateRecords = null;   

           if (dtTemplateRecords != null)   

            {   
                if (dtTemplateRecords.Rows.Count > 0)   
                {   

                    ViewState[viewStateGVName] = dtTemplateRecords;   

                  gvTemplate.DataSource = ViewState[viewStateGVName];   

                    gvTemplate.DataBind();   

                }   
                 

           }   

Thanks for helping me all these while:)

 

Answers (3)