Ebikeneser
Ebikeneser

Reputation: 2364

Save all changes in DataList on button click

I have the following DataList-

<td>
<asp:Label ID="Label3" runat="server" Text="Exclude"></asp:Label>
<asp:DataList runat="server" ID="excludeTextBox">
<ItemTemplate>

<br />
<asp:TextBox ID="myTextBox" runat="server" Text='<%# Container.DataItem.ToString() %>'></asp:TextBox>&nbsp;


</ItemTemplate>
</asp:DataList>
<td>
<asp:Label ID="Label4" runat="server" Text="Active"></asp:Label>
<asp:DataList runat="server" ID="activeCheck" >

<ItemTemplate>

<br />
<asp:CheckBox ID="CheckBox1" runat="server"  Checked='<%# Container.DataItem.ToString().Equals("1") %>' OnCheckedChanged="CheckBox1_CheckedChanged" AutoPostBack="true" />


</ItemTemplate>
</asp:DataList>

</td>

So this is generating a textBox and checkBox for every row that exits in the database.

Currently there is no save action associated with the dataList, however if a user checks or unchecks one of the checkBoxes this calls a webservice and toggles the value in a data base table.

My question is, after a user has edited the text in some of the textBoxes and checked or unchecked some of the check boxes, how can I catch the changes in a save changes button. Because if the items have been dynamically created then technically they do not exist on the page. I want to do this without having a save option on ever row on the dataList.

protected void saveChanges_Click(object sender, EventArgs e)
{
     // capture all edits and call update webservice method.   
}

Upvotes: 1

Views: 1486

Answers (1)

Brian Mains
Brian Mains

Reputation: 50728

You'd essentially have to build the representation of the changes. Because there is no generic solution to this, I don't have any code, but here is the process:

As an item changes, you have to store what changed. You could store the indexes of the rows that actually changed in a hidden variable on the page, or store the changes in a JavaScript object. When save changes is clicked, on the client you then loop through these changes, and if they exist send them in bulk or one at a time.

If you had a hidden field with a value like 0,2,5 for each index that changed, you could easily find those items, grab the values from the form, and ship them off to the web service. Or, you could build a JavaScript object that has the changes like:

{ key: 2, checked: true, text:"My new text" }

Store these in an array, and push them up to the server through the web service.

Upvotes: 1

Related Questions