วิธีการลบรายการที่ซ้ำกันใน asp: dropdownlist, Sharepoint Designer 2013

ฉันใช้ asp:drowdownlist บน dataviewwebpart และเชื่อมโยงกับต้นทาง spdatasource1 มีรายการที่ซ้ำกันหลายรายการ ฉันจะลบรายการที่ซ้ำกันเหล่านั้นได้อย่างไร

asp:DropDownList runat="server" id="DropDownList1" DataValueField="ID" DataTextField="ProgramMaster" DataSourceID="spdatasource1" AutoPostBack="False" AppendDataBoundItems="True" ToolTip="Select a Program from the list"/>

นอกจากนี้ยังแสดงรายการในรหัสการก่อตัวต่อไปนี้#ProgramName ฉันจะรับ programName เท่านั้นได้อย่างไร


person Ryan Koir    schedule 07.04.2014    source แหล่งที่มา


คำตอบ (2)


ฉันใช้ JQuery เพื่อลบรายการที่ซ้ำกันออกจาก asp:dropdownlist และนี่คือโค้ดในกรณีที่มีคนต้องการมัน โค้ดทำงานในสี่ส่วน รับค่าจากดรอปดาวน์ ตัดส่วนที่ซ้ำกันและรับค่าในรูปแบบที่ใช้งานได้ ลบค่าที่มีอยู่ออกจากดรอปดาวน์และชุดสุดท้าย หรือผนวกค่ากลับเข้าไปในรายการดรอปดาวน์

<script type="text/javascript">
$(document).ready(function(){
var handles= [];               
$('#DropDownList1 option').each(function() { 
var Prog1 = $(this).attr('text');
if(Prog1 != 'All'){
var Prog2 = Prog1.split(';#');
handles.push('All');
handles.push(Prog2[1]);
 }
//Remove The existed Dropdownlist value
$("select#DropDownList1 option[value='" + $(this).val() + "']").remove();
//$(this).val().remove();
});
//Removing the Duplicates
var individual = [];
for (var i = 0; i<handles.length; i++) {
if((jQuery.inArray(handles[i], individual )) == -1)
{
individual .push(handles[i]);
} 
 }
//Inserting or setting the value from array individual to the dropdownlist.  
var sel =   document.getElementById('DropDownList1');
for(var i = 0; i < individual.length; i++) {
var opt = document.createElement('option');
opt.innerHTML = individual[i];
opt.value = individual[i];
sel.appendChild(opt);
    }
 });
</script>

ป.ล. หาก ID ที่ระบุใช้งานไม่ได้กับดรอปดาวน์ ให้รับ ID จากเครื่องมือดีบัก IE ซึ่งจะอยู่ในรูปแบบ ctl00_PlaceHolderMain_g_a0a2fb36_2203_4d2e_bcd4_6f42243b880f_DropDownList1

person Ryan Koir    schedule 07.04.2014

คุณสามารถทำได้ด้วย jquery

var usedNames = {};
$("select[name='company'] > option").each(function () {
if(usedNames[this.text]) {
    $(this).remove();
} else {
    usedNames[this.text] = this.value;
}
});

หรือด้วยโค้ดฝั่งเซิร์ฟเวอร์ลองสิ่งนี้

void RemoveDuplicateItems(DropDownList ddl)
{

for (int i = 0; i < ddl.Items.Count; i++)
{

ddl.SelectedIndex = i;
string str = ddl.SelectedItem.ToString();
for (int counter = i + 1; counter < ddl.Items.Count; counter++)
{
ddl.SelectedIndex = counter;
string compareStr = ddl.SelectedItem.ToString();
if (str == compareStr)
 {
  ddl.Items.RemoveAt(counter);
  counter = counter - 1;
 }  


}    }     }
person Richa Jain    schedule 17.04.2014