Dynamic Hyperlink is not getting visible in gridview - c#

I am trying to show dynamic hyperlink on each row of a dynamic gridview using below code but it is not showing the hyperlink.
gvdates.DataSource = xyz;
var m = tool.ToolsID;
HyperLink hp = new HyperLink();
hp.Text = e.Row.Cells[i].Text;
hp.NavigateUrl = "~/OutageInfo.aspx?name=m;" + hp.Text;
e.Row.Cells[i].Controls.Add(hp);
gvdates.DataBind();
e.Row.Cells[i].Controls.Add(gvdates);

You should create dynamic controls in RowCreated instead of RowDataBound because this event gets fired on every postback whereas RowDataBound only will fire when the GridView gets databound to it's DataSource.
Dynamically created controls must be recreated on every postback with the same ID as before, then they retain their values in the ViewState and events will fire correctly.
So you should create them in RowCreated and "fill" them in RowDataBound(f.e. the HyperLink's NavigateUrl if it's from the datasource).
However, you should add dynamic controls after you have databound the GridView. So this seems to be pointless (whatever e is):
e.Row.Cells[i].Controls.Add(hp);
gvdates.DataBind();

Related

Change ImageUrl of image outside Repeater with value from inside Repeater

I have a Repeater with several items. With the use of a timer, the items are highlighted one after another with this code:
((HtmlControl)Repeater1.Controls[nextHighlight].Controls[1]).Attributes["class"] += "highlighted";
One of the values that the items hold is an ImageUrl (collected from a database), which is to be used with an asp:Image outside the Repeater.
How can i get the value - hopefully from the Timer_Tick function - in the Repeater item, so that i can change the ImageUrl? It doesn't matter how i should store the ImageUrl in the Repeater (Hiddenfield, maybe?) - whatever is the easiest to get from the Timer_Tick function.
Get RepeaterItem of child repeater in Timer_Tick event using NamingContainer method.
HtmlImage aImage=(HtmlImage)RptRow.Parent.Parent.Parent.FindControl("aImage");
// RptRow is RepeaterItem
aImage.Src = "Your url";
Let me know if you face any issue.

Dynamically added TableRows not in Rows collection on Postback

I've added TableRows via code-behind to a table. The rows render on the page, but on PostBack, the rows are no longer part of the collection. What is the trick to get these rows to persist so that I can use them on PostBack?
.ascx
<asp:Table id="OrderItemsTable" runat="server">
.ascx.cs
TableRow itemRow = new TableRow();
// Ticket Quantity
TableCell cell1 = new TableCell();
cell1.Attributes.Add("align", "center");
TextBox ticket1QuantityTextBox = new TextBox();
ticket1QuantityTextBox.Width = Unit.Pixel(30);
ticket1QuantityTextBox.MaxLength = 3;
ticket1QuantityTextBox.Attributes.Add("class", "OrderItemTicketQuantityTB");
ticket1QuantityTextBox.Text = item.Quantity.ToString();
cell1.Controls.Add(ticket1QuantityTextBox);
itemRow.Cells.Add(cell1);
...
OrderItemsTable.Rows.Add(itemRow);
You need to contruct dynamic controls on the Page_Init event. Are you perhaps doing this in Page_Load or elsewhere. If they are added later than Page_Init then they don't participate in ViewState which will cause problems.
Yes, you need to construct the control tree with your dynamic controls in Page_Int event so that ASP.NET can load the ViewState data later into Page Control tree (which you will need to access the values from TextBox)
On a side not you should use Dynamic control only when the type of control is dynamic. Else you can achive the same using Repeater and listview
Truely Understanding Dynamic Controls
And here is the alternative to using Dynamic controls
ASP.NET: Alternatives to Dynamic Controls - Part 1

Gridview not updating after removing row from linkbutton within an updatePanel

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

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.

How to retrieve a changed value of databound textbox within datagrid

ASP.NET 1.1 - I have a DataGrid on an ASPX page that is databound and displays a value within a textbox. The user is able to change this value, then click on a button where the code behind basically iterates through each DataGridItem in the grid, does a FindControl for the ID of the textbox then assigns the .Text value to a variable which is then used to update the database. The DataGrid is rebound with the new values.
The issue I'm having is that when assigning the .Text value to the variable, the value being retrieved is the original databound value and not the newly entered user value. Any ideas as to what may be causing this behaviour?
Code sample:
foreach(DataGridItem dgi in exGrid.Items)
{
TextBox Text1 = (TextBox)dgi.FindControl("TextID");
string exValue = Text1.Text; //This is retrieving the original bound value not the newly entered value
// do stuff with the new value
}
So the code sample is from your button click event?
Are you sure you are not rebinding your datasource on postback?
When are you attempting to retrieve the value from the TextBox? i.e. when is the code sample you provided being executed?
If you aren't already, you'll want to set up a handler method for the ItemCommand event of the DataGrid. You should be looking for the new TextBox value within that method. You should also make sure your DataGrid is not being re-databound on postback.
I would also highly recommend reading through Scott Mitchell's excellent article series on using the DataGrid control and all of it's functions:
https://web.archive.org/web/20210608183626/https://aspnet.4guysfromrolla.com/articles/040502-1.aspx

Categories

Resources