开发者

Text change in TextBoxes

I have some 32 TextBoxes and 1 "Save" button. What I want is to save the text for only those TextBoxes which have their Texts changed. How can I achie开发者_如何学编程ve that? How do I know the text of which TextBoxes have changed?


Personally, I would do it server-side. Assuming you have no data binding involved, you could do something like the following in your ASPX.CS code-behind:

...

private string InitialValue1 
{ 
    get { return ViewState[@"IV1"] as string; } 
    set { ViewState[@"IV1"] = value; }
}
// Repeat for all 32 text boxes.

protected void Page_Load( object sender, EventArgs e )
{
    if(!IsPostBack )
    {
        TextBox1.Text = InitialValue1 = loadText1FromDatabase();
        // Repeat for all 32 text boxes.
    }
}

protected void MySaveButton_Click( object sender, EventArgs e )
{
    if ( TextBox1.Text!=InitialValue1 ) saveText1ToDatabase( TextBox1.Text );
    // Repeat for all 32 text boxes.
}

...

Of course, in a real-world-scenario, I would do some looping/array handling instead of writing 32 same functions/properties.


I would recommend using a jQuery function on load to make a fake old data input. Something like this:

$(document).ready(function (){
$(":text").each(
var newid=$(this).attr("id") + "_old";
var oldvalue=$(this).val();
var formy=$("form");
formy.appendChild("<input type='hidden' name='" + newid +"' value='" + oldvalue +"'>);
})

This way, if you change the number, the function automatically writes the correct number for you. Then on the server side you can compare the values with something like below :

var inputs = Request.Form.Keys.Where(rs=>rs.Contains("_old"));
foreach (var input in inputs)
{
  //check the _old vs the input without the old and do your thing
}


You can detect this on the client side for keypress, or compare the values on post and send over a list of items that changed. However if you want to do this on the server side, you will have to load up your server data and manually compare it against every textbox.


Use session variables to store the data for every postback and compare the values. If the value of a certain textbox changes then save that otherwise dont save.


You can do using HiddenFields. on page load you can set HiddenFields with original TextBox values. on save button you can compare HiddenFields value with TextBox value, If HiddenField value does not match with TextBox value then you can save that TextBox value in DB otherwise not.


Use cookie variables to store the data for every postback and compare the values.


Which version are you using, In case of 3.5 and above i would request you to check on this IPostBackDataHandler. This should help you in achieving what you want.

In previous versions i think you will have to do it only by comparing it with the original text or the least you could use the MERGE keyword (you will have to modify your db queries) while you are pushing your records into the database.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜