ASP gridview: adding two values and validate - asp.net

I use GridView to show/edit data in the database. In the GridView there are two columns called mark1 and mark2. What I want to do is when user clicks update, the program should check if the values entered at mark1 and mark2 sum to 100. If they don't the program should cancel the update and show an error message. How do I do it? I use ASP.NET with VB.

take a look at custom validation control. if you provide some code, i can fix it for you if it doesn't work.

Related

asp.net webform Repeater, Update a set of textbox, Add to the List / Array

I am working on old webform asp.net application which uses Repeater.
The Repeater is a set of Name and address textbox fields. I have 3 sets of Name and Address information, basically, 3 sets of records in the object list to bind.
On the UI, I changed/update one of the Name and Address. I noticed in the list, which it iterate or loops through the repeater control
foreach (RepeaterItem item in this.NameAddressRepeaterControl.Items)
I see that an extra recod is added to the items.
Question:
I am used to fixed textboxes. When I update the textbox, I write code to take Exactly what is filled in the textbox and populate the DTO object to pass to the data layer to perform database transaction to update the database records.
When the new updated record is added to the Repeater Control list, I don't know which records is updated and which is the new records.
I am checking out OnItemDataBound and OnItemCommand to see if there is a way to get the old value from one of the field and also record the value of the new value. Is this possible?
The form contains 1 Save button and it will loop through the Repeater.Items to see what Name/Address to extract, but the new and old company exist in this list.
Instead of looping through the RepeaterControl.Items, Is there a way to extract from directly the visible Repeater control? If there are 3 repeater Name/Address control, is there a way to get all the info from each of the 3 sets of Repeater controls? The Repeater wraps around a user control, NameAddressCtrl.
I prefer not to replace the Repeater controls with fixed textboxes.
Any help is greatly appreciated.
Thanks.
I reviewed the code several time and add additional code to keep track what was add and changed in the repeated and only passed the information to be saved to the db. The solution was to write additional code. I did not change anything with the repeater behavior. Thanks for the comments and help.

ASP.NET controls disappearing from dynamically built user control

I have a curious problem with an ASP.NET user-control which is dynamically created in VB.NET. Sorry that this is a long post, but I think it’s better for me to explain carefully what I’m doing and what the problem is.
Essentially, the user-control is an HTML table used to display data from a back-end SQL Server DB. It uses a stored procedure to return a DataTable, from which it builds html rows and columns with the values stored in the InnerHtml property of the relevant table cell. This is just a passive display of the data and works perfectly.
However, the user-control also has the facility to insert a new record, triggered by an ASP button. This causes the following sequence of events;
Post-back
The user-control_Init event rebuilds all the existing rows in the table and inserts their values as described above.
The page_Init event.
The page_Load event.
The user-control_Load event.
The button_Click event then builds a new row with the same structure as the main table. But this time it inserts into each table cell a relevant ASP.NET control, such as a textbox or checkbox, holding “null” values. Each control has its ID set explicitly to a unique value. Finally, an “insert flag” is set to true.
The user then enters the data into these controls and clicks an Insert-Save button. This causes another post-back, which follows the procedure described above, except that the “insert flag” tells the user-control_Init event (2) to rebuild the insert row. Then, before the page_Load event (4) the values previously entered by the user are reinstated in the ASP.NET controls from ViewState. Finally, the Insert-Save_Click event calls a stored procedure to save these data to the DB.
All this work perfectly when there are existing rows in the html table. But – and this is the strange behaviour – it fails when there are no existing rows. The first post-back completes successfully although, obviously, event 2 skips over the rebuilding of the existing rows because there are none. However, the insert row is rebuilt correctly and the user can then enter data into the ASP.NET controls. During the second post-back (Insert-Save), the first three events described above work perfectly. But, the moment the code hits the page_Load event (4), all the ASP.NET controls disappear. The html row and cells are still present, including the literal control (index 0) in each cell. No error is thrown at this stage, but when the Insert-Save_Click event occurs and the data should be read from the ASP.NET controls – they are not available and an "index out-of-bounds" error is thrown, because the ASP.NET controls should each have an index of 1 within their cells.
I’ve spent hours (days!) debugging this and I cannot see what is causing these controls to just evaporate! Any clever programmers out there got any ideas please?

Is it possible to have a SQLDataSource with a parameter that is based only upon the GridView that is binding to it?

I have a scenario where I want to put four identical Gridviews on the same page. (They will be on different tabs in an Ajax TabControl.) They show the same source data, but there are four corresponding groups of source data in a common underlying table. So I want to show Group 1 on Tab 1, Group 2 on Tab 2, etc. These Gridviews contain complicated controls, so I would prefer to use the same data source for all of them to avoid unnecessary repetition. The Insert and Update commands are completely identical.
So in theory I could build the Select command in such a way that I could filter the data based on the GridView that is binding to the SQLDataSource. The problem is that if I use the same SQLDataSource for all the Gridviews, I cannot find a way to have each GridView tell the SQLDataSource which one is calling it. I am thinking maybe this is not possible, because the SQLDataSource binds first before it knows what is binding to it, but I'm not sure. Can anyone think of a way to do this?
You can change the parameter value dynamically using OnSelecting event of SQLDataSource. This can be done in server side code.
Create a property which holds your current gridview unique key, which is causing SQLDataSource to fetch data from SQL database.
Assign this property unique gridview key on DataBinding event of gridview.
Based on this property change the parameter in OnSelecting event of SQLDataSource.
Let me know if I am missing something.

ASP.net: GridView trouble getting updated values

I am using *GridView1_RowUpdating* event which is called after clicking "Update" link. In the method I handle updating of the GridView.
I want to get the values from textboxes (i.e. new values):
string title= ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
But strangely I get old value. I've debugged but couldn't find where the error is.
Also, e.NewValues.Count is 0.
The problem was I was doing GridView.DataBind() everytime in page_load. Solved now.
you need to bind data to the gridView control

Bind a Text Box to a Field Selected by SQLDataSource (VB.NET)

I'm looking for the easiest way to bind data from a SqlDataSource to textboxes dropped in Visual Studio 2008.
For example, I have 4 textboxes currently that have Address, City, State, Zip. I also have a SqlDataSource on the page fetching the ID of the record and selecting those 4 fields based on ID.
How am I able to quickly bind each box to those particular fields selected? I would think this would be really straight forward - but seems it's not. Seems like the answer is funneled towards having to create a GridView or some type of control.
Be gentle...I'm a nub :)
In general you are correct, if you want to use databinding you'll need to use an appropriate control. For this example I'd suggest using a FormView - it is designed to display the results from a single database record and uses templates, meaning you'll have complete control over the output. This article is probably a good place to start: FormView Control: Step by Step.
To read the values bound to the FormView in the code-behind class you would need to create an event handler for the FormView's DataBound event. In that event handler you would reference the controls programmatically via FindControl, like so:
Dim myLabel As Label = CType(FormViewID.FindControl("id"), Label)
Here, id would be the ID of the Label whose value you were interested in. Once you have a reference to the Label you can get its value using myLabel.Text.

Resources