Ich bin ein neuer ASP.NET-Entwickler und versuche, Linq-To-Entities zu lernen. Ich versuche, eine DropDownList mit der Linq-Anweisung zu binden, um die Statusliste in der Statusentität abzurufen. Alles funktioniert gut. Ich versuche jedoch jetzt, der DropDownList die Option "Select" hinzuzufügen, aber bei mir funktioniert das nicht. Könnten Sie mir bitte sagen, wie ich das beheben kann?
ASP.NET-Code:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
Code-Behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.Items.Add(new ListItem("Select", "0", true));
bindStatusDropDownList();
}
}
private void bindStatusDropDownList()
{
Status status = new Status();
DropDownList1.DataSource = status.getData();
DropDownList1.DataValueField = "ID";
DropDownList1.DataTextField = "Description";
DropDownList1.DataBind();
}
UPDATE:
Ich habe es auch im Markup-Set der DropDownList versucht, aber es funktionierte bei mir nicht
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Selected="True" Value="0" Text="Select"></asp:ListItem>
</asp:DropDownList>
Der Grund, warum es nicht funktioniert, ist, dass Sie der Liste ein Element hinzufügen und dann die gesamte Liste mit einer neuen DataSource
überschreiben, die Ihre Liste löscht und neu füllt, wobei das erste manuell hinzugefügte Element verloren geht.
Also musst du dies umgekehrt machen:
Status status = new Status();
DropDownList1.DataSource = status.getData();
DropDownList1.DataValueField = "ID";
DropDownList1.DataTextField = "Description";
DropDownList1.DataBind();
// Then add your first item
DropDownList1.Items.Insert(0, "Select");
Obwohl dies eine ziemlich alte Frage ist, besteht eine andere Möglichkeit darin, die AppendDataBoundItems - Eigenschaft zu ändern. Der Code lautet also:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
AppendDataBoundItems="True">
<asp:ListItem Selected="True" Value="0" Text="Select"></asp:ListItem>
</asp:DropDownList>
Ich habe mit folgendem Code versucht. es funktioniert gut für mich
ManageOrder Order = new ManageOrder();
Organization.DataSource = Order.getAllOrganization(Session["userID"].ToString());
Organization.DataValueField = "OrganisationID";
Organization.DataTextField = "OrganisationName";
Organization.DataBind();
Organization.Items.Insert(0, new ListItem("Select Organisation", "0"));
Private Sub YourWebPage_PreRenderComplete(sender As Object, e As EventArgs) Handles Me.PreRenderComplete
If Not IsPostBack Then
DropDownList1.Items.Insert(0, "Select")
End If
End Sub
Move DropDownList1.Items.Add (neues ListItem ("Select", "0", true)); After bindStatusDropDownList ();
so:
if (!IsPostBack)
{
bindStatusDropDownList(); //first create structure
DropDownList1.Items.Add(new ListItem("Select", "0", true)); // after add item
}
Wenn Sie das erste Element deaktivieren möchten, versuchen Sie Folgendes:
DropDownList1.Items.Insert(0, new ListItem("Select", "-1"));
DropDownList1.Items[0].Attributes.Add("disabled", "disabled");
Wenn Sie ein "Hinzufügen" ausführen, wird es am Ende der Liste hinzugefügt. Sie müssen ein "Einfügen" vornehmen, wenn das Element an der Spitze der Liste hinzugefügt werden soll.