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>
</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.
}
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.
Related
I am supporting a web application. In that, there are two tables - TaxCode and TaxRate. TaxCode has 1 to many relationship with TaxRate.
The UI has a ListView with LayoutTemplate, ItemTemplate and EditTemplate to show TaxCode. When the users selects a tax code in EditTemplate it shows a CutomGridView that allows the user to create or edit tax rates for that particular tax code. This CustomGridView has 3 rows each has 4 template fields as shown below.
<asp:TemplateField HeaderStyle-CssClass="highlightTitlebar" HeaderStyle-Width="5%" HeaderStyle-Height="30px">
<HeaderTemplate>
<custom:CustomImageButton ID="imgAdd" runat="server" ImageUrl="image/add_round.gif" OnClick="imgAddTaxRateDetail_Click" CausesValidation="False"/>
</HeaderTemplate>
<ItemTemplate>
<custom:CustomImageButton ID="imgEdit" runat="server" ImageUrl="image/edit.gif" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ID="imgUpdate" runat="server" ImageUrl="image/update.gif" CommandName="Update" />
<asp:HiddenField runat="server" ID="hfId" Value='<%# Bind("Id") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="imgInsert" runat="server" ImageUrl="image/insert.gif" CommandName="Insert" OnClick="imgInsert_Click" />
</FooterTemplate>
Each row in the CustomGridView is a template field. In, below image EffectiveOn section is CustomGridView,
When I try to save TaxRate with proper EffectiveOn and Rate, it throws "Insert can only be called on an insert item. Ensure only the InsertTemplate has a button with CommandName=Insert." error as the ListView doesn't have InsertTemplate. But the record gets inserted into the DB.
Please let me know if there is any way to resolve this issue?
The error is self explanatory.
Take a look at this: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.insertitemtemplate(v=vs.110).aspx
So you can do either of these things.
Create an InsertItemplate and insert using the ItemInserted event of the listview
Change the CommandName to CommandName="InsertData" and catch that event on the ItemCommand
I have created a bulk edit gridview using Itemtemplate for each column. Label for non-edit mode, Textbox for edit mode. But this works only if the structure of gridview is known(Defining templates in asp).
<asp:TemplateField HeaderText="Name" ConvertEmptyStringToNull="True">
<ItemTemplate>
<asp:Label ID="lblName" Visible='<%# !(bool) IsInEditMode %>' runat="server" Text='<%# Eval("name") %>' />
<asp:TextBox ID="txtName" ControlStyle-CssClass="wide" Visible='<%# IsInEditMode %>'
runat="server" Text='<%# Eval("name") %>' />
</ItemTemplate>
</asp:TemplateField>
Now, what I am trying to achieve is when the user selects a dropdown value, the sql query is fired and it returns result. And this result is shown in gridview. But the problem arises because the number of columns in the result for each selected value from dropdown may vary. And i want to make the gridview as editable(or read-only). This requires two templates to be defined for each column.
So i want to know how this can be done dynamically i.e defining templates depending on number of columns returned by sql.
First off, you could trigger the query and following code with this event.
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//insert code
}
For the issue of different templates, I might suggest using two GridViews. I've done this where I have two separate sets of results. If they choose a dropdownItem, it hides GridView1 and shows GridView2 (while querying GridView2.SqlDataSource).
Now are you querying different SQL Tables based on dropdown? Or are they querying from the same place?
I am using ASP.NET and C# couple text boxes to calculate results entered. Textbox2 has a value entered and on button click Textbox1 gets populated. This works fine the first time I enter the value. But the second time I change the value in Textbox2, I see that the value is being assigned to Textbox1 while debugging on Button click, but doesn't show up on the screen. I have many other controls and a master page. The textboxes are within an update panel.
Can someone help me what's going on?
Here is the code:
<asp:TextBox ID="Textbox1" runat="server" Width="150px" TabIndex="6" MaxLength="8"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="TextBox1" ErrorMessage="*"></asp:RequiredFieldValidator>
<asp:TextBox runat="server" ID="Textbox2" Width="150px" Visible="false"></asp:TextBox> 
<asp:ImageButton runat="server" ID="imgCalcAdjRate" ImageUrl="~/Resource/Images/calrate_0.bmp" onmouseout="this.src='../Resource/Images/calrate_0.bmp'" onmouseover="this.src='../Resource/Images/calrate_1.bmp'" Height="25px" Width="25px" Visible="false" onclick="imgCalcAdjRate_Click" />
Check the postback in the page_load function.
if(!Page.IsPostBack) {
}
I think you are loosing information on post back. The data is changed, but it get reset on post back.
I am using the following technique to dynamically update a textbox -
Code behind
Lat.DataSource = ws.returnLAT(place);
Lat.DataBind();
aspx code
<asp:DataList runat="server" ID="Lat">
<ItemTemplate>
<td>
<asp:Label for="latBox" ID="Label5" runat="server" Text="Latitude"></asp:Label>
<br />
<asp:TextBox ID="latBox" runat="server" Text='<%# Container.DataItem.ToString() %>'></asp:TextBox>
</td>
</ItemTemplate>
</asp:DataList>
One debugging, DataSource is equal to 99, however when presented in the web page, that value is split into two separate text boxes each with a single 9 in them instead of the desired single textbox with 99 in it.
How can I resolve this?
I think what's happening here is it's (for whatever reason) treating 99 as 2 separate records. You shouldn't really be passing a single value as a DataSource it should be some form of collection.
For example, if you changed your binding code to:
Lat.DataSource = new List<int>(1) { ws.returnLat(place) };
Does the problem go away?
I have the following textBox -
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true"></asp:TextBox>
<asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"
MinimumPrefixLength="1" ServiceMethod="PRETURN" ServicePath="WebService1.asmx"
TargetControlID="TextBox1"> </asp:AutoCompleteExtender>
When a user types into TextBox1 this sends a request to WebService1.asmx and calls the PRETURN service method. So as a user is typing the textBox brings a drop down list of strings that start with the letters the user is typing.
I now have the following DataList -
<asp:DataList runat="server" ID="pTextBox" >
<ItemTemplate>
<asp:CheckBox ID="CheckBoxPN" runat="server" Checked='false' OnCheckedChanged="CheckBoxPN_CheckedChanged" AutoPostBack="true" />
<asp:TextBox ID="profileTextBox" runat="server" Text='<%# Container.DataItem.ToString() %>'></asp:TextBox>
</ItemTemplate>
</asp:DataList>
Where on Page_Load -
WebService1 ws = new WebService1();
pTextBox.DataSource = ws.Method();
pTextBox.DataBind();
My problem is I want to combine the functionality of the textBox with the DataList. So that when a user types into the textBox, instead of the textBox having a dropdown list, the rows in the DataList are updated. So for instance, if Text in the profileTextBox did not contain the prefix text in TextBox1 when the user was typing, it would disappear. Leaving the user with a list of rows relevant to their search. How can I achieve this?
Others have done something similar to the GridView control, using JQuery to show/hide rows depending on the filter criteria. One solution, which should be easily adaptable to the DataList, is available here.