5
Answers

Click on the ContractID(hyperlink) after search and view records tied to it by click on it

Becky Bloomwood

Becky Bloomwood

14y
11.7k
1

Hi, currently I am doing a web application such that after user search for all the contracts,the contract id is a hyperlink in which user can click on it to view the results tied to it. There is 2 pages. One is for the search page and after searching results is populated below. The contract id in the grid view is a hyperlink so that when user clicks on it, they will go to another page and view the specific records tied to it. How shld I do this as I am creating this web application in 3-tier formatting.
This is the search.aspx:
 

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 ContractManagement : System.Web.UI.Page
{
private vrm_database vrmdb = new vrm_database();
private String viewStateGVName = "gvContract";
private const int GVContractID = 4;//columns to reference
//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)
{
gvContract.Visible =
false;
tbSearchCID.Text =
"";
tbSearchCOwner.Text =
"";
tbSearchCSD.Text =
"";
tbSearchCED.Text =
"";
tbSearchVBRN.Text =
"";
tbSearchVName.Text =
"";
tbSearchVProd.Text =
"";
tbSearchRFPRef.Text =
"";
tbSearchRFPProjTitle.Text =
"";
SDL.Text =
"";

}
protected void btnSearch_Click(object sender, EventArgs e)
{
BindGrid(
true);
gvContract.Visible =
true;
}
protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk;
foreach (GridViewRow rowItem in gvContract.Rows)
{
chk = (
CheckBox)(rowItem.Cells[0].FindControl("selected"));
chk.Checked = ((
CheckBox)sender).Checked;
}
}
//Bind the GridView to with the Database returned records

private void BindGrid(bool Reload)
{
DataTable dtContract = null;
if (Reload)
//get from database and bind to GV
{
string startDate1 = "=";
if (SDL.SelectedValue == "Select One" || tbSearchCSD == null)
{
startDate1 =
"=";
}
else
{
startDate1 = SDL.SelectedValue;
}
SqlDateTime startDate = SqlDateTime.Null;
SqlDateTime endDate = SqlDateTime.Null;
dtContract = vrmdb.Get_ContractRecords(tbSearchCID.Text, tbSearchCOwner.Text, startDate, endDate, tbSearchVBRN.Text, tbSearchVName.Text, tbSearchVProd.Text, tbSearchRFPRef.Text, tbSearchRFPProjTitle.Text, SDL.Text).Tables[0];
ViewState[viewStateGVName] = dtContract;
}
else
{
//vrmdb.Get_TemplateRecords(tbSearchTName.Text)
//retrive the view state object data table from previous retrival
dtContract = ViewState[viewStateGVName]
as DataTable;
}
if (dtContract != null)
{
gvContract.Columns[GVCHKBOX].Visible =
true;
gvContract.DataSource = ViewState[viewStateGVName];
gvContract.AllowSorting =
true;
gvContract.DataBind();
}
else
{
dtContract.Rows.Add(dtContract.NewRow());
ViewState[viewStateGVName] = dtContract;
gvContract.AllowSorting =
false;
gvContract.DataSource = ViewState[viewStateGVName];
gvContract.DataBind();
//hide the checkbox and edit columns
gvContract.Columns[GVCHKBOX].Visible =
false;
gvContract.Columns[GVEDITBTN].Visible =
false;

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


}





protected void gvContract_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";
tCell.ColumnSpan = 14;
tCell.HorizontalAlign =
HorizontalAlign.Left;
tCell.CssClass =
"trWithBorder";
gvr.Cells.Add(tCell);
// Add the Merged TableCell to the GridView Header
Table tbl = gvContract.Controls[0] as Table;
if (tbl != null)
{
tbl.Rows.AddAt(0, gvr);
}
}

}
protected void gvContract_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;
}










}
}

This is the next page that will display the results tied to this particular ID:
 

using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
com.vrm.database;
using
System.Data;
using
com.vrm.com;
using
System.Collections.Specialized;
using
System.Text;
namespace
StarTrack
{
public partial class ContractThreeGridView : System.Web.UI.Page
{
private vrm_database vrmdb = new vrm_database();
private String viewStateGVName = "gvCriticalTerms";
private const int GVContractID = 10;//columns to reference

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Set the sortExpression
ViewState[
this.ToString() + "_SortExpression1"] = "terms";
ViewState[
this.ToString() + "_SortDirection1"] = "ASC";
//Set the sortExpression
ViewState[
this.ToString() + "_SortExpression2"] = "filename";
ViewState[
this.ToString() + "_SortDirection2"] = "ASC";
//populate the criticalterms datatable
DataTable tmpdt = vrmdb.Get_CriticalTerms().Tables[0];
//tmpdt.PrimaryKey = new DataColumn[] { tmpdt.Columns[0] };
ViewState[viewStateGVName] = tmpdt;


BindGrid();

}
}
private void BindGrid()
{
DataTable dtgvCriticalTerms = null;

//retrieve the ViewState object datatable
dtgvCriticalTerms = ViewState[viewStateGVName]
as DataTable;
dtgvCriticalTerms.DefaultView.Sort = ViewState[
this.ToString() +
"_SortExpression1"].ToString() + " " +
ViewState[
this.ToString() + "_SortDirection1"].ToString();

if (dtgvCriticalTerms != null)
{
if (dtgvCriticalTerms.Rows.Count > 0)
{
ViewState[viewStateGVName] = dtgvCriticalTerms;
gvCriticalTerms.DataSource = ViewState[viewStateGVName];
gvCriticalTerms.DataBind();
}
else
{
dtgvCriticalTerms.Rows.Add(dtgvCriticalTerms.NewRow());
ViewState[viewStateGVName] = dtgvCriticalTerms;
gvCriticalTerms.DataSource = ViewState[viewStateGVName];
gvCriticalTerms.DataBind();
int TotalColumns = gvCriticalTerms.Rows[0].Cells.Count;
gvCriticalTerms.Rows[0].Cells.Clear();
gvCriticalTerms.Rows[0].Cells.Add(
new TableCell());
gvCriticalTerms.Rows[0].Cells[0].ColumnSpan = TotalColumns;
gvCriticalTerms.Rows[0].Cells[0].Text =
"No Record Found";
}
}

}


protected void gvCriticalTerms_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvCriticalTerms.EditIndex = -1;
BindGrid();
}
protected void gvCriticalTerms_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AddNew"))
{
TextBox txtTermsID = (TextBox)gvCriticalTerms.FooterRow.FindControl("txtTermsID");
TextBox txtTerms = (TextBox)gvCriticalTerms.FooterRow.FindControl("txtTerms");
TextBox txtDefaultValue = (TextBox)gvCriticalTerms.FooterRow.FindControl("txtDefaultValue");
TextBox txtActualValue = (TextBox)gvCriticalTerms.FooterRow.FindControl("txtActualValue");
TextBox txtReasonForDeviation = (TextBox)gvCriticalTerms.FooterRow.FindControl("txtReasonForDeviation");
TextBox txtAlert = (TextBox)gvCriticalTerms.FooterRow.FindControl("txtAlert");
TextBox txtContractID = (TextBox)gvCriticalTerms.FooterRow.FindControl("txtContractID");


vrmdb.Insert_CriticalTermsRecords(txtTermsID.Text,txtTerms.Text, txtDefaultValue.Text, txtActualValue.Text, txtReasonForDeviation.Text, txtAlert.Text, txtContractID.Text);
BindGrid();
Response.Redirect(
"ContractThreeGridView.aspx");

}
}




protected void gvCriticalTerms_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
vrmdb.Delete_CriticalTermsRecords(gvCriticalTerms.DataKeys[e.RowIndex].Values[0].ToString());
BindGrid();
}
protected void gvCriticalTerms_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtTermsID = (TextBox)gvCriticalTerms.Rows[e.RowIndex].FindControl("txtTermsID");
TextBox txtTerms = (TextBox)gvCriticalTerms.Rows[e.RowIndex].FindControl("txtTerms");
TextBox txtDefaultValue = (TextBox)gvCriticalTerms.Rows[e.RowIndex].FindControl("txtDefaultValue");
TextBox txtActualValue = (TextBox)gvCriticalTerms.Rows[e.RowIndex].FindControl("txtActualValue");
TextBox txtReasonForDeviation = (TextBox)gvCriticalTerms.Rows[e.RowIndex].FindControl("txtReasonForDeviation");
TextBox txtAlerts = (TextBox)gvCriticalTerms.Rows[e.RowIndex].FindControl("txtAlerts");
//Console.WriteLine(txtTerms.Text);
//Console.WriteLine(txtDefaultValue.Text);
//Console.WriteLine(txtActualValue.Text);
//Console.WriteLine(txtReasonForDeviation.Text);
//Console.WriteLine(txtAlerts.Text);
vrmdb.Update_CriticalTermsRecords(txtTermsID.Text,txtTerms.Text, txtDefaultValue.Text, txtActualValue.Text, txtReasonForDeviation.Text, txtAlerts.Text);
gvCriticalTerms.EditIndex = -1;
BindGrid();
Response.Redirect(
"ContractThreeGridView.aspx");

}
protected void gvCriticalTerms_RowEditing(object sender, GridViewEditEventArgs e)
{
gvCriticalTerms.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void gvCriticalTerms_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvCriticalTerms.PageIndex = e.NewPageIndex;
BindGrid();
}
protected void gvCriticalTerms_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 =
"Critical Terms";
tCell.ColumnSpan = 10;
tCell.HorizontalAlign =
HorizontalAlign.Left;
tCell.CssClass =
"trWithBorder";
gvr.Cells.Add(tCell);


// Add the Merged TableCell to the GridView Header
Table tbl = gvCriticalTerms.Controls[0] as Table;
if (tbl != null)
{
tbl.Rows.AddAt(0, gvr);
}
}

}
protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk;
foreach (GridViewRow rowItem in gvCriticalTerms.Rows)
{
chk = (
CheckBox)(rowItem.Cells[0].FindControl("selected"));
chk.Checked = ((
CheckBox)sender).Checked;
}
}

}
}
Thanks for ur help all these while;)
Answers (5)
0
Amit Choudhary

Amit Choudhary

NA 27.7k 3m 14y
you can create a helper or manager class to separate your business logic from presentation. Write all methods here which are used to interact with data e.g., creating connection firing query or Update operations.
you can create your function let say public DataTable GetDetails(string ContratId) in the helper class. Create a connection to you db and fire a query to get the details of bases on the ContractId and fill the data in DataSet or Datatable what ever you like. and return it.
Now at your presentation call the function and bind the grid with the returned Datatable.
'
Thanks.

Please mark "Do you like this post" if it helps you.
0
Becky Bloomwood

Becky Bloomwood

NA 119 240.1k 14y
Okay but how do we declare it in the second page. And also, how do we declare the business logic for ir? Sorry i do not know how to do it cause I am still new to c#. Thanks!
0
Amit Choudhary

Amit Choudhary

NA 27.7k 3m 14y
Hi friend,
just don't bind it with contract ID make it little more custom...
you can use Hyperlink in that column and can create the Text='<%#Eval("ContractID")%>' NavigateLink='~\Search2.aspx?ContractId=<%#Eval("ContractID")%>'
and you will have the  dynamic url different for each row.

now on the second page you can use Request.QueryString["ContractId"] to fetch the value from URL in page_load event.
and then from your Business logic call a function to load the data based on this ContractID.

Hope this make sense.

Please mark "Do you like this post" if it helps you.

Thanks.
0
Becky Bloomwood

Becky Bloomwood

NA 119 240.1k 14y
Thanks. One of the column in the grid view is being converted to template and I bind it to the ContractID in the database.Is this the way to do it? cause the contract id is being shown. how do we make use of params in the url from the second page and bind to grid. Oh ya i am using 3-tier programming to do it out. Thanks!
0
Amit Choudhary

Amit Choudhary

NA 27.7k 3m 14y
Hi friend,

you don't have post all your code every time you ask a question just be specific and give the specific region of the code. so that the helper can understand it clearly and quickly.
What i understood with your problem is: You want a link in your first grid and then clicking on that link wanted to show the related data for that particular row in the second page.
Here is the workaround for it:
1. Create a custom column in either you datasource or in gridview itself.
For datasource you can simply add a new column iterate through all rows and fill the values in the column e.g., a link to the second page and the parameters in the url you want on the second page e.g, Contract ID
For gridview approach you have to do the same bt in the Row_Created or Row_Bound event of gridview.

2. Use the params from the url in the second page. and fire the query or whatever source you have to get the data on the basis of the params. and bind your grid.

Hope you got idea from this.

Thanks.