(I'm using Lukinha RS's solution to the row onclick functionality)
When I click on a row within the gridview I get a postback before the ModalPopupExtender opens, I dont want the postback however as you see the method I use is the cause. Unfortunatly it is the only way I have been able to get an onClick applied to a gridview row to open the MPE.
Another problem I have is with the MPE open - I click a 'close' button on the popup panal it simply reloads the page resulting with the same popup panal opening.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
if (row.DataItem == null)
{
return;
}
try
{
switch (e.Row.RowType)
{
case DataControlRowType.Header:
break;
case DataControlRowType.DataRow:
e.Row.Attributes.Add("onmouseover", "this.style.cursor='hand'");
e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(GridView1, "Select$" + e.Row.RowIndex.ToString()));
break;
}
}
catch
{
return;
}
And here is my SelectedIndexChanged
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = ((GridView)sender).SelectedRow;
ModalPopupExtender mpe = (ModalPopupExtender)row.FindControl("ModalPopupExtender1");
mpe.Show();
}
Unfortunatly it is the only way I have been able to get an onClick
applied to a gridview row to open the MPE
Incorrectly. Actually, you CAN open modal extender without postback.
Change onclick attribute value as below:
e.Row.Attributes.Add("onclick", String.Format("javascript:$find('{0}').show();", ModalPopupExtender1.ClientID));
Related
I have a GridView object on a C# WebForm page which displays table data and allows editing. I have made a Template Button column to execute the Update Command in my SQLDataSource Object.
The buttons are enabled by default, which caused errors when they were pressed without the Row being in Edit Mode. So I used the GridView1_DataBound() Event to disable the buttons for each row using FindControl on the appropriate column. This works fine.
protected void GridView1_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow gvrow in GridView1.Rows)
{
Button setButton = (Button)gvrow.Cells[7].FindControl("Button1");
setButton.Visible = False;
}
}
What I want to do is when a row is selected for editing, I enable only that row's button. I have been able to use FindControl to get a reference to the button (verified I have the reference doing Debug) But when I make changes to the button, it doesn't work.
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
DataBind();
int c = GridView1.Rows[e.NewEditIndex].Cells.Count - 1;
Button setButton = (Button) GridView1.Rows[e.NewEditIndex].Cells[c].FindControl("Button1");
// doesn't work!!
setButton.Visible = true;
}
In debug mode, I verified that I do have access to the button for the row and can see its properties, etc.
I have tried changing forecolor, text, etc. but nothing changes on the actual button; what do I need to do in order to change the button?
I have a Gridview with a link button in the first column. Once the link button is clicked I want to open a window, but I also want to disable the link button.
Not working
Once I click on the link button the window behavior is the expected one. However, the link button is not disabled. Therefore is permitting me to click on it over and over again.
The front end is:
<asp:LinkButton runat="server" ID="lnkbtnView" CommandArgument='<%# Eval("Id")%>' OnCommand="GetViewOnClientClick" >View<br/></asp:LinkButton>
GetviewonClientClick method:
protected void GetViewOnClientClick(object sender, EventArgs e)
{
LinkButton lb = (LinkButton)sender;
GridViewRow row = (GridViewRow)lb.NamingContainer;
if (row != null)
{
int index = row.RowIndex;
LinkButton link = (LinkButton)row.FindControl("lnkbtnView");
link.Enabled = false;
}
}
Why is not working as expected?
That's what I want to solve. I suspect I might have to do a rebinding or something related, but I don't quite understand what is really going on. Therefore, I don't know how to implement it.
I never found the solution to edit the control in that method. What I did instead was to read on the cycles of the grid view events.
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview_events(v=vs.110).aspx
If you read the previous link, you'll know that once you click on a record it is going to trigger a call to the RowDataBound event. Therefore; in the RowDataBound event you'll have access to the control and apply logic to edit the control's properties (such as color, disabled, enabled...).
Therefore, because I have two types of records in this grid view, I needed to store and to make persistent what records were clicked.
This is my fix to keep track of the clicked rows:
protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
gvs.SetHeaderArrows(e);
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
int Id = (int)(e.Row.RowIndex);
int? inspID = convert.ToIntQ(DataBinder.Eval(e.Row.DataItem, "InspectionID"));
string rowclicked = string.Format("clickedrow{0}", Id);
if (convert.ToIntQ(Session[rowclicked]) != null)
{
if (Id == Convert.ToInt32(Session[rowclicked]))
{
LinkButton button = (LinkButton) e.Row.FindControl("lnkbtnView");
button.ForeColor = Color.Gray;
button.Enabled = false;
}
else
{
LinkButton button = (LinkButton)e.Row.FindControl("lnkbtnView");
button.ForeColor = Color.DarkBlue;
button.Enabled = true;
}
}
I have two image buttons in the GridView used for selection. They populate the textboxes. What I need is when i click imagebutton "Edit" it should populate the textboxes from the gridview and when the imagebutton "Delete" is pressed it does the same and disbale the textboxes too. The point is am not getting a logic of how to get the column indexes programmatically.
help me out with the condition.
the issue is just with the c# code. Here is the snippet which i have tried:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
var row = GridView1.SelectedRow;
if (condition)
{
_PropertyTitle.Text = row.Cells[1].Text;
_Address.Text = row.Cells[2].Text;
}
else
{
_PropertyTitle.Text = row.Cells[1].Text;
_PropertyTitle.Enabled = false;
_Address.Text = row.Cells[2].Text;
_Address.Enabled = false;
}
}
}
It is not very clear from question that what you are trying to achieve.But what i got is you want to write specify both image button click separately.
for that you have to use the RowCommand event.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
}
In this row command event according to CommandName (of button) you can manage the code for two buttons
Refer this link for Rowcommand event row command
One more link
ı have been traying to create dynamic control panel using webusercontrol , delegate,and ADO.
Even ı have write the delegate for deleting and editing ı faced with "The GridView 'GridView1' fired event RowDeleting which wasn't handled."problem . Can anybody help me pls
here is my codes
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = this.DataSource;
GridView1.DataBind();
GridView1.DataKeyNames = new string[] { this.DataKeyNames };
}
public object DataSource { get; set; }
public string DataKeyNames { get; set; }
public event GridHander RowDeleting;
public event GridHander RowSawing;
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow gvr = ((LinkButton)e.CommandSource).Parent.Parent as GridViewRow;
int rowIndex = gvr.RowIndex;
object id = GridView1.DataKeys[rowIndex].Value;
switch (e.CommandName)
{
case "Edit":
GridView1.EditIndex = rowIndex;
break;
case "Delete":
if (RowDeleting != null)
{
GridEventArgs args = new GridEventArgs()
{
row=gvr,
id=id,
rowIndex=rowIndex
};
RowDeleting.Invoke(e.CommandSource, args);
}
break;
case"Save":
if (RowSawing != null)
{
GridEventArgs args = new GridEventArgs()
{
row = gvr,
id = id,
rowIndex = rowIndex
};
RowSawing.Invoke(e.CommandSource, args);
}
GridView1.EditIndex = -1;
break;
case "Cancel":
GridView1.EditIndex = -1;
break;
default:
break;
}
}
}
//My webform
ublic partial class CategoryControlPanel : System.Web.UI.Page
{
CategoryResposite _categoryResposite=new CategoryResposite();
protected void Page_Load(object sender, EventArgs e)
{
ControlPanel.DataSource = _categoryResposite.ListCategories();
ControlPanel.RowDeleting += ControlPanel_RowDeleting;
ControlPanel.RowSawing += ControlPanel_RowSawing;
}
void ControlPanel_RowSawing(object sender, GridEventArgs e)
{
throw new NotImplementedException();
}
void ControlPanel_RowDeleting(object sender, GridEventArgs e)
{
_categoryResposite.DeleteCategories(Convert.ToInt32(e.id));
}
You are trying with a command name of Delete for your delete button. So the gridview creates a row deleting event automatically....
You need to change the command argument from Delete to something else like Delete_Product or whatever you want...
The code that you have posted is incomplete (missing the aspx file code), from your description of the problem it sounds as though you have not assigned the RowDeleting event to GridView1.
Inside the opening gridview tag in the aspx file add the assignment as follows:
<asp:gridview ID="..." runat="server" ... OnRowDeleting="<name of event handler>" ...>
If the event handler ControlPanel_RowDeleting is designed to handle a delete from gridview action then insert this as the event handler name. Ensure that you re-bind the gridview after delete so that the changes are shown on postback.
protected void ControlPanel_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
// cancel the automatic delete action
e.Cancel = true;
// do the delete
_categoryResposite.DeleteCategories(Convert.ToInt32(e.id));
// complete delete action
GridView1.DataBind();
}
One of the good things about GridView is that it provides a built-in CommandField Buttons which allows us to perform certain actions like editing, updating,deleting and selecting of GridView data.
To add those command fields mentioned in the GridView you can follow these few steps below:
1. Switch to Design View
2. Right Click on the GridView and Select --> Show Smart Tag --> Add New Columns
3. On the List Select CommandField
4. Check Delete and Edit/Update options then OK
As you can see the Edit and Delete CommandField are automatically added in the last column of GridView. Now we can start to write our codes for editing and updating the information in the GridView.
In-order to perform Edit and Update in GridView we need to use three events ( GridView_RowEditing, GridView_RowCancelingEdit , GridView_RowUpdating). For those who do not know on how to generate Events in GridView you can follow these steps below:
Switch to Design View in Visual Studio Designer
Click on the GridView
Navigate to the GridView Property Pane and then SWITCH to Event Properties
From there you would be able to find the list of events including those three events mentioned above
Double Click on that to generate the Event handler for you
Try adding protected to the method signatures.
I have a problem here regarding to editable gridview. what I want to do is replacing edit button function by using a single clickable row. When I click a row, it should be forwarding me to a new page for editing those row data. How can I achieve this, without using edit button?
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
// only apply changes if its DataRow
if (e.Row.RowType == DataControlRowType.DataRow)
{
// when mouse is over the row, save original color to new attribute, and change it to highlight yellow color
e.Row.Attributes.Add("onmouseover",
"this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#EEFF00'");
// when mouse leaves the row, change the bg color to its original value
e.Row.Attributes.Add("onmouseout",
"this.style.backgroundColor=this.originalstyle;");
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string abc = ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();
e.Row.Attributes["onClick"] = "location.href='Default.aspx?id=" + abc + "'";
//e.Row.Attributes["onClick"] = "location.href='Default.aspx?id=" + DataBinder.Eval(e.Row.DataItem, "CategoryID") + "'";
e.Row.Attributes.Add("style", "cursor:pointer;");
}
}
Simply add controls as you want to Default.aspx and on default.aspx pageload event(In not postback condition) retrieve the record through ID and populate the controls.
Now when submit button pressed update the record and redirect back to your original page