Label.property reference - c#

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");

Related

Get value of SelectedIndex in a dynamically created RadioButtonList added to a dynamically created table

I have an array of RadioButtonLists.
RadioButtonList[] r = new RadioButtonList[20];
On clicking a view button, my program creates a dynamic table and adds an image and a RadioButtonList to each cell:
tcell.Controls.Add(r[i])
On clicking a submit button, I’m trying to access the value of SelectedIndex on all RadioButtonLists; however, it’s showing up as null.
var value = r[i].SelectedValue;
I found some answers on how to get the value of RadioButtons created dynamically using .FindControl, but since my table is also dynamic, when I give Table1.rows, it results in null.
Dynamically created controls lose their state, when they are posted back, so you will have to re-create it yourself - outside the
if(!IsPostBack)
block in Page_Load (or somewhere similar).

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.

Is there a textboxlist control available somewhere?

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

Row_DataBound or Row_Created event in GridView

I have doubt about the Row_DataBound and Row_Created events :
What is the difference between Row_DataBound and Row_Created events?
What are the parameters for choosing between these two events?
RowCreated occurs after a row and all of its child controls are created.
RowDataBound occurs after the row (and its controls) are databound, i.e. populated with the data values.
The answer about which to use really depends on if you need the databound values or not. e.g. If you wanted to change the background color of your row based the value of one of your fields, then you would have to use the RowDataBound event. If your logic doesn't depend on the data, then I don't think it matters which event you use.
An example of where you would have to use RowCreated is if you had a drop down list in your row that would need to be popluated with values before the selected value was databound.

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