I have two ASP.NET Grid views, which contain an Image-button, which both call an Edit On-click event. the On-click event looks like:
protected void Edit(object sender, EventArgs e)
{
ImageButton ibtn1 = sender as ImageButton;
using (GridViewRow row = (GridViewRow)((ImageButton)sender).Parent.Parent)
{
txtMessageID.ReadOnly = true;
txtMessageID.Text = row.Cells[2].Text;
txtReference.Text = row.Cells[6].Text;
buttonClicked.Text = ibtn1.ID.ToString();
popup.Show();
}
}
which sole purpose is to fire off a ModalDialogBox, with key items from the grid view being clicked. My problem is that one of the grids doesn't have the Cells[6] (Reference) and therefore falls over. What i need to do is wrap a statement around this cell checking which grid (ID) the button click came from.
I'm not using Row-command, as this wouldn't allow for a single method call from multiple grids. My question is how do I obtain the Grid ID from the Image Button being clicked within this method (see above)?
ended up using the RowCommand of the Gridview, and then used the following to get what i need:
rotected void Edit(object sender, GridViewCommandEventArgs e)
{
ImageButton ibtn1 = sender as ImageButton;
GridView grd = sender as GridView;
string gridName = grd.ClientID;
string buttonId = e.CommandName;
using (GridViewRow row = (GridViewRow)((ImageButton)e.CommandSource).NamingContainer)
{
txtMessageID.ReadOnly = true;
txtMessageID.Text = row.Cells[2].Text;
if (gridName == "grdMessageDups")
{
txtReference.Text = row.Cells[6].Text;
}
buttonClicked.Text = ibtn1.ID.ToString();
popup.Show();
}
}
Related
I created a user control which has datagridview. Then I added row and column dynamically from textfile in datagridview.
My problem is I need column which has buttons in each row. In first row, the text of button is 'Test1' and in second row 'Test2' not the same text.
After searched on google I tried this code
var testButton = new DataGridViewButtonColumn();
testButton.Name = "Test";
testButton.HeaderText = "Test";
testButton.UseColumnTextForButtonValue = true;
testButton.Text = "Test1";
this.dataGridView1.Columns.Add(testButton);
But it gives me both button text as 'Test1'.
Assuming you are in Windows Forms, you need to add a DataGridViewButtonColumn to your DataGridView - Not directly to the DataTable.
This should occur somewhere after you bind the DataTable to the DataGridView.
Something like this should work:
DataGridViewButtonColumn uninstallButtonColumn = new DataGridViewButtonColumn();
uninstallButtonColumn.Name = "uninstall_column";
uninstallButtonColumn.Text = "Uninstall";
int columnIndex = 2;
if (dataGridViewSoftware.Columns["uninstall_column"] == null)
{
dataGridViewSoftware.Columns.Insert(columnIndex, uninstallButtonColumn);
}
Of course you will have to handle the CellClick event of the grid to do anything with the button.
Add this somewhere in your DataGridView Initialization code
dataGridViewSoftware.CellClick += dataGridViewSoftware_CellClick;
Then create the handler:
private void dataGridViewSoftware_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == dataGridViewSoftware.Columns["uninstall_column"].Index)
{
//Do something with your button.
}
}
//Change the Button text property on a data-bound event
protected void YourDataGridViewId_RowDataBound(object sender, GridViewRowEventArgs e)
{
// finding the button control..`enter code here`.
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button YourbtnVariable = (Button)e.Row.FindControl("YourbuttonID");
YourbtnVariable.Text = "Text"+e.Row.RowIndex+1;
//e.Row.RowIndex+1 will give u index of row every time incremented with 1
}
}
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
I'm having trouble figuring out how could I use my imagebutton (see below) in my ITemplate to append the button's corresponding row data (ItemID) as a query string.
My ImageButton in my ITemplate:
ImageButton select_button = new ImageButton();
select_button.ID = "select_button";
select_button.ImageUrl = "~/Files/System/Icons/highlighter.png";
select_button.CommandName = "Select";
select_button.ToolTip = "Select";
container.Controls.Add(select_button);
Should I handle it in in the imagebutton's OnClick event (if so, is there a way to get the row where the button is located) or can I handle in in the GridView events (rowbinding, rowseleted, rowcommand, etc.)?
I'd be glad to elaborate more on my code upon request. ^ ^
You can set the ID in the CommandArgument property of your button control in the RowDataBound Event. Once you have an ID, you can track rows with it.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow dr = ((DataRowView)e.Row.DataItem).Row;
((Button)e.Row.FindControl("select_button")).CommandArgument = dr["IdColumn"].ToString();
}
}
I have a Gridview for which Dropdown list has to be added on the run time at the Pager row. I have added the below code on the Gridview RowCreated.
protected void gv_transaction_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Pager)
{
using (DropDownList ddlpagesize = new DropDownList())
{
ddlpagesize.Items.Add("25");
ddlpagesize.Items.Add("50");
ddlpagesize.Items.Add("75");
ddlpagesize.Items.Add("100");
ddlpagesize.Items.Add("150");
ddlpagesize.Items.Add("200");
ddlpagesize.AutoPostBack = true;
ddlpagesize.Items.FindByText(gv_transaction.PageSize.ToString()).Selected = true;
ddlpagesize.SelectedIndexChanged += ddlpagesize_SelectedIndexChanged;
using (Table tbl = (Table)e.Row.Cells[0].Controls[0])
{
using (TableCell cell = new TableCell())
{
cell.Controls.Add(new LiteralControl("<b>Page Size: </b>"));
cell.Controls.Add(ddlpagesize);
tbl.Rows[0].Cells.AddAt(0, cell);
}
}
}
}
}
protected void ddlpagesize_SelectedIndexChanged(object sender, EventArgs e)
{
using (DropDownList ddlpagesize = (DropDownList)sender)
{
gv_transaction.PageSize = int.Parse(ddlpagesize.SelectedValue);
gv_transaction.PageIndex = 0;
BindTransactionGrid();
}
}
Now, SelectedIndex change event is not firing, when I change the dropdownlist value.
But interestingly, when I remove the using statement from the initiation of page size Dropdownlist; Selectedindex event is firing perfectly. Please tell me if there is any relation with the disposing of dropdownlist and selectedIndex Changed event for the dynamic dropdown in a Gridview
You don't need to wrap asp.net controls in using statements, asp.net will call dispose automatically on your controls, i think your using statements are causing them to be disposed too early.