"Action" column in a DataReader - asp.net

I have an "action" column in my repeater which shows actions a user can select for an item. The column contains ASP.NET HyperLink or LinkButton controls. Some actions are based on whether a user is in a role, which I determine programatically. I'm struggling with the best way to dynamically generate this column when I populate the repeater. Now I am assigning in-line code to the Visible property of each control, but I feel that is sloppy and not very straight forward. Would I be better served using a PlaceHolder control and populating that at runtime? What kind of methods do other people use for situations such as this?

The "normal" way to apply any sort of dynamic rendering to a Template based control such as the Repeater is to handle the ItemCreated or ItemDataBound events.
In your particular case, you could check appropriate conditions within that event handler and toggle the visibility of the relevant "Action" column.
Also, see this question where Ian Quigley posted a code snippet that should serve as a good example for you. It may also help to read my own answer which shows how to use visibility toggling in inline code.

Related

Can I Use GridView SelectedIndexChanged Instead of RadioButtonList SelectedIndexChanged?

I have a few reasons that I need this to happen, but suffice it to say that when I use the event handler for the RadioButtonList the sender object returns as just the RadioButtonList, and I need more information from the GridView because that's how I can uniquely identify which record my RadioButtonList is tied to.
I added an "OnSelectedIndexChanged" property to my GridView but it doesn't fire/handle when I want it to. I'm assuming this has something to do with the way the GridView is designed to work.
Does anyone have any suggestions on how to do this or a better alternative?
I also thought about using a hidden field, but I can't add it to the RadioButtonList so it won't help unless it's global. The problem with it being global is that I can't set the value uniquely to the record that has it's RadioButtonList being altered.
Clear as mud? Please ask if you need any clarification.
Thanks in advance!
Just thought I would add an answer and explain how I solved my issue.
Since the scope was too narrow, I needed to add a custom field to the radiobuttonlist. I just went ahead and added the following code:
MyRadioButtonList.Attributes.Add("NecessaryField")
I added this so that everytime a grid view row was databound this code would aldo execute, ensuring that every RadioButtonList had this field.
After that it's available for use.

How can I make a repeater field editable?

I have a page with a repeater.. I have to make some fields of this one editable. I don't see how I can transform the repeater label into a textbox.. Can I use jquery to do that?
Have somebody make this kind of manipulation?
Thanks..
The Repeater control does not have an EditTemplate like many of the other data controls.
I would suggest having the edit fields either in a hidden Placeholder, then show this when clicking an edit button. This would involve the page posting back and then you having to show/hide the relevant parts in the ItemCommand handler.
Another way would be do add the edit fields/textboxs in a Panel control, then hide this through display: none;. Then you can change this to display: block; with some javascript. This will avoid the page PostBack.
This can be done in a Repeater, but a DataList control is more straightforward and is just as easy to use. There's an MSDN article on doing it in a Datalist control with full source code here: http://msdn.microsoft.com/en-us/library/bf5211wb(v=vs.71).aspx
Converting a repeater to a DataList is a much easier approach than having editable items in a Repeater.
HOWEVER
to answer your question directly, there IS a Codeproject sample here: http://www.codeproject.com/KB/aspnet/EditableRepeater.aspx
that shows how to use a Repeater with full edit functionality (including adding and deleting items).
To see the relevant code in the CodePlex article, search for the text "EditIndex". The relevant code-behind is always a few lines above and/or below this keyword.
It depends on how you want to do this:
using standart control probably you need GridView.
You can define the template for repeater and put TextBox there, then
on postback you will need to find dynamically created controls and
also you will need to take care to keep ID's of these controls the
same on postbacks.
And the other thing - you can replace label with the textBox using
jQuery and then update value via Ajax reques.
You decide what you need :) Anyway it's a lot of samples in the internet.

Required Field Validator For RadioButton Columns Inside Gridview

I have GridView and one columns is a RadioButton for select. How can I set a validator for columns to check that at least one RadioButton is selected?
I posted to a similar question the other day with a solution that should work for you:
How to check that at least one RadioButtonList has an item selected?
It uses a CustomValidator with a server side check to ensure at least one item is selected. You will need to substitute looking for a RadioButton instead of the RadioButtonList and maybe a few other tweeks but the code should work for what you need. It searchs for all of one type of control and checks for a specific critera. In your case look for all RadioButton controls and ensure at least one is checked.
there is no out-of-the-box way for what you are asking, you will need to use custom validator and implement the logic yourself on the client as well as on the server side; few way you can go about it, the one that comes to mind is to have the custom validator logic check at least one of the checkboxes is checked for passing IsValid, we will need more details about your structure to provide more specific solution

How to retrieve values of dynamically created controls in code behind?

I intend to use Javascript to dynamically create a group of html elements (input boxes, select list, radio button, etc.) each time the user clicks "Add More" button.
I have found the Javascript that does that here:
http://viralpatel.net/blogs/2009/01/dynamic-add-textbox-input-button-radio-element-html-javascript.html
http://forums.asp.net/p/1611284/4118564.aspx
So how do I get the values of these dynamically created HTML elements from code-behind? Is there a JQuery way which is better?
Thank you.
I think you'd may be able to do it with a callback/async postback to your ASP.NET page to update the control tree as you update. Not sure how the ViewState is updated/affected by this.
Otherwise, because your messing with the ViewState, so you'll need to revert to accessing them by the Request.Form object.

GridView Making a single Edit Item Template take the entire row

I've got this gridview. The gridview uses TemplateFields as the number of fields in the database's table. What I do next is use an ItemTemplate to present the correct column info.
Now, problems rise when the user click's Edit. Since I can only use EditItemTemplate to edit I am resulted with a control in each column. What I really want is to have a single row with no columns so I can easily style the edit mode (having a table with a different layout for example).
Is this possible? If so, how?
I suspect you will have to create your own specialized class that inherits from GridView and does its own rendering to accomplish this.
You might hack some workaround though... such as manually editing the rendered HTML output or DOM.

Resources