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.
Related
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();
I inserted a label (called Label1) in a "table cell" which in its turn is inserted into a datalist.
Why is it that in this situation, I can't use, for example, Label1.Text inside the Page_Load method, however when I insert the label outside the datalist, any reference to Label1 works perfectly?
Is there a workaround to this, as I need the label in the table cell as described and also to be able to modify its caption (.Text property) from the code-behind file, where I plan to assign it the value of a stored procedure.
Hello and thank you very much in advance.
Label control is created for every row a DataList has, and it's ID is prefixed by every container that it is inside of (so its not called Label1 anymore) - you need to handle OnItemCreated or OnItemDataBound event, then find and cast it as Label Label1 = (Label)e.Item.FindControl("Label1"), and only after that can you access it.
If a control is inside a datalist, then it will be repeated for each item in the list. Consequently, it doesn't make sense to refer to the control outside of the datalist, as ASP.NET would not know which instance to refer to - and if there is no data, there won't even be any instances!
You can access the label given an item in the datalist, however:
DataListItem item = dataList.Items[0]; // or whichever item you want
Label label1 = (Label)item.FindControl("Label1");
I have a control that is loaded dynamically and contains a repeater amongst other items such as a group of text boxes.
The control is used for address verification.
The process is:
control displays.
User enters the address info.
page posts back and the control validates the address info against a web service.
If the address isn't 100%, it displays a list of potential matches. <- this occurs in a repeater. In other words, it is databound in code.
The user checks a box next to the one they want to accept.
page posts back
So far, 1 - 6 work. The problem is that when the page posts back, there are no repeater items.
How can I get the repeater to load it's items without having to run another databind() operation?
Failing that, how can I run the databind() effectively? In other words, I don't have the controls street/city/state/zip values during the oninit. So, how can I reapply the viewstate changes?
Most controls are rendered as HTML form elements like <input type="text" name="myId" id="myId/>.
Part of Web Forms is really just a wrapper around all of this.
Whenever you need to get at the raw values, you can just look in the Request.Forms collection for any control values that are posted back with the name of the control you are looking for (which will usually be made up of the unique id that asp.net generates for uniqueness of controls, and your name).
This is exactly what you did - you went through the Forms collection looking at the raw form values that were POSTed to the server. It's also what you often do when you implement the IPostBackDataHandler interface when creating your own controls - you scoop the value out of Request.Forms.
The annoying thing with checkboxes is that with "standard" HTML they only get submitted if they are ticked.
Two important facts:
Controls in a Repeater Item should be created in OnItemCreated and not OnItemDataBound.
OnItemCreated runs during a Postback without one having to call DataBind().
I've been loading user controls into repeater items and the type of user control is based on the data item being bound to the repeater. So I added the type of user control into the repeater item's viewstate so that during the OnItemCreated that runs on postback (without a data item to bind to because DataBind() has not been called) it knows what type of user control to load. The viewstate will then be applied.
protected void rpTransactions_OnItemCreated(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var dataItem = e.Item.DataItem;
string typeName;
string viewStateKey = string.Concat("typeName", e.Item.ItemIndex);
if (dataItem == null)
{
typeName = ViewState[viewStateKey].ToString();
}
else
{
typeName = dataItem.GetType().ToString();
ViewState[viewStateKey] = typeName;
}
Control template = TransactionTemplateFactory(typeName);
template.ID = "trans";
e.Item.FindControl("phTransSpecific").Controls.Add(template);
}
}
If the code in OnItemDataBound runs, it fishes this user control out and binds its data to it.
There are ASP controls such as radiobuttonlist and checkboxlist and you can databind them to a database query. It's great for creating dynamic lists with user interaction. What I'm trying to do is generate a list of textboxes in the same fashion. A list of textboxes that behave the same way.
The object is to have a checkboxlist that is generated via datasource/database. When the user is finished selecting items from this list, they click a button. That list hides (using jquery) and a new list is created based on their selections. However, the new list is now a list of their selections accompanied by an empty textbox. The user fills in the textboxes for each entry and submits again which commits it to a database.
SO:
checkbox - description
checkbox - description
checkbox - description
checkbox - description
Becomes:
Description - Textbox
Description - Textbox
The reason that I'm looking for a list-type control is so that I can ultimately loop through it for submission to the database using linq. Does that make sense? My real question is if there is a control like this yet. I gave the full description in case someone has any other ideas, short of creating a custom control.
There's nothing out of the box that does what you describe no. But you can still loop through controls. I would put your form controls inside of an asp:Panel or a div with runat="server" and use something like the following code to cycle through them as you described.
foreach(Control ctl in myPanel.Controls)
{
//check control type and handle
if (ctl is TextBox)
{
//handle the control and its value here
}
}
asp:ListBox has a property named SelectionMode, which can be set to SelectionMode="Multiple" and allows you to select items you want. This is not exactly what you need but it's a simple solution.
After the selection is made in checkboxlist, make a postback to server. Here create a datatable with two columns (description & text). For every selected item in the checkboxlist add a row to this table and bind it to a gridview control. Here 'text' column will be always empty. Configure the gridview to use template column with a textbox in the itemtemplate
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