Hide DataList row based on value from web service - c#

I am retrieving a set of value from a web service and populating a dataList with those values-
ActiveDataList.DataSource = ws.TermsReturnActive(sql);
ActiveDataList.DataBind();
How can I hide a particular column of the dataList depending on the value,
e.g.
if(value == 1)
{
//Hide Column
}
This action however would have to hide the same row of another dataList in parallel to it.
I can modify a cell on this second dataList using by retrieving the value from the first as so -
TextBox tb1 = (TextBox)sender;
DataListItem item1 = (DataListItem)tb1.NamingContainer;
TextBox txt1 = (TextBox)tData.Items[item1.ItemIndex].FindControl("tTextBox");
string term = txt1.Text;
So if I can retrieve a value from a separate dataList row, I was thinking I would also be able to adjust its visability.
How can I achieve this as the web service call is done in the page load so I believe it would have to be done when the dataList item is bound?

if iam right, you should have something like this in your aspx-file right?:
<asp:DataList ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound"></asp:DataList>
As you can see you should add a "OnItemDataBound" Event where you can check your value and hide a item, if you want.
So you can react like this and hide some items:
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
TextBox tbCurrentTextBox = (TextBox)e.Item.FindControl("tTextBox");
if (DataList1.Items[e.Item.ItemIndex].ToString() == "1")
{
e.Item.Visible = false;
}
}
}

Related

onRowdatabound not giving label value in asp.net

I am creating the dynamic grid and adding textbox and label in row dynamically on some button click and bind some data from the database to textbox and label.I want to change the label text color and text box read only for condition depend on database.i have used onRowDataBound event of gridview but not getting any value in the textbox and label for a row. Can anyone helps me to solve this issue? Thanks
protected void grdMasterData_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label status = e.Row.FindControl("lblProduct") as Label;
if (status.Text == "LY Actuals")
{
e.Row.Cells[0].CssClass = "lblProductColor";
// lbtAction.Visible = false;
}
}
}
In RowDataBound three row types provided by grid, you are using only DataRow, try to get your values from empty row and footer row because first record will not come in DataRow.

How to select a row in a DataGridView programmatically AND trigger DataGridView.SelectionChanged event?

For the life of me I cannot seem to figure this out. I have a long DataGridView (that does not allow MultiSelect) and when a user commits a change to the data, the data from the grid is purged and redrawn (because changes can affect multiple rows, this was the simpler approach). However, when I try to select the row programmatically, it does not also fire the DataGridView.SelectionChanged event, which I use to display data from an array which is correlated to the DataGridView current cell index. When doMagicStuff executes, the values for the wrong index (specifically, index 0) is show.
private void doMagicStuff()
{
int selRow = myDGV.CurrentCell.RowIndex;
myDGV.Rows.Clear();
/*Perform Task, Redraw data*/
myDGV.CurrentCell = myDGV[selRow, 0];
}
private void myDGV_SelectionChanged(object sender, EventArgs e)
{
Label1.Text = myDisplayValue1[myDGV.CurrentCell.RowIndex];
Label2.Text = myDisplayValue2[myDGV.CurrentCell.RowIndex];
TextBox1.Text = myEditValue1[myDGV.CurrentCell.RowIndex];
TextBox2.Text = myEditValue2[myDGV.CurrentCell.RowIndex];
}
Make sure that your client settings and OnSelectedIndexChanged is set like so: (ASP.NET AJAX)
.aspx page
<telerik:RadGrid ID="Grid1" runat="server" OnSelectedIndexChanged="Grid1_SelectedIndexChanged" OnItemDataBound="Grid1_ItemDataBound" OnPreRender="Grid1_PreRender">
<ClientSettings EnablePostBackOnRowClick="true">
<Selecting AllowRowSelect="true"></Selecting>
</ClientSettings>
</telerik:RadGrid>
aspx.cs page
protected void Grid1_SelectedIndexChanged(object sender, EventArgs e)
{
string value = null;
foreach(GridDataItem item in Grid1.SelectedItems)
{
//column name is in doub quotes
value = item["Name"].Text;
}
}
Add a button click to the form to test the selected values in the DataGridView.. double click that button then paste this code in there
foreach (DataGridViewRow row in myDGV.SelectedRows)
{
Label1.Text = //This should be hard coded the only thing that should change dynamically is the TextBox Values
Label2.Text = //This should be hard coded the only thing that should change dynamically is the TextBox Values
TextBox1.Text = row.Cells[0].Value.ToString();//change the 0 or 1 to fit your column Index position
TextBox2.Text = row.Cells[2].Value.ToString();
}
also if you have 4 columns and 4 text boxes then you will assign all of the textbox.Text values within the foreach loop just follow the pattern and increase the index by 1 so 2 textboxes means row.Cells[0] is the first column row.Cells[1] is the second column ...etc

How to Reload the page after Datalist Databind

Asp.net with c#
i am using 16 datalist asp.net controls in my page I have programatically defined each updated command as below:
protected void DataList1_UpdateCommand(object source,
DataListCommandEventArgs e)
{
//This codes repeats for all 16 datalist , diferent naming controls such as DataList2, Datalist3, etc.....
String categoryID = DataList1.DataKeys[e.Item.ItemIndex].ToString();
String dp_status = ((DropDownList)e.Item.FindControl("DropDownList1")).SelectedValue;
//String txt_dept =
SqlDataSource1.UpdateParameters["Id"].DefaultValue = categoryID;
SqlDataSource1.UpdateParameters["Status"].DefaultValue = dp_status;
// SqlDataSource1.UpdateParameters["Comment"].DefaultValue = ;
// SqlDataSource1.UpdateParameters["Census"].DefaultValue = ;
SqlDataSource1.Update();
DataList1.EditItemIndex = -1;
DataList1.DataBind();
}
protected void DataList_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView drv = (DataRowView)(e.Item.DataItem);
string status = drv.Row["Status"].ToString();
if (status == "Available")
e.Item.BackColor = System.Drawing.Color.LightGreen;
if (status == "Assigned")
e.Item.BackColor = System.Drawing.Color.LightSteelBlue;
if (status == "BR")
e.Item.BackColor = System.Drawing.Color.LightSalmon;
}
}//End Nt1 Change Colors
The problem is that once I update a datalist I loose the color change in my other Datalist controls....
Is there any way I can have all datalist rebind? or page reload after updating a record in any datalist?
If i insert new row / update exciting row, delete a row means automatically the gridview have to refresh. what i have to do//?....
During a postback the ItemDataBound event does not re-fire if .DataBind() is not being called. When your items are loaded from viewstate/controlstate no ItemDataBound results in no coloration.
At first cut I would remove your Item_DataBound as you have it and create a routine that iterates through each of the lists and sets the desired color. I would invoke this on pre-render.
Another choice would be to use jquery/javascript on the client once the page is loaded to do basically the same thing using styles and css.
You might get the result you want wrapping each list in an ajax 'update panel' but that will bloat your page and cost you on performance.
You can put that DataList section of your page inside an update panel like:
<asp:ScriptManager runat="server"/>
<asp:UpdatePanel runat="server">
<contentTemplate>
<!-- Your content Here -->
</contentTemplate>
</asp:UpdatePanel>
This should definitely solve your problem.

Asp.net Gridview - Why Are DataRowBound changes Lost on sort?

I am making conditional formatting changes to the data in my gridview using a RowDataBound event:
void gvReg_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DateTime lastUpdate DateTime.Parse(DataBinder.Eval (e.Row.DataItem, "LAST_UPDATE");
if (lastUpdate < DateTime.Today.AddMonths(-1))
{
Hyperlink hypLastUpdate = (Hyperlink)e.Row.FindControl("hypLastUpdate";
hypLastUpdate.CssClass = "Error";
hypLastUpdate.NavigateUrl = "http://www.someExampleErrorPage.com";
}
}
}
This works, and sets the proper CssClass to the hyperlink (which makes it a jarring shade of bold red), but once the gridview is sorted (via the user clicking a column heading) the css class is reset on hypLastUpdate and it loses both it's style and associated NavigateUrl property.
The control hypLastUpdate is contained in a template field in a gridview, and it's text value is databound to a field called "LAST_UPDATE".
Is this a planned behavior (is sorting supposed to break the conditional formatting done in RowDataBound events?) or is there something I can check to make sure I am not doing something incorrectly?
I am not using the DataBind method anywhere in the code behind, and viewstate is turned on for the gridview in question.
--EDIT--
It ended up being a mistake in event handling.
I was doing:
gvReg.Sorted += {SomeEventHandler}
Inside of the page load event, but only when it wasn't a postback. This function called gvReg.DataBind after the grid view was sorted. I removed the handler wire up and instead added the event handler function to the OnSorted event. I guess assigned delegates to a gridview are not saved in ViewState between callbacks?
Hi here is a quick example of what I meant on my comment. This is the only way i could think of it:
protected void gvReg_Sorting(object sender, GridViewSortEventArgs e)
{
GridView gridView = (GridView)sender;
if (e.SortExpression.Length > 0)
{
foreach (DataControlField field in gridView.Columns)
{
if (field.SortExpression == e.SortExpression)
{
cellIndex = gridView.Columns.IndexOf(field);
break;
}
}
if (pSortExpression != e.SortExpression)
{
pSortDirection = SortDirection.Ascending;
}
else
{
pSortDirection = (pSortDirection == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending);
}
pSortExpression = e.SortExpression;
}
//Retrieve the table from the database
pSortOrder = pSortDirection == SortDirection.Ascending ? "ASC" : "DESC";
List<Partners> partnerList = GetPartnerList();
gvReg.DataSource = partnerList;
gvReg.DataBind();
}

How do I change a ListView dynamically on DataBound?

I have a ListView with a template, it puts a bunch of data in, like X Y Z.
I want to hide show some columns based on criteria, so I have ItemDataBound event, but I don't know how to get the actual listview row so I can do things to do it.
Any ideas?
You can access the ListViewItemEventArgs' Item property to get at the current item (the one being bound to data).
The sample code below (which shows how to customize a ListView item in the ItemDataBound event) was taken from the MSDN documentation:
protected void ContactsListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
Label EmailAddressLabel;
if (e.Item.ItemType == ListViewItemType.DataItem)
{
// Display the e-mail address in italics.
EmailAddressLabel = (Label)e.Item.FindControl("EmailAddressLabel");
EmailAddressLabel.Font.Italic = true;
System.Data.DataRowView rowView = e.Item.DataItem as System.Data.DataRowView;
string currentEmailAddress = rowView["EmailAddress"].ToString();
if (currentEmailAddress == "orlando0#adventure-works.com")
{
EmailAddressLabel.Font.Bold = true;
}
}
}

Categories

Resources