Solved

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

Posted on 2009-07-13
17
177 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

895 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

18 Experts available now in Live!

Get 1:1 Help Now