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!
Related
I have 2 gridviews which update some data, they are independent each one,
both are using "onrowupdating="actualizar"
i want to use the same event like below:
protected void actualizar(object sender, GridViewUpdateEventArgs
e)
{ }
How can i select the specific grid view inside the event?
The object sender will be the GridView who started the event so you can cast it to GridView and you will have it's properties like its Id.
Example:
(sender as GridView).ID
Thats the answer !!
Control cntrl = sender as GridView;
var gridId = cntrl.ID;
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
I'm trying to get a Gridview "select" link to drill down to a specific page for the selected row in ASP.NET with C#. This gridview is generated dynamically in the page_load, and is databound from a fairly simple SQL select query. Each row has a unique id, and I would like this to be passed as a parameter in the url when the select button is clicked for that row. So, when you click the "select" button on the row with an id value of 9 (note - not the id as defined by the gridview, but the one from the SQL query) you are redirected to an address such as moreDetail.aspx?id=9.
However, when trying to pass the id into the event handler I've hit issues... GridView.SelectedIndexChanging takes the usual (object sender, EventArgs e) as parameters and nothing else, and since the Gridview is created at Page_Load the EventArgs class is useless. I can't seem to find any way to pass the id that I have retrieved earlier into the event handler.
After lots of searching,I tried creating a class that extends EventArgs (obviously with my extra parameter added in), but it seems using any parameters other than (object sender, EventArgs e) just won't work. I could theoretically redo the SQL query within the event handler, but that seems to me a terrible way to achieve what I'm looking for, so I'm hoping someone will be able to see what I've got wrong here, because I'm sure I'm missing something obvious.
Some code - grid.SelectedRow.Cells[0] will contain the parameter I want to pass:
In Page_Load:
GridView grid = new GridView();
grid.DataSource = source;
CommandField selectField = new CommandField();
selectField.ShowSelectButton = true;
selectField.SelectText = "View Jobs";
grid.Columns.Add(selectField);
grid.SelectedIndexChanging += grid_SelectedIndexChanging;
grid.DataBind();
content.Controls.Add(grid);
And the Event Handler:
protected void grid_SelectedIndexChanging(object sender, EventArgs e)
{
Response.Redirect("ViewCustomer.aspx?id=" + grid.SelectedRow.Cells[0]);
}
Obviously this doesn't work because the scope of grid doesn't extend to the handler...but how can I get access to that data?
You need to cast sender to GridView to reference your calling GridView:
protected void grid_SelectedIndexChanging(object sender, EventArgs e)
{
GridView grid = (GridView)sender;
Response.Redirect("ViewCustomer.aspx?id=" + grid.SelectedRow.Cells[0]);
}
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...
}
}
}
i have a alphabetic filter consist of 26 dynamically created link button on selecting any link button it is filtering the name of user's on the basis of alphabet and changing its color to orange to make it different from other linkbuttons it is working fine but if there are more number of user associated with a particular alphabet and on applying filter it is filtering the user on the basis of that alphabet and showing them in a list view on clicking the data pager next page or any other page number the link button changes its color to default color but i want to keep that highlighted until and unless other link button is selected
my code
protected void Page_Init(object sender, EventArgs e)
{
// Adding Dynamically linkbuttons for all alphabets(i.e. A-Z)
for (char asciiValue = 'A'; asciiValue <= 'Z'; asciiValue++)
{
LinkButton lbtnCharacter = new LinkButton();
lbtnCharacter.ID = "lbtnCharacter" + asciiValue;
divAlphabets.Controls.Add(lbtnCharacter);
// Setting the properties of dynamically created Linkbutton.
lbtnCharacter.Text = Convert.ToString(asciiValue);
lbtnCharacter.CssClass = "firstCharacter";
lbtnCharacter.ToolTip = "Show Tags starting with '" + Convert.ToString(asciiValue) + "'";
lbtnCharacter.CommandArgument = Convert.ToString(asciiValue);
lbtnCharacter.Command += new CommandEventHandler(lbtnCharacter_Command);
}
}
// For assigning default color to linkbutton text in page load
foreach (var ctrl in divAlphabets.Controls)
{
if (ctrl is LinkButton)
((LinkButton)ctrl).CssClass = "firstCharacter";
}
void lbtnCharacter_Command(object sender, CommandEventArgs e)
{
// Storing the values of pressed alphabet in viewstate.
ViewState["Selected_Character"] = e.CommandArgument;
LinkButton lbtnSelected = (LinkButton)divAlphabets.FindControl("lbtnCharacter" + e.CommandArgument);
lbtnSelected.CssClass = "firstCharacter highlighted";
txtTagFilter.Text = string.Empty;
BindTagList();
}
I hope I understood your question.
You are setting your Selected_Character item in the command handler and then setting the class of the button to highlight it. This only gets fired when the button is clicked, not when you move to the next page. Why not separate these two operations. Set the class of the link button on prerender if the Selected_Character matches. That way even when you page the link button will stay highlighted.
I would also set your selected character as a query string parameter, if someone copies and pastes a link to your page the button would not highlight and the correct data would not display.
Hope this helps.
Edit: Haven't tested the below but maybe it will get you started.
void lbtnCharacter_Command(object sender, CommandEventArgs e)
{
// redirect to self with tag as qs parameter
Response.Redirect(string.Format("{0}?tag={1}", Request.Url.GetLeftPart(UriPartial.Path), e.CommandArgument));
}
protected void Page_PreRender(object sender, EventArgs e)
{
if (Request.QueryString["tag"] != null) {
LinkButton lbtnSelected = (LinkButton)divAlphabets.FindControl("lbtnCharacter" + Request.QueryString["tag"]);
lbtnSelected.CssClass = "firstCharacter highlighted";
}
}
N.B You will also need to change your BindTagList to use the query string also. I'm assuming you call this in the page load event.