Hyperlinks on a DataGrid

  • Thread starter Thread starter Ruy Castelli
  • Start date Start date
R

Ruy Castelli

Hello,

I'm trying to get the details out of a datagrid component and it works fine
when I'm accessing the first page of the datagrid, but when I try to click
on the details hyperlink for an item that is not on the first page, the
web-page displays the first page of the datagrid again and show the details
of the field on the same location, but on the first page.

For instance, if I have the first page with the following data:

Header:
DATE NAME MSG HYPERLINK

data
01/05 A B Details
02/05 C D Details

And on the second page I've got something like this:

Header:
DATE NAME MSG HYPERLINK

data
01/05 E F Details
02/05 G H Details

If I click on the details for E and F (first line of the second page), the
page returns to the first one and the details return as NAME=A and MSG=B.

Here is my Page_Load event, I tried to use the FillData only when the server
starts, but then when I have a postback the datagrid is empty.

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
try
{
oleDbConnection1.Open();

oleDbDataAdapter1.SelectCommand.CommandText =
"SELECT hour, name, msg FROM [msg log]";

oleDbDataAdapter1.Fill(dataset11);
DataGrid1.DataBind();
}
catch (System.Data.OleDb.OleDbException exception)
{
Error.Visible = true;
Error.Text = "Server error: " + exception.ToString();
}
finally
{
oleDbConnection1.Close();
}
}

Here is my PageIndexChanged event:

private void DataGrid1_PageIndexChanged(
object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
}

And here is my ItemCommand event:

private void DataGrid1_ItemCommand(
object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if (e.CommandName=="det")
{
Label5.Text = "Correct Command";

Label3.Text = e.Item.Cells[0].Text;
Label4.Text = e.Item.Cells[1].Text;
Label6.Text = e.Item.Cells[2].Text;
}
}

Please, if you have any idea, let me know.

Thanks.
Ruy.
 
Ruy, It looks to me like your page load event, which always runs, rebinds
the datagrid to the dataset and you will be setting on page one. Then your
Item Event runs but of course you are looking at the wrong page now. So,
only bind (and rebind) the dataset when you need to. In your case, bind the
data set the first time (!page.ispostback) and pageIndexChanged. Note, you
still need to load the dataset each time or add a load of the dataset in the
pageIndexChanged event.

....Chuck
 
Hi Chuck, thanks for answering.

I changed the events as you said, but now the datagrid just disappears when
I click on the page number or Next/Previous.

Here are the new events:

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!PostBack)
{
try
{
oleDbConnection1.Open();

oleDbDataAdapter1.SelectCommand.CommandText =
"SELECT hour, name, msg FROM [msg log]";

oleDbDataAdapter1.Fill(dataset11);
DataGrid1.DataBind();
}
catch (System.Data.OleDb.OleDbException exception)
{
Error.Visible = true;
Error.Text = "Server error: " + exception.ToString();
}
finally
{
oleDbConnection1.Close();
}
}
}

Here is my PageIndexChanged event:

private void DataGrid1_PageIndexChanged(
object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;

try
{
oleDbConnection1.Open();

oleDbDataAdapter1.SelectCommand.CommandText =
"SELECT hour, name, msg FROM [msg log]";

oleDbDataAdapter1.Fill(dataset11);
DataGrid1.DataBind();
}
catch (System.Data.OleDb.OleDbException exception)
{
Error.Visible = true;
Error.Text = "Server error: " + exception.ToString();
}
finally
{
oleDbConnection1.Close();
}

DataGrid1.DataBind();
}

Thanks.
Ruy.

Chuck Rudolph said:
Ruy, It looks to me like your page load event, which always runs, rebinds
the datagrid to the dataset and you will be setting on page one. Then your
Item Event runs but of course you are looking at the wrong page now. So,
only bind (and rebind) the dataset when you need to. In your case, bind
the data set the first time (!page.ispostback) and pageIndexChanged. Note,
you still need to load the dataset each time or add a load of the dataset
in the pageIndexChanged event.

...Chuck

Ruy Castelli said:
Hello,

I'm trying to get the details out of a datagrid component and it works
fine when I'm accessing the first page of the datagrid, but when I try to
click on the details hyperlink for an item that is not on the first page,
the web-page displays the first page of the datagrid again and show the
details of the field on the same location, but on the first page.

For instance, if I have the first page with the following data:

Header:
DATE NAME MSG HYPERLINK

data
01/05 A B Details
02/05 C D Details

And on the second page I've got something like this:

Header:
DATE NAME MSG HYPERLINK

data
01/05 E F Details
02/05 G H Details

If I click on the details for E and F (first line of the second page),
the page returns to the first one and the details return as NAME=A and
MSG=B.

Here is my Page_Load event, I tried to use the FillData only when the
server starts, but then when I have a postback the datagrid is empty.

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
try
{
oleDbConnection1.Open();

oleDbDataAdapter1.SelectCommand.CommandText =
"SELECT hour, name, msg FROM [msg log]";

oleDbDataAdapter1.Fill(dataset11);
DataGrid1.DataBind();
}
catch (System.Data.OleDb.OleDbException exception)
{
Error.Visible = true;
Error.Text = "Server error: " + exception.ToString();
}
finally
{
oleDbConnection1.Close();
}
}

Here is my PageIndexChanged event:

private void DataGrid1_PageIndexChanged(
object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
}

And here is my ItemCommand event:

private void DataGrid1_ItemCommand(
object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if (e.CommandName=="det")
{
Label5.Text = "Correct Command";

Label3.Text = e.Item.Cells[0].Text;
Label4.Text = e.Item.Cells[1].Text;
Label6.Text = e.Item.Cells[2].Text;
}
}

Please, if you have any idea, let me know.

Thanks.
Ruy.
 
Hello Chuck,

I followed your tips, but it still didn't work. Then I was checking some
configuration and I noticed that somehow the document properties
"enableSessionState" and "enableViewState" were set to false. When I set
then to true, everything worked.

Thanks.
Ruy.

Chuck Rudolph said:
Ruy, It looks to me like your page load event, which always runs, rebinds
the datagrid to the dataset and you will be setting on page one. Then your
Item Event runs but of course you are looking at the wrong page now. So,
only bind (and rebind) the dataset when you need to. In your case, bind
the data set the first time (!page.ispostback) and pageIndexChanged. Note,
you still need to load the dataset each time or add a load of the dataset
in the pageIndexChanged event.

...Chuck

Ruy Castelli said:
Hello,

I'm trying to get the details out of a datagrid component and it works
fine when I'm accessing the first page of the datagrid, but when I try to
click on the details hyperlink for an item that is not on the first page,
the web-page displays the first page of the datagrid again and show the
details of the field on the same location, but on the first page.

For instance, if I have the first page with the following data:

Header:
DATE NAME MSG HYPERLINK

data
01/05 A B Details
02/05 C D Details

And on the second page I've got something like this:

Header:
DATE NAME MSG HYPERLINK

data
01/05 E F Details
02/05 G H Details

If I click on the details for E and F (first line of the second page),
the page returns to the first one and the details return as NAME=A and
MSG=B.

Here is my Page_Load event, I tried to use the FillData only when the
server starts, but then when I have a postback the datagrid is empty.

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
try
{
oleDbConnection1.Open();

oleDbDataAdapter1.SelectCommand.CommandText =
"SELECT hour, name, msg FROM [msg log]";

oleDbDataAdapter1.Fill(dataset11);
DataGrid1.DataBind();
}
catch (System.Data.OleDb.OleDbException exception)
{
Error.Visible = true;
Error.Text = "Server error: " + exception.ToString();
}
finally
{
oleDbConnection1.Close();
}
}

Here is my PageIndexChanged event:

private void DataGrid1_PageIndexChanged(
object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
}

And here is my ItemCommand event:

private void DataGrid1_ItemCommand(
object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if (e.CommandName=="det")
{
Label5.Text = "Correct Command";

Label3.Text = e.Item.Cells[0].Text;
Label4.Text = e.Item.Cells[1].Text;
Label6.Text = e.Item.Cells[2].Text;
}
}

Please, if you have any idea, let me know.

Thanks.
Ruy.
 
Back
Top