Is FirstDisplayedScrollingRowIndex property in GridView Control? - c#

I've got one long GridView control on ma website. It allows row selection. The problem is, when I scroll down this GridView and select some of the bottom rows the selection occurs, but whole GridView is scrolling back to top.
I refer this link for resolving my problem but not able to find any property into GridView control. I also search on msdn Link .
Please guide me where is this property and how can resolve my issue.Base on below checkbox event I select the row. After check the checkbox enable to delete and edit button.
protected void ChkChanged_Click(object sender, EventArgs e)
{
int count = 0;
foreach (GridViewRow gr in grdProducts.Rows)
{
CheckBox chkGrd = ((CheckBox)gr.FindControl("CheckBox2"));
ImageButton editbutton = gr.FindControl("btnEdit") as ImageButton;
ImageButton deleteButton = gr.FindControl("btnDeleted") as ImageButton;
if (chkGrd.Checked)
{
count++;
editbutton.Visible = true;
deleteButton.Visible = true;
if (count > 1)
break;
}
else
{
editbutton.Visible = false;
deleteButton.Visible = false;
}
}
if (count > 1)
{
foreach (GridViewRow gr in grdProducts.Rows)
{
CheckBox chkGrd = ((CheckBox)gr.FindControl("CheckBox2"));
ImageButton editbutton = gr.FindControl("btnEdit") as ImageButton;
ImageButton deleteButton = gr.FindControl("btnDeleted") as ImageButton;
if (chkGrd.Checked)
{
editbutton.Visible = false;
deleteButton.Visible = false;
}
}
DeleteAll.Enabled = true;
}
}

Can you give your 'row selection' code,is it posting back the entire page,If so, try to put the GridView in an UpdatePanel, so that the event is sent to the server without actually reloading the whole page.

Where are the scrollbars - to the page or to the grid-view? If possible, get rid of scrollbars to the grid-view and get scroll bars at the page level. In such case, you can use MaintainScrollPositionOnPostback which is a page property and it maintains page' scroll position on the post-back.
If your layout does not allow you to have scroll-bars at the page then I will suggest that you use a container div to the grid-view and have scroll-bars at the div level (instead of table i.e. grid-view) level. Now, you can have some JS code that can record the scroll position into the hidden field before page gets submitted and then same can use used after post-back to restore the position back. See this link where it uses such a trick : http://michaelsync.net/2006/06/30/maintain-scroll-position-of-div-using-javascript-aspnet-20

I found the answer .
In GridViewRow have Focus() method.
gr.Focus();
It's 95% set the position in your gridview selected Row.

Related

How do I edit a button in a grid with only knowing the Grid Row & Column?

I have a grid with some buttons. And basically want to edit the IsEnabled state of a Button in a specific Grid Row + Column. I don't get how to do this e.g.
public void Disablebutton (int Column, int Row)
{
//disable the button at Grid Row = Row and Grid Column = Column
}
Can someone provide an example how to solve given problem?
EDIT:
I found a temporary solution... Well, it's working, but it's very bad
if("B" + x.ToString()+ y.ToString() == "B00")
{
B00.IsEnabled = false; //B00 is the button name f.e.
}
I don't know if you're using WPF, Windows Forms or anything else, but you should have a Cell in the datagrid that you access using the row and col values. That cell has the button control that you can cast to a variable to change its properties. Something like this:
Button button = (Button)Grid.Cell[Column][Row].Control;
button.IsEnabled = false;
or
Button button = Grid.Cell[Column][Row].Control as Button
button.IsEnabled = false;
Keep in mind that this is a very rough example, since I don't know what technology you're using to paint the interface.

Gridview Label Appear in a Selected Row

I has a Gridview called Gridview1 which is binded to a datasource.
and I have a ItemTemplate at Column 5 within it contain a
- Linkbutton called search
- Label called lbl_password , visible press to true
- Label called txt_password , visible set to false
code in done in asp.net webForm , .aspx
when link button pressed this function will get the ID at cell 1 in the row tat button clicked
GridViewRow grdrow = (GridViewRow)((LinkButton)sender).NamingContainer;
string fieldID = grdrow.Cells[1].Text;
Now i want to know how can I make the txt and lbl become visible when the button pressed
I am assuming you mean when link button pressed you want to make the text and label from the same row to be visible.For that purpose you can just use the findcontrol method on gridview row to find the control you need.
If that's what you want then in link button click event you can do as follow,
protected void lnkButton_Click(object sender, EventArgs e)
{
GridViewRow grdrow = (GridViewRow)((LinkButton)sender).NamingContainer;
((TextBox)grdrow.FindControl("yourtextboxID")).Visible = true;
((Label)grdrow.FindControl("yourLabelID")).Visible = true;
}
Here is more info on findcontrol.

How to get EditTemplateItem in ASPxGridView?

I'm using ASPxUploadControl inside EditItemTemplate in ASPxGridView. When I click on edit row button the ASPxUploadControl is shown, if not in edit mode it acts as a hyperlink column and show the download file option. The issue I'm facing is that I'm not getting the control object in the Insertion and Updation event of ASPxGridView.
I' doing something like this
ASPxUploadControl = grid.FindEditRowCellTemplate(grid.Columns[0] as GridViewDataColumn, "upload_control_id") as ASPxUploadControl;
I've also tried grid.FindControl() function.
Try this for Edit Refer This
protected void ASPxUploadControl1_FilesUploadComplete(object sender, DevExpress.Web.ASPxUploadControl.FilesUploadCompleteEventArgs e){
// Intentionally pauses server-side processing to demonstrate the Loading Panel or Progress Panel functionality
System.Threading.Thread.Sleep(2000);
ASPxUploadControl uploadControl = sender as ASPxUploadControl;
if (uploadControl.UploadedFiles != null && uploadControl.UploadedFiles.Length > 0){
for (int i = 0; i < uploadControl.UploadedFiles.Length; i++){
UploadedFile file = uploadControl.UploadedFiles[i];
if (file.FileName != ""){
string fileName = string.Format("{0}{1}", MapPath("~/Images/"), file.FileName);
//file.SaveAs(fileName, true);//OnLine Demo Restriction
}
}
}
}
On Update or Insert event, get the GridView to find the get it's template control.
ASPxGridView gridView = sender as ASPxGridView;
ASPxUploadControl control = gridView.FindEditRowCellTemplateControl(gridView.Columns[0] as GridViewDataColumn, "upload_control_id") as ASPxUploadControl;
References:
find control update edititemtemplate
ASPxGridView - How to find a control inside the EditItemTemplate
ASPxGridView - How to find a control in EditItemTemplate
How to programmatically reach any AspxControl inside an AspXGridView's EditItemTemplate
ASPxGridView Find control (Checkbox) and Check if it is checked or not
example code snippet:
Protected Sub grid_RowInserting(ByVal sender As Object, ByVal e As DevExpress.Web.Data.ASPxDataInsertingEventArgs) Handles grdProyectos.RowInserting
Dim grid As ASPxGridView = (TryCast(sender, ASPxGridView))
Dim chk As CheckBox= grid.FindEditRowCellTemplateControl(grid.Columns("name_colum"), "nameCheckBox")
Dim marcada as Boolean = chk.Checked
End Sub

How to fire OnSelectedIndexChanged of GridView using c# code

I have 2 grids, grid1 and grid2.
grid2 will be filled based on which row is clicked in grid1. I have done it by binding OnSelectedIndexChanged of grid1.
But at page load the grid2 will be empty as no row selection is made.
So I was planning of firing the row selection of grid1 using c# code so that both grids will be having data at page load.
I have started coding like.
grid1.DataSource = versions.DefaultView;
grid1.SelectedIndex = 0;
grid1.DataBind();
But the event is not firing.
Can anyone help me in solving this issue?
You can just call the method programmatically.
grid1.DataSource = versions.DefaultView;
grid1.SelectedIndex = 0;
grid1.DataBind();
grid1_SelectedIndexChanged(grid1, new EventArgs());
You don't need the event on Page_Load because you already know what the selected index of the first grid should be. The event is needed after user interaction with the page. Just DataBind() the second grid on Page_Load which data corresponding to the 0 selected index of the first grid the same way that you databind the first grid.
if (!IsPostBack)
{
grid1.DataSource = versions.DefaultView;
grid1.SelectedIndex = 0;
grid1.DataBind();
DataBindGridByIndex(0);
}
else
{
grid1.DataSource = versions.DefaultView;
grid1.DataBind();
}
public void DataBindGridByIndex(int index)
{
// Logic to databind second grid by selected index.
}

How and WHEN to save the values of the "Checked" attribute for Dynamic checkboxes

I am adding some checkboxes dynamically during runtime, and I need to know whether they are checked or not when I reload them next time.
I load the checkbox values from a list stored in ViewState.
The question is: when do I save or check for the value of the the Checked?
I tried the event dispose for the check box and the place holder I am adding the checkboxes in, but it wasn't fired. i.e. when I put a break point it didn't stop. So any suggestions?
This is a sample code, but I don't think it is necessary:
void LoadKeywords()
{
bool add = true;
foreach (string s in (ViewState["keywords"] as List<string>))
if (s == ddlKeywords.SelectedItem.Text)
{
add = false;
continue;
}
if (add)
(ViewState["keywords"] as List<string>).Add(ddlKeywords.SelectedItem.Text);
foreach (string s in (ViewState["keywords"] as List<string>))
{
CheckBox kw = new CheckBox();
kw.Disposed += new EventHandler(kw_Disposed);
kw.Text = s;
PlaceHolderKeywords.Controls.Add(kw);
}
}
If you are dynamically adding controls at run time you have to make sure that those controls are populated to the page's Control collection before ViewState is loaded. This is so that the state of each checkbox can be rehydrated from Viewstate. The Page Load event, for example, is too late.
Typically you would dynamically add your CheckBox controls during the Init Event (before view state is loaded) and then Read the values in your Checkbox controls during the Load event (after view state is loaded).
eg:
protected override void OnInit(EventArgs e)
{
//load the controls before ViewState is loaded
base.OnInit(e);
for (int i = 0; i < 3; i++)
{
CheckBox cb = new CheckBox();
cb = new CheckBox();
cb.ID = "KeyWord" + i.ToString();
cb.Text = "Key Word"
MyPlaceHolder.Controls.Add(new CheckBox());
}
}
//this could also be a button click event perhaps?
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (Page.IsPostBack)
{
//read the checkbox values
foreach(CheckBox control in MyPlaceHolder.Controls)
{
bool isChecked = control.Checked;
string keyword = control.Text;
//do something with these two values
}
}
}
Hope that helps
****EDIT****
Forgot to mention that this is obviously just demo code - you would need to flesh it out.
For more information on dynaic control rendering in ASP.Net check out this article on 4Guys.
For more information on the page life-cycle in ASP.Net check out MSDN.
How to:
try adding a javascript code, that handles checked(),
u can get the checkboxes by using document.findElementById(ID) , then store the checkboxe's value into a hiddenfield that has a runat="server" property.
When to:
either on pageload , check if page is postback(), and check the hiddenfield(s) value(S). or add a submit button (and place its event in the code behind, runat="server" property).
hope this helps u.

Categories

Resources