I have a gridview which has rows of 'tickets'. A user has the ability to 'claim' a ticket, this is all set up and works. What i need is to be able to change/hide the 'claim' button and replace it with a 'release' or 'open' command button. Can i achieve this at a rowdatabound level? What i do not want is to query the db everytime to see if the ticket is claimed.
You could cast the button from its Cell, then change its CommandName, and CommandArgs if needed, along with its text; eg use the same actual button for many purposes.
Im asssuming there is some status field that dictates what you can do with a record? Therefore on RowDataBound get this via a datakey, and adjust the button to suit.
Then on its click, check the command name and execute the relevant function / code block?
Edit - like this:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
Button btn = e.Row.Cells[YourButtonsColumIndex].FindControl("btnYourButtonsID") as Button;
btn.CommandName = "Release";
//Or
((Button)e.Row.Cells[YourButtonsColumIndex].FindControl("btnYourButtonsID")).CommandName = "Release";
}
Bearing in mind hidden rows still count in the zero-index column list.
Related
I have gridview putting data from one table from database using stored procedure.
On click of Edit Button, post back is happen which refreshing the data on gridview and wrong row is shown in edir mode.
I have found the reason is that on click on EDIT link javascript:__doPostBack('GridView','Edit$0') is displayed at the status bar, which is a problem here. Edit$0 means the absolute value of the row and when postback get the data from data base wrong row is shown in edit mode...
I think the solution will be put the edit mode based on not row number but some unique value of the selected row for edit.
Please help if any one has answer to it.
If you're using a gridview I suggest using the native OnRowEditing="GridViewEditEventHandler"
and then in the code behind
protected void TaskGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
//Set the edit index.
TaskGridView.EditIndex = e.NewEditIndex;
//Bind data to the GridView control.
BindData();
}
see http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowediting(v=vs.110).aspx
However, if for some reason you are creating an OnClick function when you press the edit button and then changing the Itemtemplate to show/hide values I would suggest doing
GridViewRow gvr = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
int RowIndex = gvr.RowIndex;
from there you can do the .findcontrol to find the specific item you want ex:
TextBox txtbox = (TextBox)TaskGridView.Rows[RowIndex].FindControl("textBoxID");
Hope this helps
Right now I have an ASP Table. I can add rows and cells to this table just fine. What I would like to do, is instead of the cell just displaying text, I would like to add a control. For example a Button.
Right now, my first thought on how to do this would be just to put the <ASP:Button ... as the .Ttext attribute of the table cell. But my gut tells me this wont work. Further more, I probably couldn't add a function to handle the button click.
Can someone help point me in the right direction on how to achieve this?
You need to add the control to the table cell. Just call the Controls.Add method on the cell and add your control. Below is a brief sketch that should point you in the right direction.
Button b = new Button();
c.Controls.Add(b);
The following assumes you have a blank ASP:Table on your page with some defined rows (just for show really).
protected void Page_Init(object sender, EventArgs e)
{
foreach (TableRow row in this.Table1.Rows)
{
foreach (TableCell cell in row.Cells)
{
Button btn = new Button();
btn.Text = "Some Button";
btn.Click += new EventHandler(btn_Click);
cell.Controls.Add(btn);
}
}
}
void btn_Click(object sender, EventArgs e)
{
((Button)sender).Text = "Just Clicked";
}
The question hangs on what the source is for your controls. Bar far, the most effective way to make this happen is through data binding, even if your data source is just the Enumerable.Range() function.
Failing that, you need to create an instance of your controls and add them to the Control's collection of the table cell they will belong in. You can just use the += syntax for adding event handlers. The trick here is that the code to create and add the button will need to run again on every postback, and it will need to run before the page_load phase of the asp.net life cycle.
If we have the following code, then when user clicks an Edit button, page is posted back and put into Edit mode:
protected void gvwEmployees_RowEditing(object sender, GridViewEditEventArgs e)
{
gvwEmployees.EditIndex = e.NewEditIndex;
gvwEmployees.DataSource = ds.Tables["Employees"];
gvwEmployees.DataBind();
}
But with the following code, user has to click the Edit button twice before a row is put into edit mode ( thus page needs to be posted back twice before row gets into edit mode). Why does it matter whether gvwEmployees.EditIndex is assigned a value before or after we bind GridView to a data source?
protected void gvwEmployees_RowEditing(object sender, GridViewEditEventArgs e)
{
gvwEmployees.DataSource = ds.Tables["Employees"];
gvwEmployees.DataBind();
gvwEmployees.EditIndex = e.NewEditIndex;
}
Thank you
Modifying the EditIndex property with a value different than the one it already has requires that DataBind() is called after the modification.
As described in the GridView.EditIndex documentation page, it could also happen if EditIndex is modified under other circumstances:
If you set the EditIndex property
after a postback or in handlers for
events that are raised later than the
Load event, the GridView control might
not enter edit mode for the specified
row. If you read the value of this
property in other event handlers, the
index is not guaranteed to reflect the
row that is being edited.
1) I noticed that if we don’t bind GridView to object data source control, then when user puts GridView into edit mode, we have to handle GridView.RowEditing event (else we get an exception ) and in this event put GridView’s row into editing mode. Is there a reason why GridView doesn’t automatically put a row into edit mode?
2) When we manually bind GridView to one of DataSet’s tables and user puts a row into edit mode, row’s columns will replace fields with text boxes. But for some reason these text boxes don’t display current field values, but instead they don’t display any text at all. What am I doing wrong?
3) I’ve also handled gridView.RowUpdated event, so I could put row back into non-edit mode, but to no effect. I even tried by pressing Edit button of some other row, but row still wouldn’t go out of edit mode. Any ideas what I’m doing wrong?
protected void gvwEmployees_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
e.KeepInEditMode = false;
}
Thanx
When not using a DataSource control with a GridView or other data-bound control which hide the complexity of the manual data-binding you must manually handle RowEditing, RowUpdating, and RowDeleting etc. With the built in data model and automatic binding the GridView handles these events for you.
You haven't posted your RowEditing code, but i suspect that you are not setting the GridViews EditIndex to the NewEditIndex and are not rebinding, this is probably why you are not seeing current data.
protected void gvwEmployees_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView.EditIndex = e.NewEditINdex;
BindData();
}
The same is true for your RowUpdating event. You will have to manually update your data, then set the EditIndex to -1, this will put your GridView back into ReadOnly mode. Keep in mind that e.OldValues, e.NewValues and e.Keys properties of the GridViewUpdateEventArgs are not populated when binding manually. This mean you'll have to take care of the update yourself by using e.RowIndex which is the index of the edited row.
protected void gvwEmployees_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridView.EditIndex = -1;
BindData();
}
I have a Gridview which is binded to a datatable that I created. The data in the table is ever changing.
I added a buttonfield to the Gridview. When clicked I want to send a specific columns value in that row through a link or sent through a function.
How do you go about doing this?
I found out how to do it. You set the commandName property to whatever you want to call it inside the properties.
Then if you double click on the button after creating it in design view, it should pop up a function in the code behind page. You can then access the row by doing the following.
protected void gvUsers_RowCommand(object sender, GridViewCommandEventArgs e)
{
int rowNum = int.Parse(e.CommandArgument.ToString());
}
from there you can use the row number to access specific column data.
Here's a decent example from MSDN: http://msdn.microsoft.com/en-us/library/bb907626.aspx