I am dynamically creating a table which I want to have clickable rows. When the user clicks on one of the rows I want to redirect to a page specific to the item of that row. My question is how on the server side can I wire the "onclick" event to a routine that will then allow me to build a url based on some of the data included in the row they clicked?
for example I would want to do this on click:
Response.Redirect("SomePage.aspx?" itemType + "&" + COLUMN1VALUE);
where COLUMN1VALUE would be the first column in the row that was clicked.
It sounds like your actual goal is simply to display/edit the row on another page. If this is the case, you could simply add a javascript event handler to the table row when you create it.
<tr onclick="window.location='DetailPage.aspx?id=<%= IdFromDb %>'">
<!-- etc......-->
</tr>
If you use a GridView to create the table you can inject this in the RowDataBound event:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
string OnClickCmd = "window.location='DetailPage.aspx?id=";
if (e.Row.RowType == DataControlRowType.DataRow)
{
OnClickCmd += DataBinder.Eval(e.Row.DataItem, "IdFromDb").ToString() + "'";
e.Row.Attributes.Add("onclick", OnClickCmd);
}
}
Unless you need to do something in the postback, there is no need to redirect. Further, you could just create a hyperlink when you create the row eliminating the need for javascript, you don't need the full row clicking experience.
I would build the link in the table as an actual anchor tag to your SomePage.aspx (while you're building your table, you should know what the query string would be for each table row), opposed to a server side control that redirects on postback
In your click event handler, you cast the sender as button, then get its parents until you get to the TableRow, then do a FindControl() to find the control with the value specific to that row and use it to build your URL!
Good luck!
Related
I am using Telerik gridview in a winform application. When I click on the row the control goes to another page with some data. I am using grid views click event to pass the control to another form. But even when you click on header column or pager row it is going to the next form with data selected from the first row. What is the way to figure out the type of row. Whether it is a datarow or a header row?
private void grdGuests_Click(object sender, EventArgs e)
{
int id = Convert.ToInt32(this.grdGuests.CurrentRow.Cells[1].Value.ToString());
GuestDetails gd = new GuestDetails(id);
gd.Show();
}
I even tried to use MouseClick but its the same thing if I click on next page button or header row it takes it as a grid click event and pass the control to next page.
You can use the CurrentRowChanged event and check if the row type is GridViewDataRow info, so you avoid non-data rows.
We read from XML and create columns in asp gridview. Also same XML is used to create columns in a data table. We populate that data table with desired data and bind data table to gridview using server side code.
Now, we want to add delete link in each gridview row and not sure how to manage it. Since we are not using RowDataBound method.
Any help?
Lot of solution exists for this problem.
Example the delete the row from the datasource (DataSource.RemoveCurrent).
If you can define the "delete button row" (type button) you can use a simple dataGridView1.Rows.Remove .
Or if you want to delete the row with a simple click on the button:
private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
if (e.ColumnIndex == 8)// define the delete button column
{
dataGridView1.Rows.Remove(dataGridView1.Rows[e.RowIndex]); // delete with index!
} }
I have a linkbutton in a gridview that when clicked will update the DB and should remove it from being visible in the gridview. This is also in an updatepanel.
When clicking the linkbutton the DB is updated, however the gridview is never refreshed.
Both gridview and linkbuttons are dynamically generated.
Linkbuttons are created as follows:
'b' contains the unique id of the data in the row.
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lbRemove = new LinkButton();
lbRemove.ID = "removeLink" + b;
lbRemove.Command += new CommandEventHandler(lbRemove_Click);
lbRemove.Attributes.Add("onclick","return confirm('Are you sure?');");
.......
e.Row.Cells[6].Controls.Add((Control)lbRemove);
lbRemove_Click contains the method to update DB and call the griview to bind amd update the panel:
protected void lbRemove_Click(object sender, CommandEventArgs e)
{
removeFromUser(Convert.ToInt32(e.CommandArgument.ToString()));
loadGridviews(Convert.ToInt32(ViewState["currUserID"]));
upnlUserDevices.Update();
i have tried creating a linkbutton outside of the gridview using the exact same properties as the one in the gridview. When clicked it calls the same method and it refreshes the gridviews, just not when being clicked from within the gridview itself.
Bit stuck on this one if you can help??
Thanks!
Looking at your code it seems to be alright. I can only think of two suggestions:
Make sure your are doing the DataBind to the GridView at the end of the method loadGridViews()
YourGridView.DataBind();
Make sure you are updating the right UpdatePanel after doing the binding:
upnlUserDevices.Update(); // is upnlUserDevices the UpdatePanel that wraps your GridView?
Hope this helps.
So i found out how to make this work.
I need to set the linkbutton .CausesValidation = false
No expert on quite what this does, but it does fix my issue!
put this gridview in update panel
I have a datagrid view with four columns:
productid
productname
productprice
buy (this is button column )
Is it possible to find the click event of button column? I mean, if I click on the row 1 button, the corresponding row values will be transferred to another form.
If I click on the row 2 button, the corresponding values are transferred to another form. I am doing WinForms applications. Any ideas or sample code would be appreciated.
This is answered on the MSDN page for the DataGridViewButtonColumn.
You need to handle either the CellClick or CellContentClick events of the DataGridView.
To attach the handler:
dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
And the code in the method that handles the evend
void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
// Check that the button column was clicked
if (dataGridView1.Columns[e.ColumnIndex].Name == "MyButtonColumn")
{
// Here you call your method that deals with the row values
// you can use e.RowIndex to find the row
// I also use the row's databounditem property to get the bound
// object from the DataGridView's datasource - this only works with
// a datasource for the control but 99% of the time you should use
// a datasource with this control
object item = dataGridView1.Rows[e.RowIndex].DataBoundItem;
// I'm also just leaving item as type object but since you control the form
// you can usually safely cast to a specific object here.
YourMethod(item);
}
}
using validate cell you can get the (column & row of the cell). Initialize the button as win forms button object, also add a handler that call the click event of the same.
I'm trying to add HyperLinkColumns dynamically to my GridView. I have the following code:
HyperLinkColumn objHC = new HyperLinkColumn();
objHC.DataNavigateUrlField = "title";
objHC.DataTextField = "Link text";
objHC.DataNavigateUrlFormatString = "id, title";
objHC.DataTextFormatString = "{2}";
GridView1.Columns.Add(objHC);
This doesn't work, so.. how can i add a HyperLinkColumn to my GridView?
You might want to add it when the row is binded:
protected void yourGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
HyperLink hlControl = new HyperLink();
hlControl.Text = e.Row.Cells[2].Text; //Take back the text (let say you want it in cell of index 2)
hlControl.NavigateUrl = "http://www.stackoverflow.com";
e.Row.Cells[2].Controls.Add(hlControl);//index 2 for the example
}
You have to do it before the DataBinding takes place, check the GridView Events.
I think you should be using a HyperLinkField not a HyperLinkColumn.
In case, if you just want to redirect to another URL then simple use HyperLink web control and push it in the desired cell of GridView Row at RowDataBound event.
OR
If you want to perform any server event before sending it to another URL, try this
1) Add LinkButton object at RowDataBound event of GridView.
2) Set the CommandName, CommandArgument property, if requried to pass any data to this object.
3) Capture this event by handling the RowCommand event of the GridView.
By the way, I just think that you can use the DataGridView and in the Designer select the Link column and your problem would be over. The DataGridView does have a link column, than you just need to add an event on "Click" and you will be able to have what you want. This solution works if you can switch to DataGridView.
I know this thread is old but couldn't help adding my 2 cents. The procedure explained in the following tutorial worked perfectly for me:
ASP Alliance
It seems you have got things mixed up. I don't know - how that code compiles?
GridView's column collection can accept columns of type "DataControlField".
I think you need to initialize HyperLinkField and set relevant properties (text, NavigateUrl, HeaderText, Target) and add it to the columns collection.
HyperLinkColumn class is meaningful when you are using DataGrid (not in case of GridView).
Hope that helps.