I have a gridview in which there are multiple rows with Edit options in each row. So what exactly the scenario is,
whenever I click on EDIT button it will redirect me to next tab with the SAP ID of that row filled in the dropdownlist.
But currently what happening is, when I click the edit button for the first time, it shows only one value in the list,
but when I click second time, the list shows me two values. I dont want that, I want only the data value for which I have clicked the edit button.
Below is my GridView and ajax code for binding values
GRIDVIEW
<asp:GridView ID="grdSapDetails" runat="server" PageSize="10" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="SAP_ID" HeaderText="Sap Id" />
<asp:BoundField DataField="SITE_NAME" HeaderText="Site Name" />
<asp:BoundField DataField="SITE_ADDRESS" HeaderText="Site Address" />
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<input type="button" onclick='return HighlightTabFunction(this)' value="Edit" id="btnEdit" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
AJAX
function HighlightTabFunction(a) {
var row = a.parentNode.parentNode;
var rowIndex = row.rowIndex - 1;
var SAPID = row.cells[0].innerHTML;
$.ajax({
url: "UBRDashboard.aspx/GETSTATEFROM_SAP",
dataType: "json",
type: "POST",
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ SAPID: SAPID }),
async: true,
processData: false,
cache: false,
success: function (r) {
$('a[href="#tabs-2"]').click();
var ddlSapID = $("[id*=ddlSapID]");
ddlSapID.append($("<option></option>").val(r.d).html(row.cells[0].innerHTML));
$("#hdnState").val(r.d);
},
error: function (xhr) {
alert('Error while selecting list..!!');
}
})
You probably have to remove the dropdown list item before you add new. You can call remove() on children of dropdown (select)
success: function (r) {
$('a[href="#tabs-2"]').click();
var ddlSapID = $("[id*=ddlSapID]");
ddlSapID.children().remove(); //this will remove option before appending new
ddlSapID.append($("<option></option>").val(r.d).html(row.cells[0].innerHTML));
$("#hdnState").val(r.d);
},