Gridview not updating after removing row from linkbutton within an updatePanel - c#

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

Related

invoke gridview paging using button outside the gridview asp.net c#

How can you invoke the paging of a gridview using button from the outside of the gridview?
I have searched it but unfortunately I can't find the right logic for it. I'm not talking about creating a button for preview and next function inside the pager template of the gridview, but outside, the gridview itself like a remote button. Whenever I click either the preview or next button, the gridview will do the same.
Suppose you have the Button outside the Grid View. You can do grid view paging as follows on the button click.
Basically we need to set the pageindex of the grid view and rebind the data. You need to have the data stored in session/cache or get it afresh for the gridview.
protected void ButtonNext_Click(object sender, EventArgs e)
{
++gridview.PageIndex;
// if we have reached the last page, either set to first page.
// you could also do nothing.
if (gridview.PageIndex >= gridview.PageCount)
{
gridview.PageIndex = 0;
}
gridview.DataSource = GetGridViewData(); // the data needs to be available.
gridview.DataBind();
}

Gridview returning empty even when there is data. I want to use data in the gridview to update another database

I have a webform that the user captures data. This data affects other data that is in a Gridview. I wanted to right a DetailsView type control that makes an Update button visible if data is change in textboxes and drop downs that would require a master data update.
I have a gridview on the form and when the user click on update I wanted to update all the rows in the databases using the primary key OrderId which is on the Gridview.
If I put the code being each textbox and dropdown box and that calls a routine called DoUpdate then this works. However it is a little slow since each change forces an update, so I thought it would be better to rather have an update button appear, and then the user can click that to do the update.
When the routine is called from the button routine though the gridview has no rows. I assume I either need to findcontrol (gridview) or something but have not idea what to find it in.
Here is the code that works when called in a behind button event, but the gridview returns row count 0 when called as part of the code behind button:
List<string> _OrderIds = new List<string>();
foreach (GridViewRow gvr in gvOrderLines.Rows)
{
Label myOrderIDLablel = (Label)gvr.FindControl("lblOrderID"); //find control since it is a template field
_OrderIds.Add(myOrderIDLablel.Text);
}
The gridview is defined in the ASP page
<asp:GridView ID="gvOrderLines" runat="server" AutoGenerateColumns="False"
DataSourceID="odsOrderDetail"
Per my comment above you may want to do something like:
List<string> _OrderIds = new List<string>();
DataTable table = gvOrderLines.DataSource as DataTable;
foreach (GridViewRow gvr in table.Rows)
{
Label myOrderIDLablel = (Label)gvr.FindControl("lblOrderID"); //find control since it is a template field
_OrderIds.Add(myOrderIDLablel.Text);
}
I hope this get you headed in the right direction.

Changing text of an autogenerated select column of a gridview in asp.net - How?

I would like to change the text of the autogenerated "select" column in an ASP.NET GridView control. The text needs to be changed to the value of a DataField.
I suspect that there is a very logical way to do this but I am missing it.
I am able to add controls and data via the pre-render event but is there an easier better way?
Use the TemplateField and place into it buttons or linkbuttons with appropriate CommandName property: ButtonField.CommandName Property
You may set this button text using DataBinder.Eval method.
The easiest way I found to do this is after the calling DataBind() just before the gridview control is displayed.
foreach (GridViewRow row in gvAgreementList.Rows)
{
LinkButton lb = (LinkButton) row.Cells[0].Controls[0];
lb.Text = "Edit";
}
after <column> write this:
<asp:CommandField ShowSelectButton="True" SelectText="Save" />
and remove AutoGenerateSelectButton="True" from Gridview attribute.
First remove auto generated select then go to GridView tasks.. top right Button of GridView and then click on commandfields -> Select then edit SelectText.
(Edited answer of ShaileshK with some changes)
Go to GridVIew tasks.. top right Button of GridView and then click on edit columns
In selected fields section Click on select field. change the value of select text.
done.

C# Add HyperLinkColumn to GridView

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.

Working with the "onclick" of a server side dynamically created TableRow

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!

Categories

Resources