[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Gridview issues on postback

Posted on 2007-08-05
18
Medium Priority
?
735 Views
Last Modified: 2010-05-18
ASP.NET 2.0 C#

I have a gridview. The gridview contians data for a questions within a dynamic questionnaire builder. The columns are as follows:
Field Title (string)
Field Type (At present either "Text Field" or "Select List")
Graded (True or False)
Options (Basically the select command of the grid)
Edit
Delete

I do not want the option link to appear if the Field Type = Text Field and so on the methos called by the rowDataBound event i added the following code:
protected void populateGridRows(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    //If the field in question is a text field then it should never be graded so dont show the text box
                    if (e.Row.Cells[1].Text == "Text Field")
                    {
                        e.Row.Cells[2].Text = " ";
                        e.Row.Cells[3].Text = " ";
                    }
                }
            }

Now this all works fine when the grid binds initially and the option links only appear where i want them to. However as soon as i do anything on the page which causes a post back, for instance clicking one of the visible option links, the option link appears on all rows of the grid. This doesnt happen when i choose one of the edit links in the grid (i assume because it is rebound). Clicking an option link calles the selected index change for the grid but the grid itself idnt rebound so i cant figure out what is causing it to re-render.

Hope this makes sense and i hope someone can help.
0
Comment
Question by:MontySR
  • 7
  • 6
  • 2
  • +1
16 Comments
 
LVL 26

Expert Comment

by:Rejojohny
ID: 19636278
you could also rebind the grid in the selected index changed event ..

you could also try making the controls visible property to false instead of updating the text to " " .. what might be happening is that the grid's viewstate is not getting updated by ur change in the rowdatabound event ..
0
 
LVL 1

Author Comment

by:MontySR
ID: 19636334
Thanks how would i access the select (called option in my case) link. I tried e.Row.Cells[3].Controls[0].Visible = false; but it throws and ArgumentOutOfRangeException its like there isnt a controls collection for that cell.

On the note of rebinding, i would prefer not to do that as there are a number of events on the page which cause the same problem. Would be good to understand why its happening.

Thanks again for your help
0
 
LVL 14

Expert Comment

by:chinu1310
ID: 19636375
Hi MontySR,

    When you postback a page there is a datasoruce attached with your gridview is also no longer available.
Why this dose not happen when you click on the edit link of gridview is becuase that will not loose the datasource.

     But ofcourse when you post back a page it will loose the data source. The solutions to this problem is either to maintain a dataview or when you fetch the data for the first time from database store that entire dataset into session and retrive it on postback from the session.

Hope it helps.
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 1

Author Comment

by:MontySR
ID: 19636381
Thanks chinu1310,

I should also explain that i am not using any of the new .net datasource controls and i am programatically binding the grid to a BLL object, which happens to be an entity list.

If the page postback causes the data source to be lost then how does it render the data into the grid as it doesnt appear to be rebinding? Is it a cache?

Thanks for taking the time to look at my problem
0
 
LVL 14

Expert Comment

by:chinu1310
ID: 19636430
Ya true. I came across this question again. I was trying to sort the gridview and instead of my filtered data all the data was coming up. Don't know how. :(

But as you said you are programatically creating data source it means you fire query find result set and store in dataset and than bind dataset to gridview. Right ?

But when you click on any other button on page basically you are not running that query part again and thats what looses the data. So better to store them in session and retrive on postback.
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 19638536
Rowdatabound is called only when a grid is bound to datasource .. so if you do not want to bind the grid again, try manipulating the grid using the rowcreated event .. maybe that will work .. for finding the controls in the cell, try the "FindControl" method ..

For the graded field, as this value is fetched from the database, ur query could also be manipulated so that the value is empty when the fieldtype= text field

Rejo
0
 
LVL 1

Author Comment

by:MontySR
ID: 19641372
Hi chinu1310

But as you said you are programatically creating data source it means you fire query find result set and store in dataset and than bind dataset to gridview. Right ? ---- No dataset a custom entity list

But when you click on any other button on page basically you are not running that query part again and thats what looses the data. So better to store them in session and retrive on postback. ----- Its not the data thats lost grid is filled up fine its just the option link gets added back in where it shouldnt be

Thanks again
0
 
LVL 1

Author Comment

by:MontySR
ID: 19641444
Hi Rejojohny

The odd thing is is that it doesnt appear to be rebinding the datagrid i put breaks in both the databind and rowdatabound methods and it never reaches them on the postback. So i cant figure out why the options link suddenyl appears where it did not before.

for finding the controls in the cell, try the "FindControl" method ..  ---- Not sure what the ID will be as its not a template field

I really hadnt thought about the graded field as well and you are right this re-appears as well as the options link.

Thanks again for taking the time to look at this




0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 19641605
The rebinding would not happen because you are not binding the grid to the datasource and that is the reason why the events won't fire ..
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 19922737
rebinding must have solved his problem ..
0
 
LVL 1

Author Comment

by:MontySR
ID: 19923763
Sorry but none of the solutions offered would work in my specific situation.
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 19924802
so what worked and what issue did you have with rebinding the grid? its good to respond back if you still had a problem and we could have looked at it at that time ..

Rejo
0
 
LVL 1

Author Comment

by:MontySR
ID: 19924850
Sorry but i moved on in the project and took a different approach, i should however have thanks you guys for all the effort you put in and closed it of so sorry about that. Rejojohny however is right a rebind on the grid would have fixed the problem but would have conflicted with something else i was doing on the page.
0
 
LVL 1

Author Comment

by:MontySR
ID: 19924856
To be honest the comment about the grid rebinding working is a bit of a " i think it would have worked" i would need to go back and delve into it again to be sure.
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 19953270
Bob, if no comments is to be accepted, I would atleast recomment that this question be not deleted because my comments are not invalid .. i would recomment PAQ ..

MontySR, if you feel otherwise, you are welcome to comment ..

Rejo
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 19995194
PAQed with no points refunded (of 500)

Computer101
EE Admin
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month19 days, 12 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question