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:"/>
<aspropDownList ID="RelTypeIdTextBox" runat="server"
DataSourceID="dsRelType" DataTextField="RecName" DataValueField="RecId"
SelectedValue='<%# Bind("RelTypeId") %>'></aspropDownList><br />
<asp:Label ID="PositionIdLabel" runat="server" Text="Position:"/>
<aspropDownList ID="PositionIdTextBox" runat="server"
DataSourceID="dsRelPos" DataTextField="RecName" DataValueField="RecId"
SelectedValue='<%# Bind("PositionId") %>' /> <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;
}
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:"/>
<aspropDownList ID="RelTypeIdTextBox" runat="server"
DataSourceID="dsRelType" DataTextField="RecName" DataValueField="RecId"
SelectedValue='<%# Bind("RelTypeId") %>'></aspropDownList><br />
<asp:Label ID="PositionIdLabel" runat="server" Text="Position:"/>
<aspropDownList ID="PositionIdTextBox" runat="server"
DataSourceID="dsRelPos" DataTextField="RecName" DataValueField="RecId"
SelectedValue='<%# Bind("PositionId") %>' /> <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;
}