Solved

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

Posted on 2009-07-13
17
180 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
[X]
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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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!
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

697 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