dropdown disabled in table asp.net - 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.

Related

How to access dynmaically added Textbox values ASP.Net

I have a gridview placed on a ASP.Net WebSite, and added a column dynamically like this:
protected void gvGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (DataControlRowType.DataRow == e.Row.RowType && e.Row.RowState != DataControlRowState.Edit &&
(e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
{
TextBox tb = new TextBox();
tb.ID = "tb";
tb.Attributes.Add("runat", "server");
int i = e.Row.Cells.Count;
i = i - 1;
e.Row.Cells[i].Controls.Add(tb);
}
}
The gridview is populated, and at the end of every row the TextBox is added. When I look at the HTML Code that is delived to the Browser, I can see that the ID of every Textbox is "gv_Items_tb_X" with X being the current row index.
Now I would like to access the content that the user types in the tb on a button click. I tried following code, but it I get an exception becuase the textbox is null.
protected void btn_UpdateCount_Click(object sender, EventArgs e)
{
OI_Data_Service.OI_Data_ServiceClient sc = new OI_Data_Service.OI_Data_ServiceClient();
foreach (GridViewRow row in gv_Items.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
TextBox tb_value = (TextBox)gv_Items.Rows[row.RowIndex].Cells[5].FindControl("gv_Items_tb_" + row.RowIndex);
sc.UpdateItemOnOpening("1", row.Cells[0].Text, tb_value.Text);
}
}
Response.Redirect("~/okay.aspx");
}
Can anyone tell my what I am doing wrong?
Thanks!

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 enable disable buttons in datalist

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

How to access the item being data bound during ItemDataBound?

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);
...

Resources