add new item to dropdownlist in datalist - asp.net

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)

Related

Get Dropdown Selected Value in Datalist ItemDataBound

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();
}
}

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
}
}

how to retain viewstate for the dynamic controls created in GRIDVIEW

i am creating dynamic textbox onRowCreated event in gridview control, however when i try to findcontrol i get null
here is how i am dong...
protected void gvORg_RowCreated(object sender, GridViewRowEventArgs e)
{
if ((e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)) || (e.Row.RowState == DataControlRowState.Edit))
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
txBox txtReg = new TextBox();
txtReg.ID = "_registration" + e.Row.RowIndex + rowId.ToString();
txtReg.Text = reg.RegistrationToken;
e.Row.Cells[7].Controls.Add(txtReg);
}
}
}
protected void gvOrg_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
.....
....
TextBox _registration1 = gvOrg.Rows[e.RowIndex].Cells[7].FindControl("_registration" + e.RowIndex + rowId) as TextBox;
}
Have you tried to find it by:
GridView gv = (GridView)sender;
GridViewRow gvr = (GridViewRow)gv.Rows[e.RowIndex];
(TextBox)gvr.FindControl("_registration" + e.Row.RowIndex + "_" + reg.RegistrationId.ToString())
i able to fix my problem here

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