In this tutorial I'm going to go over some simple common ways to use ASP gridview controls and the RowCommand to trigger an events and then to navigate to a detail form view.
Let get into it:
A simple way to navigate to a detailed view is to set up a gridview with data from your database:

If you don't know how to setup a gridview go here.
Create a event by double clicking your gridview, you should get an event like:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
This will be triggered whenever the user clicks your gridview, we can then use some code to redirect the user to a detail view:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string RecordId = GridView1.SelectedRow.Cells[1].Text;
Session["RecordID"] = RecordId;
Response.Redirect("DetailPage.aspx");
}
Let's go line by line:
string RecordId = GridView1.SelectedRow.Cells[1].Text;
This bit of code will select the recordID which is in column 1 (remember were 0 based) I have chosen to leave this visible but you can hide the column in the markup by setting the column property visible = false
Session["RecordID"] = RecordId;
This will store our recordID into the Session Object so we can pick it up on the next page
Response.Redirect("CaseView.aspx");
This will redirect us to our our detail view page
So now were going to write some code in the pageload event of our detailpage.aspx page, this code will be executed when the page load's giving us the desired result.
protected void Page_Load(object sender, EventArgs e)
{
string RecordID2 = (string)(Session["RecordID"]);
int RecID = System.Convert.ToInt32(RecordID2);
DataClassesDataContext db = new DataClassesDataContext();
var resultset = from r in db.Records
where r.RecordID == RecID
select r;
string RecordID2 = (string)(Session["RecordID"]);
int RecID = System.Convert.ToInt32(RecordID2);
Here we are picking up the RecordID that we stored into the Session Object converting it into an integer
DataClassesDataContext db = new DataClassesDataContext();
var resultset = from r in db.Records
where r.RecordID == RecID
select r;
Next we pass it into a LINQ statement to get the specific row of data.
*You could just as easily do this with ADO/SQL if you choose.
foreach (var r in resultset)
{
Label3.Text = r.First + " " + r.Last;
TextBox1.Text = r.First;
TextBox16.Text = r.Last;
TextBox2.Text = r.Address;
TextBox3.Text = r.City;
etc .................................
}
}
Here I am populating the fields on the form to give us something like

Note: This is a very simple example and there are often better/more secure ways to do this but this is good place to start if your trying to learn ASP ...
Now lets look at how to use the RowCommand Event. This is a way to initiate a more complex event from your gridview.
Visual:

ASP Markup
<asp:GridView ID="GridView1" runat="server" OnRowCommand="myGV_RowCommand" AutoGenerateColumns="False"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:ButtonField HeaderText="Download"
Text="Download"
ButtonType="Button"
CommandName="click1" />
<asp:ButtonField ButtonType="Button"
HeaderText="Answer"
Text="Answer"
CommandName="click2" /> [/b]
<asp:BoundField DataField="Doc_Txt" HeaderText="Type"
SortExpression="Doc_Txt" />
ect ...
What am I looking at ?
Ok take it all in:
1. Our GridView is called, well GridView1
2. Our OnRowCommand is myGV_RowCommand
3. The CommandName forour buttons is Click1 and Click 2 Respectively
Next we have to write some code in our cs file to handle these two would be events:
protected void myGV_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "click1")
{
int index = Convert.ToInt32(e.CommandArgument); //get row number selected
GridViewRow row = GridView1.Rows[index];
Go ahead do something like above
}
if (e.CommandName == "click2")
{
Do something cool ...
}
}
Good link to check out:
GridView.RowCommand Event
This post has been edited by d_rop4nme: 04 August 2010 - 03:02 PM





MultiQuote





|