Saturday, March 31, 2012

There is no row at ...

I have this code so far:

private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
Panel2.Visible = false;
Panel1.Visible = true;
Label1.Text = "Logged in as: " + Session["VisitorsName"];
sqlConnection1.Open();
sqlFilesIDAdapter.SelectCommand.Parameters["@dotnet.itags.org.ID"].Value =
Session["VisitorsID"];
sqlFilesIDAdapter.Fill(filesIDData1,"Files");
DataGrid1.DataBind();
}
}

private void DataGrid1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
Panel1.Visible = false;
Panel2.Visible = true;
Label2.Text =
filesIDData1.Tables["Files"].Rows[DataGrid1.SelectedItem.ItemIndex]["TP_
Name"].ToString();
}

Now when I click on a row I get: 'There is no row at ' and then the
number. If I remove the !IsPostBack condition it works. Why is this?

--
Kind regards,

Stijn Verrept.Because the data is loaded the first time into the grid, but the second time
you haven't loaded the data therefore the grid is empty when you change the
index. You could store the object in the Viewstate and then recover it on
further loads, though this can make for a rather large page as the Viewstate
data tends to bloat a page pretty well.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage

"Stijn Verrept" <stijn@.no_s.p.a.n.entrysoft.com> wrote in message
news:400dc686$0$323$ba620e4c@.news.skynet.be...
> I have this code so far:
> private void Page_Load(object sender, System.EventArgs e)
> {
> if (!IsPostBack)
> {
> Panel2.Visible = false;
> Panel1.Visible = true;
> Label1.Text = "Logged in as: " + Session["VisitorsName"];
> sqlConnection1.Open();
> sqlFilesIDAdapter.SelectCommand.Parameters["@.ID"].Value =
> Session["VisitorsID"];
> sqlFilesIDAdapter.Fill(filesIDData1,"Files");
> DataGrid1.DataBind();
> }
> }
> private void DataGrid1_SelectedIndexChanged(object sender,
> System.EventArgs e)
> {
> Panel1.Visible = false;
> Panel2.Visible = true;
> Label2.Text =
> filesIDData1.Tables["Files"].Rows[DataGrid1.SelectedItem.ItemIndex]["TP_
> Name"].ToString();
> }
>
> Now when I click on a row I get: 'There is no row at ' and then the
> number. If I remove the !IsPostBack condition it works. Why is this?
> --
> Kind regards,
> Stijn Verrept.
Mark Fitzpatrick wrote:

> Because the data is loaded the first time into the grid, but the
> second time you haven't loaded the data therefore the grid is empty
> when you change the index. You could store the object in the
> Viewstate and then recover it on further loads, though this can make
> for a rather large page as the Viewstate data tends to bloat a page
> pretty well.

Thanks Mark for your answer, I understand now. But what I don't get is
why I need to reload the data if all I do is show a field in a label?
Is it possible to do this client side?

--
Kind regards,

Stijn Verrept.
I agree, I don't know that I would recommend the viewstate because it can get pretty large, depending on how much data you're talking about

One of the things I've done is if the page is not a postback then load the grid. On the SelectedIndexChanged event on the grid I do all the manipulation and whatever I need to do, then I do a response.redirect to that same page I'm already on. This works pretty well (I think) if you're programmatically doing something with the data and leaving the user out of any data manipulation. However, if you need the user to manipulate the data this won't work so well (I don't think)

HTH
Jeremy
Stijn

The reason you need to reload the data is because when a postback happens you loose all of data. I know it seems bizarre, but that's how the web works. Every HTTP request to a file (or page) acts like a brand new request, that's why in your pages you'll need to put in the appropriate checks to ensure the page is a PostBack or not

I'm not sure about working with the grid on the client though. But I hope this answers your question about why you need to reload the data

Jeremy
Stijn Verrept wrote:

> Mark Fitzpatrick wrote:
>
>>Because the data is loaded the first time into the grid, but the
>>second time you haven't loaded the data therefore the grid is empty
>>when you change the index. You could store the object in the
>>Viewstate and then recover it on further loads, though this can make
>>for a rather large page as the Viewstate data tends to bloat a page
>>pretty well.
>
> Thanks Mark for your answer, I understand now. But what I don't get is
> why I need to reload the data if all I do is show a field in a label?
> Is it possible to do this client side?

If you are looking up a value in the same table you bound to, just
create some invisible/hidden columns with the other column values you
need later on.

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET
Craig Deelsnyder wrote:

> If you are looking up a value in the same table you bound to, just
> create some invisible/hidden columns with the other column values you
> need later on.

Aha! Thanks :)

I changed it to Label2.Text = DataGrid1.SelectedItem.Cells[1].Text; and
this works great. I also replaced the Dataset by a DataReader which
seems to speed it up some more :)

--
Kind regards,

Stijn Verrept.

0 comments:

Post a Comment