Get Dropdown Selected Value in Datalist ItemDataBound - asp.net

I am using Dropdown list inside datalist. Now, I want dropdown selected value onDatalistItemBound.
How to get it ???

Your ItemDataBound handler should look like :
protected void dl_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var myDropDownList = e.Item.FindControl("YourDropDownListID") as DropDownList;
int currentItemID = int.Parse(this.dl.DataKeys[e.Item.ItemIndex].ToString());
myDropDownList.DataSource = GetDDLDataSource(currentItemID);
myDropDownList.DataBind();
}
}

Related

dropdown disabled in table asp.net

i try to disable dropdown in table.. like when admin view any new documents and then select value from dropdown i.e aprove/reject when he select and click on button then this dropdown must be disabled and then when amdin again view new documents then in this dropdown must bt enable until admin approve/reject this document .....how i done this ..
protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DropDownList ddldrop = e.Item.FindControl("DropDownList4") as DropDownList;
HiddenField hfDepartmentId = e.Item.FindControl("hfDepartmentId") as
HiddenField;
if (ddldrop != null && hfDepartmentId != null)
{
if(hfDepartmentId.Value != string.Empty && hfDepartmentId.Value.Trim() !=
"3")
{
ddldrop.SelectedValue = hfDepartmentId.Value.Trim();
ddldrop.Enabled = false;
}
}
I assume you want to enable if the departID is "3" but you don't have an else in your if. Here is a shortened and simplified version of your approach:
protected void Repeater2_ItemDataBound(Object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DropDownList ddldrop = (DropDownList) e.Item.FindControl("DropDownList4");
HiddenField hfDepartmentId = (HiddenField) e.Item.FindControl("hfDepartmentId");
ddldrop.Enabled = hfDepartmentId.Value == "3";
if(ddldrop.Enabled) ddldrop.SelectedValue = hfDepartmentId.Value;
}
}
Note that i've checked the ListItemType which makes your null-reference check redundant. You should also use more menaingful names for your controls, i would suggest DdlDepartment instead of DropDownList4.

Get Dropdown SelectedValue using Datalist OnItemDataBound

I am trying to get Dropdownlist selected value using Datalist OnItemDataBound.
DlSubjects is main DataList and dlTests is nested DataList. dropdownlist is included with dlSubjects.
My code :
protected void dlSubjects_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataList dlTests = e.Item.FindControl("dlTests") as DataList;
DropDownList drpTopic = e.Item.FindControl("drpTopic") as DropDownList;
string Value = drpTopic.SelectedValue;
}
}
Can anyone help me regarding this ???
Im tooooooo late, but Im doing same thing right now, if Im right, then I think answer should be something like that:
protected void dlSubjects_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataList dlTests = e.Item.FindControl("dlTests") as DataList;
DropDownList drpTopic = e.Item.FindControl("drpTopic") as DropDownList;
string Value = ((DataRowView)e.Item.DataItem).Row.ItemArray[index].ToString(); //index is your dropdown index in the datalist
}
}

add new item to dropdownlist in datalist

I have a Datalist and I'm trying to find the dropdown in the datalist to add in it text in first index in ddl I tried to do that but this appeared (object reference not set ....)
Here is my code:
private DropDownList DDLProduct;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DDlProduct_DataBound(object sender, EventArgs e)
{
DDLProduct.Items.Insert(0, new ListItem("Swithch Model", "0"));
}
protected void DLProduct_ItemDataBound(object sender, DataListItemEventArgs e)
{
DDLProduct = e.Item.FindControl("DDlProduct") as DropDownList;
}
Regards
You should check the ItemDataBound event for the datalist and see if it is of type ListItemType.Item or ListItemType.AlternatingItem, otherwise you are hitting the null reference because you are on the header of the datalist:
in C#:
if ((e.item.ItemType == ListItemType.Item) | (e.item.itemType == ListItemType.AlternatingItem))
in VB.net:
if (e.Item.ItemType = ListItemType.Item) OR (e.Item.ItemType = ListItemType.AlternatingItem)
Then you want to see if you can find it:
in C#:
DropDownList d = (DropDownList) e.Item.FindControl("DDLProduct")
in vb.net
Dim d as DropDownList = CType(e.Item.FindControl("DDLProduct"), DropDownList)
Once you have found the dropdownlist box you can do:
d.Items.Insert(0, new ListItem("Switch Model", "0"));
Maybe you just need AppendDataBoundItems :-)
Try this.
protected void DLProduct_ItemDataBound(Object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
private DropDownList DDLProduct = e.Item.FindControl("DDlProduct") as DropDownList;
DDLProduct.Items.Insert(0, new ListItem("Swithch Model", "0"));
}
}
Put you code within below if statement.
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
// Your code goes here to find the drop down list.
}
You are getting the null reference exception because of header of footer rows, as this drop down is not there.
Use this, it's working:
sQuery = "select * from tbl_Ticket_Msg where us_ID=0 and t_status='Open' order by T_id asc";
ds3.Clear();
ds3 = cl.getDataSet(sQuery);
if (ds3.Tables[0].Rows.Count > 0)
{
DataList1.DataSource = ds3.Tables[0];
DataList1.DataBind();
lbltotal.Text = "Total Messages : " + ds3.Tables[0].Rows.Count.ToString();
int row = Convert.ToInt32(ds3.Tables[0].Rows.Count);
for (int i = 0; i < row; i++)
{
DropDownList ddl = (DropDownList)DataList1.Items[i].FindControl("DropDownList1");
ddl.DataSource = BindServicetoddl();
ddl.DataTextField = "name1";
ddl.DataValueField = "us_ID";
ddl.DataBind();
}
}
(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

add css or color to item when selected from datalist

I had datalist and My manager told me when user select item in datalist this item must have css or color .I did my code but it didnot work well and this error apeared ( Operator == cannot be applied to operands of type System.Web.UI.WebControls.ListItemType and System.Web.UI.WebControls.DataControlRowType )
protected void DataList3_ItemDataBound(object sender, DataListItemEventArgs e)
{
{
if (e.Item.ItemType == DataControlRowType.DataRow)
{
e.Item.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';";
e.Item.Attributes["onmouseout"] = "this.style.textDecoration='none';";
e.Item.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.DataList3, "Select$" + e.Item.ItemIndex);
}
}
}
e.Item is a DataListItem; if you check its ItemType property, you'll see that it's a ListItemType, so you should use that enumeration.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listitemtype.aspx
Something like:
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)

How to access datasource fields in an ASP.NET Repeaters ItemDataBound event?

I have a Repeater control that is being bound to the result of a Linq query.
I want to get the value of one of the datasource's fields in the ItemDataBound event, but I'm not sure how to do this.
Depending on the DataSource...
If your DataSource is a DataTable, then your DataItem contains a DataRowView:
protected void rptMyReteater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Button b = e.Item.FindControl("myButton") as Button;
DataRowView drv = e.Item.DataItem as DataRowView;
b.CommandArgument = drv.Row["ID_COLUMN_NAME"].ToString();
}
}
You can use: e.Item.DataItem.
Example: Repeater.ItemDataBound Event
// This event is raised for the header, the footer, separators, and items.
void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
{
// Execute the following logic for Items and Alternating Items.
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
if (((Evaluation)e.Item.DataItem).Rating == "Good")
{
((Label)e.Item.FindControl("RatingLabel")).Text= "<b>***Good***</b>";
}
}
}
The data that is used for the current item can be found from the EventArgs.
RepeaterItemEventArgs e
e.Item.DataItem

Resources