Solved

C# a new feature would be nice, is it there?

Posted on 2009-07-13
17
176 Views
Last Modified: 2012-05-07
I have a web based C# .net 2008 form that is used for updating records.
I have about 13 fields and the user can update any record, so there is an 'update' button as well as a 'add new' button and a 'reset'.
So if the user clicks on 'add new' I would like to make sure they have not update any information on the fields, because if they dont hit submit, all the changes would be lost. So I was hoping to do a check (is there some built in feature), that I can check to see if any values were changed, if so, prompt them to save because the code for 'Add New' is run.
0
Comment
Question by:lobos
  • 7
  • 6
  • 2
  • +2
17 Comments
 
LVL 22

Accepted Solution

by:
p_davis earned 25 total points
Comment Utility
do you use datatables/datasets to bind to these fields? if so then it would be fairly easy.

there are addnew rejectchanges and acceptchanges methods on both. you can check datarowstates to see if something has been added or modified.
0
 
LVL 21

Expert Comment

by:silemone
Comment Utility
No, there is no way to do this from a built in control...

This is how I did this:

1) On page load, i created a ViewState object for each control...and loaded the value of it...   ViewState.Add("ControlAVal", ConrtrolA.Value)

2)  Then I can compare it to that...if (! ViewState["ControlAVal"].ToString().Equals(ControlA.Value)){ //means change occured...add logic }
0
 
LVL 21

Assisted Solution

by:silemone
silemone earned 25 total points
Comment Utility
oops...


page load:

when you load control value from db if that's where you getting them from you would also load viewstate value

//loaded control value  and use control
Viewstate.Add("SomeControlValue", SomeControl.Text) ; //say if its a textbox

then later when finding controls which have been changed (this may be for the purpose of creating a list to past as a parameter for a sproc, or just in page)

if (!ViewState["SomeControlVal"].ToString().Equals(SomeControl.Text) )
{
         //add to string builder, comma delimited string for parameter list or do build a SQL statement  in code, or whatever
         //at the end of logic do this:
        ViewState.Add("SomeControlVal", SomeControl.Text);         //Value updated so that next go around, it will evaluate new val...
}
0
 
LVL 21

Expert Comment

by:silemone
Comment Utility
oops = i should have given an example...so there you have one...
0
 

Author Comment

by:lobos
Comment Utility
ok thanks for the comments...this leads to this question...how is what is the best recommended way to load the values to the text fields....I have always done it this way below....is this the best way or is there another more efficient, microsoft recommended way based on your comments I'm not sure which way to go...
      this.txtLocation.Text = ds.Tables[0].Rows[0]["Location"].ToString();
      this.txtWebsite.Text = ds.Tables[0].Rows[0]["Website"].ToString();
      this.txtName.Text = ds.Tables[0].Rows[0]["Name"].ToString();
      this.txtEmail.Text = ds.Tables[0].Rows[0]["Email"].ToString();

Please advise, thanks.
0
 
LVL 22

Expert Comment

by:p_davis
Comment Utility
txtLocation.DataBindings.Add("Text", ds.Tables[0], "Location");
0
 
LVL 22

Expert Comment

by:p_davis
Comment Utility
this.txtWebsite.DataBindings.Add("Text ",  ds.Tables[0], "Website");
      this.txtName.DataBindings.Add("Text ", ds.Tables[0],"Name");
      this.txtEmail.DataBindings.Add("Text ", ds.Tables[0],"Email");
0
 
LVL 21

Expert Comment

by:silemone
Comment Utility
your way is ok...no difference.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 1

Expert Comment

by:chicabow
Comment Utility
what advantages are there of doing the way p_davis has displayed?
0
 
LVL 22

Expert Comment

by:p_davis
Comment Utility
umm except with binding the updates to the record are handled already. also record states and if htere is more than one row -- say attached to a grid  whatever current record you are view would be updated in the text fields --


i would say that there is a big difference.
0
 
LVL 21

Expert Comment

by:silemone
Comment Utility
chica, there aren't really any...if there was a lot of data, databinding may be quicker because the coding behind it is by microsoft themselves (which supposedly makes it more refined than our for loops), but as i stated also, there is no need for lobos to change his code.
0
 
LVL 6

Expert Comment

by:ahmad2121
Comment Utility
to answer the original question, it can be even simpler than that. Have one bool, set it to false on load, for every textbox, use the textchanged event to set it to true. In your button, if that bool is true, ask them to save.
0
 
LVL 21

Expert Comment

by:silemone
Comment Utility
that's what i was doing with with my code ID:24841070...forgot to set a flag element to false...good catch ahmad
0
 
LVL 22

Expert Comment

by:p_davis
Comment Utility
how do you know what lobos's setup is - it might be very beneficial for this to be implemented with databindings. i even use databinding with generic lists -- rowstates have to be handled on your side but it is still a nice way to handle row changes.
0
 
LVL 21

Expert Comment

by:silemone
Comment Utility
i think we're just saying binding one piece of data is overkill...its agreed if he was populating a huge table, binding would be quicker on the coding and functioning side...
0
 
LVL 22

Expert Comment

by:p_davis
Comment Utility
agreed
0
 

Author Closing Comment

by:lobos
Comment Utility
thanks for all feedback.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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 video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now