Cannot change button properties in C# Webform GridView _RowEditing event - c#

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?

Related

Link button is not being disabled, how to disable link button in grid-view?

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

DataGrid1_Selectionchanged not performed after first click on gridrow

i have only one row in my grid, when i click on a gridrow one particular cell value is stored into textbox. I have a button, when click on button that textbox value is cleared. After i clear the textbox i click again on the same gridrow but its not sending any value to textbox.
`private void DataGrid1_Selectionchanged(object sender,SelectionChangedEventArgse)
{
var selectedRow = DataGrid1.SelectedItem;
TextBox1.Text = selectedRow.coloumnName.ToString();
}
private void btn_Click(object sender, RoutedEventArgs e)
{
TextBox1.Text = "";``
}`
Edit your button's CommandName property and set it to 'Select'
Or enable auto generate select button of the GridView.
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.autogenerateselectbutton(v=vs.110).aspx
The selection changed handler is not firing because you have one row. The first time works but the second time there is nothing to change to. Change the handler or add a second row

how to get the column index of the gridview

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

Get RadGrid row value through a button column in the grid

I have a column with a button in my radgrid, I want to be able to click the button and get the values of the row in which the button is located on. This must be done without selecting the row, I remember seeing a part of code my friend wrote in which he used ".Parent" or something similar to get the row from of the button column clicked.
When you click any button in the GridButtonColumn of a RadGrid, you can access the button's row and the row's values by doing the following:
In the RadGrid's definition, add OnItemCommand="RadGrid1_ItemCommand".
In the GridButtonColumn's definition, set its command name with something like CommandName="Test".
Now add the following to your code-behind to access whatever column you want. In my sample code below I get the value in the column "Whatever":
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == "Test")
{
GridDataItem item = (GridDataItem)e.Item;
string columnWhateverValue = item["Whatever"].Text;
}
}

Lost GridView SelectIndexChanged at UserControl

I have a UserControl which contain GridView.I set AutoGenerateSelectButton is true for this GridView.
But it didn't work when I press Select inside UserControl.
Do you have anyidea?
You should move your event handling to the page that owns the UserControl and not in the UserControl
Inside the Page_Load of your page, add this
myUserControl.FindControl("GridView1"));
dvGrid.SelectedIndexChanged += new EventHandler(GridView1_SelectedIndexChanged);
Add the handler to the page
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
//access the GridView
GridView grid = (GridView) sender;
//access the selected row
GridViewRow selectedRow = grid.SelectedRow;
//access the selected Primary key - make sure you set the DataKeyNames property of the GridView to the Record Id - in your Markup
string currentRowPrimaryKey = grid.SelectedValue;
//OR
string currentRowPrimaryKey = grid.SelectedDataKey.Value;
}
Now you have several values to play with. You can put a break point and examine the properties of the sender to have more options. Good Luck

Categories