I'm using Visual Studio 2005 and asp.net 2.0.
I have a page that contains a formview with an EditItemTemplate and an InsertItemTemplate. I want to set the default value when the form is called in insert mode. I'm doing it in the Page_Load method as follows:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["peID"] == null || Session["peID"].ToString() == "")
{
PEFormView0.ChangeMode(FormViewMode.Insert);
((DropDownList)((View)PEFormView0.FindControl("PEMainView")).FindControl("PreparedByDropDownList")).SelectedValue = Session["accountName"].ToString();
}
else
{
.
.
}
}
if (((DropDownList)((View)PEFormView0.FindControl("PEMainView")).FindControl("KeyTeachingPointDropDownList")).SelectedIndex >= 0)
{
((TextBox)((View)PEFormView0.FindControl("PEMainView")).FindControl("SelectedSkillTextBox")).Text = ((DropDownList)((View)PEFormView0.FindControl("PEMainView")).FindControl("KeyTeachingPointDropDownList")).SelectedItem.Text;
}
}
Here is my aspx code:
<asp:FormView ID="PEFormView0" runat="server" DataSourceID="PEDataSource" DefaultMode="Edit" DataKeyNames="PEID" OnItemInserting="PEFormView0_ItemInserting" Width="1025px">
<EditItemTemplate>
<asp:MultiView ID="PEMultiView" runat="server" ActiveViewIndex="0">
<asp:View ID="PEMainView" OnActivate="PEMainView_Activate" runat="server">
<label class="FloatLabel">Selected Skill:</label><br />
<asp:TextBox ID="SelectedSkillTextBox" runat="Server" Width="270px" Height="125px" ReadOnly="true" TextMode="multiLine" Wrap="true"></asp:TextBox><br /><br />
<label class="DataEntryLabel">Prepared By:</label>
<aspropDownList ID="PreparedByDropDownList" TabIndex="9" runat="server" DataSourceID="UserDataSource" Width="170px" DataTextField="name" DataValueField="userName"
SelectedValue='<%# Bind("PreparedBy") %>'>
</aspropDownList><br />
.
.
For some reason, setting the selected value is not working and the PreparedByDropDownList still contains the first item in the datasource list no matter what I do.
I also tried setting the selectedvalues of PreparedByDropDownList with the following code which didn't work either:
foreach (ListItem preparedByItem in ((DropDownList)((View)PEFormView0.FindControl("PEMainView")).FindControl("PreparedByDropDownList")).Items)
{
if (preparedByItem.Selected)
preparedByItem.Selected = false;
}
((DropDownList)((View)PEFormView0.FindControl("PEMainView")).FindControl("PreparedByDropDownList")).Items.FindByValue(Session["accountName"].ToString()).Selected = true;
In addition, I'm setting an initial value for the SelectedSkillTextBox as well and that one works in Edit mode, but the textbox stays blank in insert mode.
Can anyone tell me how to set the default or initial value of a control in insert mode?
I have a page that contains a formview with an EditItemTemplate and an InsertItemTemplate. I want to set the default value when the form is called in insert mode. I'm doing it in the Page_Load method as follows:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["peID"] == null || Session["peID"].ToString() == "")
{
PEFormView0.ChangeMode(FormViewMode.Insert);
((DropDownList)((View)PEFormView0.FindControl("PEMainView")).FindControl("PreparedByDropDownList")).SelectedValue = Session["accountName"].ToString();
}
else
{
.
.
}
}
if (((DropDownList)((View)PEFormView0.FindControl("PEMainView")).FindControl("KeyTeachingPointDropDownList")).SelectedIndex >= 0)
{
((TextBox)((View)PEFormView0.FindControl("PEMainView")).FindControl("SelectedSkillTextBox")).Text = ((DropDownList)((View)PEFormView0.FindControl("PEMainView")).FindControl("KeyTeachingPointDropDownList")).SelectedItem.Text;
}
}
Here is my aspx code:
<asp:FormView ID="PEFormView0" runat="server" DataSourceID="PEDataSource" DefaultMode="Edit" DataKeyNames="PEID" OnItemInserting="PEFormView0_ItemInserting" Width="1025px">
<EditItemTemplate>
<asp:MultiView ID="PEMultiView" runat="server" ActiveViewIndex="0">
<asp:View ID="PEMainView" OnActivate="PEMainView_Activate" runat="server">
<label class="FloatLabel">Selected Skill:</label><br />
<asp:TextBox ID="SelectedSkillTextBox" runat="Server" Width="270px" Height="125px" ReadOnly="true" TextMode="multiLine" Wrap="true"></asp:TextBox><br /><br />
<label class="DataEntryLabel">Prepared By:</label>
<aspropDownList ID="PreparedByDropDownList" TabIndex="9" runat="server" DataSourceID="UserDataSource" Width="170px" DataTextField="name" DataValueField="userName"
SelectedValue='<%# Bind("PreparedBy") %>'>
</aspropDownList><br />
.
.
For some reason, setting the selected value is not working and the PreparedByDropDownList still contains the first item in the datasource list no matter what I do.
I also tried setting the selectedvalues of PreparedByDropDownList with the following code which didn't work either:
foreach (ListItem preparedByItem in ((DropDownList)((View)PEFormView0.FindControl("PEMainView")).FindControl("PreparedByDropDownList")).Items)
{
if (preparedByItem.Selected)
preparedByItem.Selected = false;
}
((DropDownList)((View)PEFormView0.FindControl("PEMainView")).FindControl("PreparedByDropDownList")).Items.FindByValue(Session["accountName"].ToString()).Selected = true;
In addition, I'm setting an initial value for the SelectedSkillTextBox as well and that one works in Edit mode, but the textbox stays blank in insert mode.
Can anyone tell me how to set the default or initial value of a control in insert mode?