How to retain values in a FormView control after rebinding the data

A

Alan Lambert

I have a FormView with a couple of DropDownLists and a Button. When clicked
the button adds a new value to one of the DropDownLists and then calls
DataBind on the FormView to repopulate the DropDownList (calling it on the
DropDownList produces a binding error).

In the button's click event handler I read in the values currently stored in
the formview's fields, rebind the data and then put the values back (as I
want any values currently entered but not saved to remain in the fields).
However, the values don't the reappear in the FormView. I have tried
repopulating onthe FormView's ItemCreated event handler and the databound
event handler. All fail to give me the desired result.

How should I achieve this?

Relevant code snippets are below.

Thanks in advance for your help.

Alan

<asp:FormView ID="FormView1" runat="server" DataKeyNames="ContactRelId"
DataSourceID="dsContactRel" DefaultMode="Insert">
<InsertItemTemplate>
<asp:Label ID="RelTypeIdLabel" runat="server" Text="Relation
type:"/>
<asp:DropDownList ID="RelTypeIdTextBox" runat="server"
DataSourceID="dsRelType" DataTextField="RecName" DataValueField="RecId"
SelectedValue='<%# Bind("RelTypeId") %>'></asp:DropDownList><br />
<asp:Label ID="PositionIdLabel" runat="server" Text="Position:"/>
<asp:DropDownList ID="PositionIdTextBox" runat="server"
DataSourceID="dsRelPos" DataTextField="RecName" DataValueField="RecId"
SelectedValue='<%# Bind("PositionId") %>' />&nbsp;<asp:Button
id="AddPositionButton" runat="server" Text="Add" Visible="true"
OnClick="AddPositionButton_Click" />
</InsertItemTemplate>
</asp:FormView>

protected void AddPositionButton_Click(Object sender, EventArgs e)
{
DropDownList relationTypeControl =
FormView1.FindControl("RelTypeIdTextBox") as DropDownList;
DropDownList positionControl =
FormView1.FindControl("PositionIdTextBox") as DropDownList;

string relationType = relationTypeControl.SelectedValue;
string position = positionControl.SelectedValue;

FormView1.DataBind();

relationTypeControl.SelectedValue = relationType;
positionControl.SelectedValue = position;
}
 

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

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top