I have got a datalist with some databound fields with 2 buttons. I want to enable disable button depending on the column(state) value of each row, so for instance if the value of state is 0 , the remove button should be disable and add button should be enabled similarly when the value of state is 1 , vice versa..
protected void dlEditCaravans_ItemDataBound(object sender, DataListItemEventArgs e)
{
Button addtoFeauture = e.Item.FindControl("btnAddToFeature") as Button;
Button removetoFeauture = e.Item.FindControl("btnRemoveFeature") as Button;
int id = Convert.ToInt32(dlEditCaravans.DataKeys[e.Item.ItemIndex]);
int check = caravans.GetfeautureValue(id);
if (check == 0)
{
addtoFeauture.Enabled = true;
}
else
{
removetoFeauture.Enabled = true;
}
}
I have tried something like above but it gives object reference not set to an instance error.
You need to use ItemDatabound event of Datalist
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
DataRow dr = ((DataRowView)e.Item.DataItem).Row;
if (Convert.ToBoolean(dr["StateColumnName"])
{
((Button)e.Item.FindControl("Button1")).Enable = True;
}
}
}
void DataListProduct_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
Button BT = e.Item.FindControl(“ButtonID“) as Button;
BT.Enable = True or false depends upon your condition
}
}
Related
I would like to check the previous row data if it is equal to --,
if it is not equal to -- then I would enable button in the next row
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (DataBinder.Eval(e.Row.DataItem, "time_start").ToString() == "--")
{
Button btn = ((Button)e.Row.FindControl("Edit_Button"));
btn.Enabled = false;
}
}
}
You can also do it like this using GridView1.Rows[e.Row.RowIndex - 1].
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridViewRow prevrow = GridView1.Rows[e.Row.RowIndex - 1];
if( prevrow.RowType == DataControlRowType.DataRow)
{
// Your code for manipulating prevrow
}
if (DataBinder.Eval(e.Row.DataItem, "time_start").ToString() == "--")
{
Button btn = ((Button)e.Row.FindControl("Edit_Button"));
btn.Enabled = false;
}
}
}
One way to do this is:
Create a field previousRow in your class, of type GridViewRow.
Initialize this field to null in the a GridView.DataBinding event handler. This event is fired when databinding starts, before any of the RowDataBound events fires.
In your GridView.RowDataBound event handler, do your processing (including comparing with previousRow), then set previousRow = e.Row.
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.
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();
}
}
public void Repeater1_ItemDatabound(Object Sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
if (e.Item.ItemIndex == 0)
{
Button b = e.Item.FindControl("btnmoveup") as Button;
b.Enabled = false;
}
DataView view = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
DataTable result = view.ToTable();
if (e.Item.ItemIndex == (result.Rows.Count)-1)
{
Button b2 = e.Item.FindControl("btnmovedown") as Button;
b2.Enabled = false;
}
}
}
I want to Grey out the first and the last buttons in my asp repeater. The first Button is disabled but the last is not. Can someone point out my error.?
I want to get at the item that is being data bound, during the ItemDataBound event of an asp:repeater.
I tried the following (which was an unaccepted answer in a stackoverflow question):
protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Object dataItem = e.Item.DataItem;
...
}
but e.Item.DataItem is null.
How can I access the item being data bound during the event called ItemDataBound. I assume the event ItemDataBound happens when an item is being data bound.
I want to get at the object so I can take steps to control how it is displayed, in addition the object may have additional helpful properties to let me enrich how it is displayed.
Answer
Tool had the right answer. The answer is that e.Item.Data is only valid when e.Item.ItemType is (Item, AlternatingItem). Other times it is not valid. In my case, I was receiving ItemDataBound events during header (or footer) rows, where there is no DataItem:
protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
// if the data bound item is an item or alternating item (not the header etc)
if (e.Item.ItemType != ListItemType.Item &&
e.Item.ItemType != ListItemType.AlternatingItem)
{
return;
}
Object dataItem = e.Item.DataItem;
...
}
Right off the bat I would have to guess you need this:
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
//Put stuff here
}
After all, the item itself could be representing a header or footer row.
I just wanted to add that I did accomplished this by doing the following:
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
//determine if the row type is an Item
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
DataRowView row = (DataRowView)e.Item.DataItem;
if (row["RowName"].ToString() == "value")
{
//INSERT CODE HERE
}
}
}
For repeater
if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType...
Can be modified to:
if (e.Item.DataItem != null) ...
Use dynamic
dynamic item = e.Item.DataItem;
string style = item.Style;
If you're dealing with an asp:ListView, you can do something like this:
protected void myLV_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType != ListViewItemType.DataItem)
return;
object dataItem = ((ListViewDataItem)e.Item).DataItem;
}
(The title of the question doesn't mention an asp:repeater.. so I thought it might be helpful to post the code for an asp:listview)
For a repeater with a custom template binding; you can use the following template. I used this to create a table which breaks down each data item into two rows for print view.
Repeater1.HeaderTemplate = new PrintTemplate(ListItemType.Header);
Repeater1.ItemTemplate = new PrintTemplate(ListItemType.Item);
Repeater1.AlternatingItemTemplate = new PrintTemplate(ListItemType.AlternatingItem);
Repeater1.FooterTemplate = new PrintTemplate(ListItemType.Footer);
public class PrintTemplate : ITemplate
{
ListItemType templateType;
public PrintTemplate(ListItemType type)
{
templateType = type;
}
public void InstantiateIn(System.Web.UI.Control container)
{
Literal lc = new Literal();
switch(templateType)
{
case ListItemType.Header:
lc.Text = "<TABLE>";
container.Controls.Add(lc);
break;
case ListItemType.Item:
case ListItemType.AlternatingItem:
//lc.Text = "<TR><TD>";
lc.DataBinding += new EventHandler(TemplateControl_DataBinding);
container.Controls.Add(lc);
break;
case ListItemType.Footer:
lc.Text = "</TABLE>";
container.Controls.Add(lc);
break;
}
}
private void TemplateControl_DataBinding(object sender,
System.EventArgs e)
{
Literal lc;
lc = (Literal)sender;
RepeaterItem container = (RepeaterItem)lc.NamingContainer;
ListItemType itmType = container.ItemType;
//construct the repeater row using a custom function that switches on item type; HEADER vs ITEM vs ALTERNATINGITEM
lc.Text += GetPopulatedRepeaterRow(dataInterface, container.DataItem, container.ItemType);
...