Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2009-07-13
17
179 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
ID: 24840901
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
ID: 24840939
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
ID: 24841070
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 21

Expert Comment

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

Author Comment

by:lobos
ID: 24852404
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
ID: 24852484
txtLocation.DataBindings.Add("Text", ds.Tables[0], "Location");
0
 
LVL 22

Expert Comment

by:p_davis
ID: 24852503
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
ID: 24852712
your way is ok...no difference.
0
 
LVL 1

Expert Comment

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

Expert Comment

by:p_davis
ID: 24852820
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
ID: 24852847
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
ID: 24852866
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
ID: 24852892
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
ID: 24852895
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
ID: 24853748
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
ID: 24853950
agreed
0
 

Author Closing Comment

by:lobos
ID: 31602882
thanks for all feedback.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Best resource to learn entity framework 2 47
Graphics 2 32
What Does This C# Code Block Do? 5 56
Changes to XXX.exe.config file are not retrieved on restart. 3 39
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 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