DropDownList Selected Item Incorrect When Using FindControl - c#

I have a number of dropdownlists embedded in a gridview. When I submit the page I loop through all the rows of the gridview and use the findcontrol method to get the dropdownlist e.g:
foreach (GridViewRow gvrItem in gvItems.Rows)
{
DropDownList ddlOption = gvrItem.Cells[2].FindControl("ddlOption") as DropDownList;
}
This works nicely, however when I try to get the selected item of the dropdownlist e.g:
ddlOption .SelectedItem.Text
It always returns the first item in the list rather than whats actually selecte din the page. Any ideas what I'm doing wrong?

You need to do this after the GridView has been databound. Try calling it in the DataBound event:
protected void GridView1_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow gvrItem in gvItems.Rows)
{
DropDownList ddlOption = gvrItem.Cells[2].FindControl("ddlOption") as DropDownList;
}
string selectedItem = ddlOption.SelectedItem.Text;
}

It turned out to be a quirk of .Net, if you fill the drop down list with ListItem's it won't carry those items into the ViewState. If you fill the drop down list with strings it will. Very odd I know.
e.g.:
DropDownList ddl = new DropDownList();
ddl.Add(new ListItem("text", "value")); <----Fails :(
ddl.Add("text"); <---- Works :)

Related

How to keep values in textbox inside a binded GridView on ASP.NET?

I have a gridView, and inside is there exist a modifiable textbox.
I also have a dropdownlist, where if I click the dropdownlist, the system will read the database in SQL and display it in gridView.
How do I keep the number I previously typed in the first row of the gridView, when a new data is added after the dropdownlist is clicked?
Normally, the previously entered value of the textbox will be resetted if i rebind the gridView witthout refilling the textbox with a saved list or behaviour
Thank you very much in advance
I'm assuming you are using something like the OnSelectedIndexChanged event of the DropDownList. In there you can use FindControl to find the TextBox, store the data, rebind the GridView and set the value back to the TextBox. You need to use FindControl twice, one for the old and once for the new TextBox.
Note the use of oldValues.Count instead of GridView1.Rows.Count. If there were less rows in the new dataset than in the old you would get Index out of Bounds error.
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
List<string> oldValues = new List<string>();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
TextBox tbOld = GridView1.Rows[i].FindControl("TextBox1") as TextBox;
oldValues.Add(tbOld.Text);
}
//rebind gridview here
for (int i = 0; i < oldValues.Count; i++)
{
TextBox tbNew = GridView1.Rows[i].FindControl("TextBox1") as TextBox;
tbNew.Text = oldValues[i];
}
}
You have used dropdownlist and textbox inside gridview?
if you click dropdownlist select value then postback to server and textbox value reset?

c# dropdownlist selectedindexchanged in gridview sets selectedindex of second dropdownlist

I have a gridview with two dropdownlists populated from a db. One is a descriptive name, the other is an abbreviated name. I need to accomplish the following:
When I select an item in DDL1 I need to change the selected index of DDL2 to match, and vice versa.
I have searched on here and found the following:
protected void ddlAddLabTest_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlLabTest = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddlLabTest.NamingContainer;
DropDownList ddlAddLabTestShortName = (DropDownList)row.FindControl("ddlAddShortname");
ddlAddLabTestShortName.SelectedIndex = intSelectedIndex;
}
Only when it gets to the assignment for "row" I receive the following:
Unable to cast object of type 'System.Web.UI.WebControls.DataGridItem' to type 'System.Web.UI.WebControls.GridViewRow'.
I have tried finding a working example but I can't. Any help is greatly appreciated!
Try this
protected void ddlAddLabTest_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlLabTest = (DropDownList)sender;
DataGridItem row = (DataGridItem) ddlLabTest.NamingContainer;
DropDownList ddlAddLabTestShortName = (DropDownList)row.FindControl("ddlAddShortname");
ddlAddLabTestShortName.SelectedIndex = intSelectedIndex;
}
Seems like the NamingContainer is not a row, so leave it like that. It already has the FindControl method.
var row = ddlLabTest.NamingContainer;

How to find a DropDownList in a GridView ItemTemplate without RowDataBound?

I have a DropDownList outside of a GridView and I have a DropDownList inside an ItemTemplate of a GridView. The DropDownList that is outside has a SelectedIndex_Changed event and when that fires, it should populate the DropDownList inside the GridView. The problem is that in the method that I use to populate the inside DropDownList, it can't find the control: Here is sample code that is called when the outside DropDownList is changed:
//Does not find ddlRoom
DropDownList ddlRoom = (DropDownList)gv.TemplateControl.FindControl("ddlRoom");
if (rows.Count() > 0)
{
var rooms = rows.CopyToDataTable();
ddlRoom.Items.Clear();
ddlRoom.Items.Add(new ListItem("Select...", "-1"));
ddlRoom.DataSource = rooms;
ddlRoom.DataBind();
}
I have also tried:
DropDownList ddlRoom = (DropDownList)gv.FindControl("ddlRoom");
You'll need to bound the dropdown for each row. Try something like this
DropDownList ddlRoom = null;
foreach(var gridRow in gv.Rows)
{
ddlRoom = gridRow.FindControl("ddlRoom") as DropDownList;
if (ddlRoom != null)
{
//your code here
}
}

Accessing gridview from a non GridView event

I have a button outside a gridview called "UpdateAll", i need to be able to click on such button and find an item template "DropDownList" and update the database with that value for each record, I am not sure how to go about this any ideas?
public void UpdateAll_Click(object sender, EventArgs e)
{
}
Now I know I can access the drop down in the GridView_RowCommand something like this
GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
Myddl = row.FindControl("Quantity") as DropDownList;
But i am not sure how to do that from an outside event that is not GridView related, I can not do the one above because it accesses the e.CommandSource.
Please help.
You can do something like this;
for(int rowIndex =0; rowIndex<gv.rows.count; rowIndex++)
{
Myddl = gv.rows[rowIndex].FindControl("Quantity") as DropDownList;
}

selected index of DDL is always equal to Zero in asp .net

I have a Program Class, with properties as Id, ProgramName,ShortName and Code, im my app
I have a ASP DDL like
<asp:DropDownList ID="DDLProgram" runat="server"
OnSelectedIndexChanged ="OnDDLProgramChanged" AutoPostBack = "true">
</asp:DropDownList>
My OnDDLProgramChanged method is defined as
protected void OnDDLProgramChanged(object sender, EventArgs e)
{
List<CcProgramEntity> programEntities = GetAllPrograms();
DDLProgram.DataSource = programEntities;
DDLProgram.DataTextField = "Shortname";
DDLProgram.DataValueField = "Id";
//My Problem goes here
string programCode = programEntities[DDLProgram.SelectedIndex].Code;
}
My list is getting all the records correctly, I have checked it. But whenever I am changing an item in the DDL, the selected index in not changing. The selected index is remaining zero.Therefore, I am not being able to get the code of other items but the 0 index's item.
Can anyone help me in this case?
You are binding the data again in your selectedIndex Change event and it will reset your current SelectedIndex after rebinding. You don't need to rebind the data to your dropdown in SelectedIndex Change Event
It should be like..
protected void OnDDLProgramChanged(object sender, EventArgs e)
{
string programCode = programEntities[DDLProgram.SelectedIndex].Code;
}
You have to bind data to the DropDownList on page load method
if (!IsPostBack)
{
DDLProgram.DataSource = programEntities;
DDLProgram.DataTextField = "Shortname";
DDLProgram.DataValueField = "Id";
DDLProgram.DataBind();
}
Otherwise it will bind data everytime and hence clear the selection
Why are you assigning the DataSource in OnDDLProgramChanged, this would be resetting the selection you make.

Categories

Resources