How to get EditTemplateItem in ASPxGridView? - c#

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

Related

Add dynamic ASP.NET controls via a DropDownList to the page

- Primary Info:
In my recent project, I need to have a page with a DropDownList with some items like 'firstName' , 'lastName' , 'Age' and etc. I want to add optional controls to the page when every item selected by user. For example when user select the 'Age' another dropdownlist created dynamically with these values : 'Less than 10'
'Between 10 and 30'
'more than 30'
Here is a button that add this user selection to listBox and let user to choice another options. (I made a query at last according to user choices and send it to db)
- What I do:
I create a dropDownList and set it's AutoPostBack property to true and adds some items in it and user must select one of those item. then I add user SelectedValue of dropDownList in a Cache variable before page post back happens:
protected void DropDownListColumnNameSelectedIndexChanged(object sender, EventArgs e)
{
Cache["SelectedKey"] = dropDownListColumnName.SelectedValue;
}
When user select an item from dropDownList *DropDownList_SelectedIndexChanged* fire, and I must create controls dynamically in a place holder:
var textBoxName = new TextBox
{
ID = "textBoxName",
CssClass = "str-search-textbox-highlight",
ViewStateMode = ViewStateMode.Disabled
};
placeHolderFirstItem.Controls.Add(textBoxName);
- What is the problem?
When I try add new control in current Button_Click event, control added successfully to page but I can't find it by placeHolderFirstItem.Controls.Find("textBoxName") actually placeHolderFirstItem.Controls.Count is always zero. So I can't get textBoxName.Text values.
I try to google that for any solution and I found some solution that I must add controls in Page.OnInit so I add controls in overridden OnInit(e):
protected override void OnInit(EventArgs e)
{
if (!Page.IsPostBack) return;
var textBoxName = new TextBox
{
ID = "textBoxName",
CssClass = "str-search-textbox-highlight",
ViewStateMode = ViewStateMode.Disabled
};
placeHolderFirstItem.Controls.Add(textBoxName);
}
after doing this I can find "textBoxName" in placeHolderFirstItem, but it fire before DropDownList_SelectedIndexChanged !
so how can I add new controls to place holder exactly when user change the dropDownList value and how can I read new controls value?
Thanks in advance,
Mohsen.
- Updated:
Here is the better solution
(http://forums.asp.net/p/1959726/5596531.aspx?p=True&t=635244790943067485&pagenum=1)
When you are dynamically adding controls, you have to reload the controls into the control tree everytime thereafter for it to appear. With the help of viewstate, you could change your code sample to have:
ViewState("ShowTextbox") = true
And then in your init routine:
protected override void OnInit(EventArgs e)
{
if (!Page.IsPostBack) return;
if (ViewState("ShowTextBox") == true) {
var textBoxName = new TextBox
{
ID = "textBoxName",
CssClass = "str-search-textbox-highlight",
ViewStateMode = ViewStateMode.Disabled
};
placeHolderFirstItem.Controls.Add(textBoxName);
}
}
Please note it's much easier to have a control on the control tree, and then show/hide by setting Visible to true/false, because of these ASP.NET control tree issues.

Is FirstDisplayedScrollingRowIndex property in GridView Control?

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.

Obtaining the ctl property from a gridview control in C#, ASP.Net

I am trying to obtain the ctl property that is specific to each row in a gridview. In my scenario, there is a list of titles, each title is displayed as a hyperlink using the LinkButton. When this is clicked, I would like to pass the ctl property (or what ever value is specific to that title, to the database and pull the information that is relative to the value).
I guess my first step is to obtain the ctl value. Could someone please help me get on my way. Thanks in advance.
You will need to keep track of the control ID that is written to the page in the row created event. Make a integer to increment for each row written and save it as a command argument on a link in the grid.
linkbutton CommandArgument='<%# Eval("some_id") %>'
protected void linkButton_Click(object sender, EventArgs e)
{
LinkButton linkButton = (LinkButton) sender;
if (linkButton != null)
{
if (linkButton.CommandArgument != null)
{
...some code...
}
}
}

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.

Gridview with FileUpload control

I have a gridview that shows an image as part of one of its columns. In Edit mode, I would like to let the user have the ability to upload a new image file, so I am using the FileUpload control in the edit portion of the template.
I have an event to capture this i believe:
protected void GridVew1_RowUpdated(object sender, GridViewUpdateEventArgs e)
{
if (FileUpload1.HasFile)
{
FileUpload1.SaveAs(Server.MapPath("images/hardware/" + FileUpload1.FileName));
}
}
I do not know how to call the control correctly though... How is this functionality coded?
First you need to handle the RowUpdating event instead of RowUpdated. Then you need to find a reference to the FileUpload control on that row.
IMPORTANT: You need to know the ordinal position of the column where the control is located. In my example, I set it to 0, assuming it is the first column. Otherwise, you would need to through the Cells collection to find it.
protected void gridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = gridView.Rows[e.RowIndex];
FileUpload fileUpload = row.Cells[0].FindControl("fileUpload1") as FileUpload;
if (fileUpload != null && fileUpload.HasFile)
{
fileUpload.SaveAs(Server.MapPath("images/hardware/" + fileUpload.FileName));
}
}
If I understand what you are doing here you will have to find the control in the row
so in VB some thing like this
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
Dim aRow As GridViewRow = Me.GridView1.Rows(e.RowIndex)
dim xFileUpload as fileupload = CType(aRow.FindControl("FileUpload1"), FileUpload)
xFileUpload. save file etc etc etc
End Sub
Caveat - if this is wrong I would love to see a better way to do this!

Categories

Resources