to populate one dropdown on selectedindexchanged of another inside gridview - c#

I have gridview with two dropdowns in a row say country and state,on change of country dropdown i want to populate the state dropdown when the grid is in edit state.
I got both the dropdowns in RowEditing event of grid,also selectedindexchanged event is attached to first dropdown of grid.The problem is how to get second dropdown i.e. state dropdown in selectedindexchange event of country dropdown.

protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl1 = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddl1.NamingContainer;
if (row != null)
{
DropDownList ddl2 = (DropDownList)row.FindControl("DropDownList3");
{
//call the method for binding the second DDL based on the selected item on the first DDL
DataTable dt = BindDropDownList(ddl1.SelectedItem.Text);
ddl2.DataTextField = "Field1";
ddl2.DataValueField = "Field2";
ddl2.DataBind();
}
}
}

If RowEditing event occurs before dropdown selection changed event, you can store reference to current Row in you application. Then get this row in dropdown changed event.

Related

Getting value of dropdownlist that's added onrowdatabound asp.net c#

I'm querying a database filling a gridview with values, also adding dropdown boxes into each cell within the dataview 'onrowdatabound' so these DDL's are populated when the gridview is populated.
I want to be able to click a button to get values from these DDL's however when the button is clicked postback happens and all the DDL's disappear and it gives me the default value for the DDL.
I assume they dissapear as they're not called on pageload (which I can't seem to do as they're called onrowdatabound)
<asp:GridView id="View" name="Spview" onrowdatabound="populateCellswithDDls" runat="server"></asp:GridView>
Adding the ddl with inside 'populateCellswithDDls' function looping each cell:
e.Row.Cells[i].Controls.Add(DDL1);
The next thing I've have a play with is ViewState and Sessions to save the dropdownlists on postback(Tried making sessions within 'populateCellswithDDls' function as so:
DropDownList DDL1 = new DropDownList();
//I've tried newSkillsMon.AutoPostBack = true; but this just removes them all too
Session.Add("ViewState", View);
Session.Add("DropdownState", DDL1);
I've tried all sorts do to with viewstate and session but unsure where to use them in relation to saving states the 'onrowdatabound' population.
My button currently looks like this:
protected void confirm_Click(object sender, EventArgs e){
{foreach (GridViewRow row in View.Rows)
// if (DDL1.SelectedItem.Value != "Select Item"){
if (IsPostBack)
{
Debug.WriteLine(DDL1.SelectedValue);
}
This just gives me X amount of "Select Item" rather than what i have selected in the DDL
What am I missing, where would I add these sessions to keep the ddl's created by onrowdatabound?
Thanks
When creating dynamic controls, you need to recreate them on every Page Load and that includes a PostBack. So start with binding the GridView data outside an IsPostBack check.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//normally you would bind here
}
//but now bind grid every page load
GridView1.DataSource = Common.LoadFromDB();
GridView1.DataBind();
}
Now in the RowDataBound event make sure the DropDownList has an ID
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the row is a datarow
if (e.Row.RowType == DataControlRowType.DataRow)
{
//create a dropdownlist and assign an ID
DropDownList DDL1 = new DropDownList();
DDL1.ID = "DDL1";
//add some dummy listitems
DDL1.Items.Insert(0, new ListItem() { Text = "A", Value = "A" });
DDL1.Items.Insert(1, new ListItem() { Text = "B", Value = "B" });
DDL1.Items.Insert(2, new ListItem() { Text = "C", Value = "C" });
//add the control to the row
e.Row.Cells[0].Controls.Add(DDL1);
}
}
Now you can get the value from the correct row on a button click.
protected void Button1_Click(object sender, EventArgs e)
{
//find the dropdownlist in the correct row and cast it back to one
DropDownList DDL1 = GridView1.Rows[i].FindControl("DDL1") as DropDownList;
//display the result
Label1.Text = DDL1.SelectedValue;
}
Something that might work for you instead of trying sessions is to use hiddenfields.
Make a hidden field for each dropdown location and then use javascript to populate the hidden fields with the dropdown values. (Possibly wanting some server side validation on submission)
Something like this with jQuery:
$(".dropdowns").on("change", function () {
$(this).closest('input:hidden').val($(this).val());
});
And in your confirmation:
if (HF_HiddenField1.Value != "Select Item")

Keeping the selected item of DropDownList during resetting DataSource

I have several DropDownlist controls in my web page which the DataSource of all of them set to a specified list of items.
When one of these items is selected from one of the DropDownlist ,this item should remove from the list of other DropDownList. So I should set the DataSource of all of DropDownList again.
But in this case the selected item returned to the first item.
Is there any way to keep the selected item of DropDownList during resetting DataSource?
Edit:
protected void dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(dropdownlist1.Text))
//Some code to remove the selected item from list so the user can not see it in the select list anymore
dropdownlist1.DataSource = myList;
dropdownlist1.DataBind();
dropdownlist2.DataSource = myList;
dropdownlist2.DataBind();
dropdownlist3.DataSource = myList;
dropdownlist3.DataBind();
//...
}

getting command id from drop down list in a grid view

i inserted a drop down list into my grid view from the code behind like it is done here
Convert gridview field into Dropdownlist
i need to make command in the row that contains this dropdownlist when its selected index is changed.
please can some one hepl me
thanks
If you need to get the GridViewRow of a DropDownList which was generated dynamically use following approach.
Add the same SelectedIndexChanged-event handler to the dropdowns. Handle it, cast sender to DropDownList, cast it's NamingContainer to GridViewRow. Now you are all set.
protected void DropDown_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList) sender;
GridViewRow row = (GridViewRow) ddl.NamingContainer;
// now you get the reference to the other controls via row.FindControl
}

how to get dropdown value inside gridview on button click in c#?

I have tried this but its not working,giving null value:please tell me what have to change in this code.
protected void Button1_Click(object sender, EventArgs e)
{
GridViewRow row;
row = dgData.Rows[0];
DropDownList ddl= (DropDownList)(row.Cells[1].FindControl("ddlCol1"));
}
use
DropDownList ddl= (DropDownList)(row.FindControl("ddlCol1"));
and try and let me know
try this code on click event
foreach (GridViewRow row in GridView1.Rows)
{
//Finding Dropdown control
DropDownList ddl1 = row.FindControl("ddlTest") as DropDownList;
if (ddl1 != null)
{
// your code here
}
}
#creby i saw your code.
you are adding your dropdown in grid view during row data bound event ok.. but now when you click on the button, all the drop down will be vanished so that's why you will not able to get drop down.
use item template of grid view and then bind dropdown in row data bound event.

Is this any way to find selected index id (selected record key value) of selected item of datalist without using SelectedIndexChanged event?

Is this any way to find selected record key value of selected item of datalist?
What is am doing is
protected void dlstSelectedImages_SelectedIndexChanged(object sender, EventArgs e)
{
int indexId = Convert.ToInt32(dlstSelectedImages.DataKeys[dlstSelectedImages.SelectedIndex]);
}
But my datalist SelectedIndexChanged is not firing (Itried with: View State="Enable", AutoEventWireup="true", AutopostBack="true" for firing the event ), so Is there anyother way to get the SelectedIndexChanged id or selected record key value
You should be able to call dlstSelectedImages.SelectedIndex at any time to get the currently selected index. It doesn't have to reside in an SelectedIndexChanged event handler. SelectedIndex is zero based and its default value is -1.
The SelectedIndexChanged event fires whenever the selected index changes (i.e. whenever SelectedIndex is assigned a new value). Typically, this would be on an ItemCommand or some other event:
void Item_Command(Object sender, DataListCommandEventArgs e)
{
// Set the SelectedIndex property to select an item in the DataList.
dlstSelectedImages.SelectedIndex = e.Item.ItemIndex;
// Rebind the data source to the DataList to refresh the control.
dlstSelectedImages.Rebind();
}

Categories