Best way to add to DDL


D

David C

Can someone help me figure out how to add entries to a DropDownList control
"on-the-fly"? I have a FormView with several DropDownList controls that are
bound to differnt lookup table in a SQL Server database. I have no problem
with code to add a record to the bound table but am getting errors every
time I try to set the SelectedValue to the new ID number of the added record
in the Page_Load event. Below is an example control and DataSource I have
for one of the DDL's. Thank you.

David

<asp:DropDownList ID="ddlAgentID" runat="server" DataSourceID="SqltlkpAgent"
DataTextField="Agent" DataValueField="AgentID"
SelectedValue='<%# Bind("AgentID") %>' Width="300"
AppendDataBoundItems="true">
<asp:ListItem Value="-1" Text="&lt;&lt;New Agent&gt;&gt;"></asp:ListItem>
</asp:DropDownList>

<asp:SqlDataSource ID="SqltlkpAgent" runat="server" ConnectionString="<%$
ConnectionStrings:FiledataConnectionString %>"
SelectCommand="SELECT 0 As AgentID, NULL As Agent UNION SELECT [AgentID],
[Agent] FROM [tlkpAgent] ORDER BY [Agent]">
</asp:SqlDataSource>
 
Ad

Advertisements

S

Scott Roberts

As I mentioned in your other thread on this same topic, you only need to
rebind the DDL (ddlAgentID.DataBind()). Your problem is that you have
AppendDataBoundItems="true" so every time you rebind you are appending to
the list instead of refreshing it.
 
Ad

Advertisements

D

David C

You are right. Once I changed that it worked fine. Sometimes my brain
malfunctions. Have a safe holiday.

Scott Roberts said:
As I mentioned in your other thread on this same topic, you only need to
rebind the DDL (ddlAgentID.DataBind()). Your problem is that you have
AppendDataBoundItems="true" so every time you rebind you are appending to
the list instead of refreshing it.


David C said:
Can someone help me figure out how to add entries to a DropDownList
control "on-the-fly"? I have a FormView with several DropDownList
controls that are bound to differnt lookup table in a SQL Server
database. I have no problem with code to add a record to the bound table
but am getting errors every time I try to set the SelectedValue to the
new ID number of the added record in the Page_Load event. Below is an
example control and DataSource I have for one of the DDL's. Thank you.

David

<asp:DropDownList ID="ddlAgentID" runat="server"
DataSourceID="SqltlkpAgent" DataTextField="Agent"
DataValueField="AgentID"
SelectedValue='<%# Bind("AgentID") %>' Width="300"
AppendDataBoundItems="true">
<asp:ListItem Value="-1" Text="&lt;&lt;New Agent&gt;&gt;"></asp:ListItem>
</asp:DropDownList>

<asp:SqlDataSource ID="SqltlkpAgent" runat="server" ConnectionString="<%$
ConnectionStrings:FiledataConnectionString %>"
SelectCommand="SELECT 0 As AgentID, NULL As Agent UNION SELECT [AgentID],
[Agent] FROM [tlkpAgent] ORDER BY [Agent]">
</asp:SqlDataSource>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top