ASP.NET GridView EditTemplate and find control - c#

In the GridView we are using an edit button. Once the edit button is clicked Controls in the edit template will display in the same row with update button. That row has two dropdownlist controls.
Process flow:
controls:d1 and d2
d1 is using sqldatasource for item display : working fine.
d2 is using codebehind code to load the item based on the selected value in the d1 : Not working
How to find the control in the edit template to display item value for d2?

I got the answer.
protected void GridView1_PreRender(object sender, EventArgs e)
{
if (this.GridView1.EditIndex != -1)
{
Button b = GridView1.Rows[GridView1.EditIndex].FindControl("Button1") as Button;
if (b != null)
{
//do something
}
}
}

When you switch to the edit mode, you need to rebind the grid for this to take effect.
So, you can use the 'RowDataBound' event.
void MyGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow
&& e.Row.RowIndex == MyGridView.EditIndex)
{
DropDownList d1 = e.Row.FindControl("d1") as DropDownList;
if(d1 == null) return;
//Now you have the drop down. Use it as you wish.
}
}

Related

How to select checkbox when click on any cell in the table except clicking on checkbox icon? DevExpress

I have a C# devexpress project. I am using the checkbox for my project. Here is the screenshot of my design.
I want to click the "In Price" and "Out Price" cell, and select the rows (multiple. To be exact: I have a number of rows. If I click a cell, the cell will be selected, if I click another row cell, the first-row will still remain selected, and then the second one also be selected.)
Here is a demo of what I exactly expecting:
I am assuming that, because of the Toggle button, only if I click on the checkbox column, it is selected. If I need to turn it off, How can I do that?
If I need to write code what I am expecting, the code will be written:
private void gridView2_RowCellClick(object sender, RowCellClickEventArgs e)
{
if (e.Column.FieldName == "InPrice" || e.Column.FieldName == "OutPrice")
{
//Here I need to write the code.
}
}
private void gridView2_RowCellClick(object sender, RowCellClickEventArgs e)
{
if (e.Column.FieldName == "InPrice" || e.Column.FieldName == "OutPrice")
{
if ((Control.ModifierKeys & Keys.Control) != Keys.Control)
{
GridView view = sender as GridView;
GridHitInfo hi = view.CalcHitInfo(e.Location);
if(hi.InRowCell)
{
view.FocusedRowHandle = hi.RowHandle;
view.FocusedColumn = hi.Column;
view.ShowEditor();
CheckEdit edit = (view.ActiveEditor as CheckEdit);
if(edit != null)
{
edit.Toggle();
(e as DevExpress.Utils.DXMouseEventArgs).Handled = true;
}
}
}
}
}
This code works what I exactly wanted. If anyone need this, it may help you.

trigger an eventhandler on postback c#

I am pretty new to c# programming.
I have an issue that follows:
I have a GridView on a web form, a DropDownList and a Label control. When i select a value from a DDL a Grid View is populated with rows from the database that equal the DDL condition(in my case DDL represent Countries, GV lists the Cities).
When i delete the last City from a GV using a built in GV Delete function i would like to automatically write in a Label that there are no more Cities in selected Country.
How can i achieve that?
I tried to put
protected void GridView1_RowDeleted1(object sender, GridViewDeletedEventArgs e)
{
if (GridView1.Rows.Count == 0)
{
LabelGrid.Text = "No more cities.";
}
}
but it didn't work.
Thanks for your help
Consider using PreRender event, when that event runs the Row.Count property contains the correct value. (decremented after delete)
protected void GridView1_PreRender(object sender, EventArgs e)
{
if (GridView1.Rows.Count == 0)
{
LabelGrid.Text = "No more cities.";
}
}

Hide DataList row based on value from web service

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

Why do I receive old data in my gridview on page load?

The process I am doing is if lbl.Text is "Validated" then disable the checkbox accordingly in grid. The code works fine if paging is not there.
Now the problem is I am using paging and when I click to the next page of grid
the validated things appear with the checkbox enabled.
I checked through breakpoints. Its loading the previous gridpage values during page load event. And after pageloadevent its going design and loads the new values in grid.
//-------loading previous page values of grid here---------
protected void Page_Load(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
Label lbl = (Label)row.FindControl("Labely8");
Label Label23 = (Label)row.FindControl("Label23");
CheckBox checkbox = (CheckBox)row.FindControl("chkRows");
if (lbl.Text == "Validated")
{
checkbox.Enabled = false;
}
else
{
checkbox.Enabled = true;
}
}
}
I think you need to do enable or disable each of the checkboxes individually in the GridView.RowDataBound event rather than all at once in the Page_Load event:
void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox checkbox = (CheckBox)e.Row.FindControl("chkRows");
checkbox.Enabled = e.Row.Cells["nameOfCellWithLabel"].Text == "Validated";
}
}

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

Categories

Resources