Showing posts with label void. Show all posts
Showing posts with label void. Show all posts

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.

Saturday, March 24, 2012

This really shouldn't have much code in it anyway

private void DataGrid1_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
/* don't put logic here. only lines to call your logic elsewhere */
}
You should not business logic in your controls. You should
not put business logic (no matter how small) in your event
handling sections or even form methods.
You'll be much better off enhancing your application if
your business rules are in classes that are not tied directly
to your UI or business layers outside of the parameters
passed in.
Robbe Morris - 2004/2005 Microsoft MVP C#
Earn money answering .NET Framework
messageboard posts at EggHeadCafe.com.
http://www.eggheadcafe.com/forums/merit.asp
<apandapion@dotnet.itags.org.gmail.com> wrote in message
news:1124130872.659013.20810@dotnet.itags.org.g47g2000cwa.googlegroups.com...
> I'm working with csharp and .net for the first time, and I've had a
> fair amount of luck. I started with the MSDN "Walkthrough : Creating a
> Distributed Application" tutorial and expanded from there, adding state
> management and other assorted things.
> So I have a lot of code that looks like this:
> private void DataGrid1_UpdateCommand(object source,
> System.Web.UI.WebControls.DataGridCommandEventArgs e)
> {
> /* snip lots of code that I've written */
> }
> It wraps a dataset obtained through a pretty standard two function
> GetTable/UpdateTable web service.
> I want to reuse this code, and not in the braindead
> cut/paste/alter/suffer manner. Under the .net architecture, what's the
> best way to go about it? Write my own class that inherits from
> DataGrid? I tried that, and it encapsulates the code nicely, but it
> also causes lots of errors from the aspx file that I'm not sure how to
> handle. Do I have to write a web custom control? Do I lose access to
> the designer in doing so?
>Morris, that's pretty much what I said. We agree on the theory. I'm
looking for a good way to reencapsulate the code, as in, "what language
construct should I use?" Inherit from DataGrid? Write a helper
object? Write my own macro preprocessor?
It's a little hard to encapsulate anything coming off of the XML soap
interface, as the objects are not objects but just a pair of get/put
function calls. And the ASPX side of my module seems to react very
strangely to me inheriting from DataGrid.
I had no trouble working out how to plug COM objects together, back in
the day, as life was just a matter of locating, implementing and
invoking interfaces. Dotnet is a lot easier to work with, so far, but
it's not abundantly clear how I'm supposed to both encapsulate my own
code and interact with the twisty maze of microsoft objects that I have
to talk to on all sides. I would like to think that dotnet is good for
more than toys, but I haven't seen real evidence of that so far.
Can you recommend some books, or technical articles, that demonstrate
the right way to do things? I don't want clouds with lines between
them... I want code. It would be nice to do things "the right way",
from the beginning, but right now I'm just a ser after the way.