Dropdownlist values based on another dropdown list - asp.net

I have a dropdownlist control in which i have added two list items.Both these list items are added manually by me and not from any database.Based on the selection i would like to show another dropdownlist which has a sqldatasource.
I am able to accomplish this if my intial dropdownlist source is also from a database
how do i accomplish this from reading the value of the dropdownlist and populating another dropdown based on the selected value
The second dropdownlist that needs to be populated is going to vary(from differant tables) based on the selected value in the first dropdownlist
Need direction ideas please
Thanks

protected void dropdown1_SelectedIndexChanged(object sender, EventArgs e)
{
string val = dropdown1.SelectedItem.Value;
if(val == "xyz")
SqlDataSource1.SelectCommand = "Select * From Table_1";
else if(val == "abc")
SqlDataSource1.SelectCommand = "Select * From Table_2";
SqlDataSource1.DataBind();
}

Related

How to store cell value of an Asp.Net GridView row on click in a C# variable

I am having a GridView in my page , I want that when a particular row is clicked I want the cell value of that particular row.Currently I get that that value by using something like this ,
protected void SearchRecordGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "location='ModifyCustomerByCategory.aspx?ApplicationNo=" + e.Row.Cells[0].Text + "'");
}
}
Currently I am passing that value as a parameter to the page , can any body tell how can I store that value in a variable or , atleast on click I assign it to a hidden field.
Thanks for any suggestions.
Try this link.
It runs through everything you will need, and on the gridview selectedindexchanged event you can reference the value with GridView.SelectedRow.Cells[0].Text or the control containing the value Gridview.SelectedRow.FindControl("[Control Name]")
In row data bound simply save that value and use it
int appno = CInt(e.Row.Cells[0].Text)
and if you want to use this value to next page
int appno = CInt(Request.Querystring("ApplicationNo"))

ASP.net Gridview Itemtemplate Dropdownlist

I am using C# ASP.net, and relative new to it, and need to accomplish below.
In my VS2010 web application project I have webform with a Gridview which fetches the data from a table.
In the Grid, I have Commandfiled Button (column1) and Item template with Dropdownlist (column2).
Use case is, user first selects one listitem from 3 list items (H, L and M) and then selects command button.
I am not able to figure out how to extract selected listitem from a selected row
protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView2.SelectedRow;
Label4.Text = "You selected " + row.Cells[4].Text + ".";
Label5.Text = "You selected this list item from dropdownlist " + ???? ;
}
Thanks in advance.
The GridViewRow object provides the method FindControl (as do all container controls) to get access to a control in the row by its id. For example, if your DropDownList has an id of MyDropDown, you could use the following to access its selected value:
GridViewRow row = GridView2.SelectedRow;
DropDownList MyDropDown = row.FindControl("MyDropDown") as DropDownList;
string theValue = MyDropDown.SelectedValue;
// now do something with theValue
This is the recommended method for accessing all controls in your GridView. You want to avoid doing things like row.Cells[#] as much as possible, because it easily breaks when you re-arrange or add/remove columns from your GridView.

How do i use Checkboxes in gridview to show data

I Have Two Gridview in first one there is some data with checkbox. I Want when i check two or more checkboxes then show the both record who's checked to another gridview There is my Coding. Problem is it's show only one record at a time..
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
foreach(GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow )
{
CheckBox chkSelect = (CheckBox)row.Cells[0].FindControl("CheckBox1");
if (chkSelect != null)
{
if (chkSelect.Checked)
{
string FoodItem = ((Label)row.FindControl("Label1")).Text.ToString();
string s = "select * from Item where Item_Name='" + FoodItem + "' ";
db.grid(s, GridView2);
}
}
}
}
By looking your code,
first think dont bind gridview immediately after checking checked property of each check box, it will override gridview2. first collect all check box values by concatinating say FoodItem = "(value1,value2,value3)".
call data base one time by modifying query like
string s = "select * from Item where Item_Name in " + FoodItem +";
db.grid(s, GridView2);
I think you have multiple selection so keep seperate button and write all codes to copy from one grid to another grid inside button click event.

Positioning of DDL items?

i have a dropDownList which gets its data from a database. Now after the data has bound, i programmatically add another listItem to the ddl, like so:
protected void ddlEffects_DataBound(object sender, EventArgs e)
{
DropDownList ddl = dwNewItem.FindControl("ddlEffects") as DropDownList;
ddl.Items.Add(new ListItem("","0")); //Adds an empty item so the user can select it and the item doesnt provide an effect
}
It all works, but the problem is that the newly added listItem appears at the bottom of the DDL. I want it to be the first item. I tried making the value 0, but its still at the bottom. Is there any way to put it at the top?
Thanks!
Check into the ddl.Items.Insert method that will allow you to insert items at a specific location.
Like this for example:
using (Entities db = new Entities())
{
ddlDocumentTypes.DataSource = (from q in db.zDocumentTypes where (q.Active == true) select q);
ddlDocumentTypes.DataValueField = "Code";
ddlDocumentTypes.DataTextField = "Description";
ddlDocumentTypes.DataBind();
ddlDocumentTypes.Items.Insert(0, "--Select--");
ddlDocumentTypes.Items[0].Value = "0";
}
Which using EF loads the DDL with items for the database, and then inserts at position zero a new item.

ASP.net list of dropdownlists - similar to Access continuous form

What I'm looking for is a way to mimic the MS-Access style continuous form within asp.net. In one particular case, I want a control, bound to a datasource which returns a dropdownlist for each row, bound to the value within the datasource. Any change to any of the dropdownlists' would perform an update to the database instantly.
I have got halfway to achieving this using a repeater control, with the DropDownList.SelectedValue assigned within the Repeater.ItemDataBound event.
But now, supposing I add an OnSelectedIndexChanged event to the DropDownList - how would I then query the repeater to know which row I was on (to get the primary key value, for example)
I'm not sure this can be done easily.. so the question is what should I really be doing? I don't want to use a GridView that requires me to select a row to edit.. I just want to have the dropdownlists autopostback for any updates.
Hope that's clear?!
Cheers! :D
For examples sake, lets say we are binding to a custom class called Record
public class Record
{
public int Id;
public string Value;
}
If you put custom logic on the Repeater.OnItemCreated event you can attach the primary key to the id of the drop down list
protected void Repeater_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if (!(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem))
return;
var dataItem = e.Item.DataItem as Record;
if (dataItem == null) return;
var dropdown = e.Item.FindControl("myDropDown") as DropDownList;
if (dropdown == null) return;
dropdown.ID = dropdown.ID + dataItem.Id;
}
Then on the SelectedIndexChange, you can pull the id off of the dropdown that fired the event.
protected void SelectedIndexChanged(object sender, EventArgs e)
{
var dropdown = sender as DropDownList;
if (dropdown == null) return;
var stringId = dropdown.ID.Replace("myDropDown", "");
int id;
if (Int32.TryParse(stringId, out id))
{
updateRecord(id, dropdown.SelectedValue);
}
}
It's very much an ugly hack, but it should allow you to do what you want.
Easiest way to tackle this would be to mimic the Access continuous form ASP.NET style. Which would be to make a UserControl to handle the row-level UI, then put said UserControl in a repeater.

Resources