Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot set SelectedValue of dropdownlist in insert mode of formview 1

Status
Not open for further replies.

JanetM

Programmer
Jul 6, 2009
9
US
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>
<asp:DropDownList ID="PreparedByDropDownList" TabIndex="9" runat="server" DataSourceID="UserDataSource" Width="170px" DataTextField="name" DataValueField="userName"
SelectedValue='<%# Bind("PreparedBy") %>'>
</asp:DropDownList><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?
 
Try setting the default value in the ModeChanged event of the formview
 
Didn't work for me, but it didn't even fire the ModeChanged event.

I modified my aspx with the following:
<asp:FormView ID="PEFormView0" runat="server" DataSourceID="PEDataSource" DefaultMode="Edit" DataKeyNames="PEID" OnItemInserting="PEFormView0_ItemInserting" Width="1025px" OnModeChanged="PEFormView0_ModeChanged">

and added the following method to my aspx:

protected void PEFormView0_ModeChanged(object sender, EventArgs e)
{
((DropDownList)((View)PEFormView0.FindControl("PEMainView")).FindControl("PreparedByDropDownList")).SelectedValue = Session["accountName"].ToString();
DropDownList temp2 = ((DropDownList)((View)PEFormView0.FindControl("PEMainView")).FindControl("PreparedByDropDownList"));
string test2 = temp2.SelectedValue;
}

What am I doing wrong?
 
i don't think you can set the dropdown list this way. you need to set the selected index.
Code:
MyDDL.SelectedIndex = MyDDL.Items.IndexOf(MyDDL.Items.FindByValue(theValue));

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
I could also be a timing issue between when the dropdownlist items are bound and when you setting the selected value.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
I tried setting the SelectedIndex as follows:

DropDownList pbDropDown = ((DropDownList)((View)PEFormView0.FindControl("PEMainView")).FindControl("PreparedByDropDownList"));
string test1 = pbDropDown.SelectedValue;
pbDropDown.SelectedIndex = pbDropDown.Items.IndexOf(pbDropDown.Items.FindByValue(Session["accountName"].ToString()));
string test2 = pbDropDown.SelectedValue;

Still didn't work, same result.

I don't think it's a timing issue because I can see the SelectedValue change from the old value to the default value in the debugger.

Any other ideas?
 
when i was using webforms I remember running into a similar problem. The problem was the selected index was set before the items were bound, so the index defaulted to -1.

I notice you are using DataSource controls. they are (or will become) a PITA because you have no control over them. I would recommend dropping these in favor of actual code to access the database. This will also help debug the issue as you can step through more of the code.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Yes, some things have been a PITA because I'm using DataSource controls, but I have multiple pages now and everything is working in my application and I'm just putting in the final small details, so at this point, it would be much more work to redo that.

If I'm seeing a SelectedValue in the dropdown, doesn't that mean the control has been bound already? It does seem like that is the problem though because somehow it is getting reset back to -1. The weird thing is that the same code to set this default works fine in the Page_Load when the formview is in Edit mode, but it's not working when the formview is in Insert mode. Is there another place I can put this code so that it will execute after the control is bound?
 
I believe your best option is to drop the datasource control for this scenario and use ado.net to get the data. take control of the code rather than letting VS/MS decide for you.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
I really can't do that at this point. I have too much code and too many tabbed pages that contain multiviews inside of formviews and almost everything is working. I don't have time to redo it all, but maybe for a future release.
 
not everything, just this scenario.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
This particular page is a complex multi-tab page which consists of a multiview inside of a formview with multiple views. It would be too much at this point to rewrite it and I would also have to rewrite the stuff that is common between this page and my other pages.

There has got to be a way to set the default. I don't see why this can't be done.
 
I resolved the problem by creating a DataBound event for the formview as follows:

protected void PEFormView0_DataBound(object sender, EventArgs e)
{
if (PEFormView0.CurrentMode == FormViewMode.Insert)
{
((DropDownList)((View)PEFormView0.FindControl("PEMainView")).FindControl("PreparedByDropDownList")).SelectedValue = Session["accountName"].ToString();
DropDownList temp2 = ((DropDownList)((View)PEFormView0.FindControl("PEMainView")).FindControl("PreparedByDropDownList"));
string test2 = temp2.SelectedValue;
}

//Display the initially selected skill
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;
}
}

So it was a timing issue. The data was getting bound to the controls after I was setting the default values. It works now this way.
 
congratulations!

For future reference i would also recommend researching encapsulation. this will do wonders for:
1. reducing complexity
2. reducing verbosity

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top