ASP.NET C# - Removing a column from a datalist - c#

I have a datalist inside a usercontrol that gets loaded into a page where users can customize a report based on some checkboxes.
One of the checkboxes, however, is "Hide Worklog" which should hide the worklog column from the result set because it can be quite long and interfere with the report.
If I do:
datatable1.Columns.Remove("WorkLog");
the code throws an exception because:
<asp:Label ID="WorkLog" runat="server" Text='<%# Bind("WorkLog") %>'></asp:Label></td>
doesn't exist.
Am I going about the usercontrol all wrong? This usercontrol should always be able to show the worklog, so I don't think it's bad to bind it in there, but at the same time I want to be able to hide it if the user wants.

Try removing the label control from your DataList instead of removing the column from the data source (i.e. the DataTable)
DataList1.Controls.Remove(DataList1.FindControl("WorkLog"));
You shouldn't get an error if the data source has more columns than you're displaying on the page, however, you will get an error, as you've discovered, if you're trying to display a column that doesn't exist in the data source.

bind it in code behind after checking some condition. like
if (visible) {
//bind
}
while removing control
visible = false;
you might need to change visible to session var :)

Related

Determine a RangeValidator max value based on another control

I have a GridView that contains a TemplateField with a TextBox in it (permanently in edit mode, you might say). The data from the GridViewRow is updated in the database automatically when the TextBoxChanged event fires. I am looking for a way to add a RangeValidator to the TextBox so that it will not allow values greater than a BoundField from the same row in the GridView.
I have other validators working on this TextBox, including a manually set RangeValidator. However, in all my googling, I haven't found any examples of making a RangeValidator dynamically pull from a TextBox, so I'm not sure if this is even possible. If it can pull from a TextBox, is there a way to get that value from a GridView's BoundField?
Figured out how to do it within a GridView (still no idea how it might work outside the GridView). Basically it comes down to adding in a data binding section where the max value would be entered (similar to binding a value to the TextBox itself):
<asp:RangeValidator ID="ShareTextBoxRangeValidator" runat="server"
ErrorMessage="!" ControlToValidate="ShareTextBox"
MaximumValue='<%# Eval("Cost") %>' MinimumValue="0"
Display="Dynamic" ClientIDMode="Predictable" Type="Double"/>
I found that Bind and Eval work equally well, but left it at Eval since it won't be updating the data.
A further note about Validators and GridViews: If you have set your page's ClientIDMode to Static, you must explicitly set it back to Predictable (or AutoID) on both the Validator and the control to be validated. Otherwise you will get key violation errors.

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

Display ASP.NET GridView inside a selected row in another GridView

I have been given a mockup that I do not know is possible to code in ASP.NET without being a real html and javascript wizard.
I want a GridView that when a row is selected, the selected row expands and below the selected row a panel of additional information is shown, which would also include another small GridView. The idea is this would all be in-line. So if the user selected row 4, then the additional information would appear below row 4 and then after the additional information the parent GridView would continue with row 5.
Ultimately I would want to do a multi-select type of set up, but first I need to figure out if this is even possible. Also, the solution must be 508 Compliant
The one solution I considered was using only one "column". Then I would put all my fields in the ItemTemplate, and my detail panel content in the EditItemTemplate and instead of selecting the row, set it to edit mode. The problem with this solution is I lose the functionality of multiple columns if I throw everything in one huge ItemTemplate.
Any and all suggestions or ideas are appreciated.
What you're describing would be best fulfilled with a ListView control. It takes a little more templating work to set up than a grid view, but you have much more control over it and you can emulate the look of a GridView. You would set your Selected Item template to contain another ListView (or GridView) thats bound to your detailed data.
I've done this using a gridview inside a ListView and using ajaxcontroltoolkit collapsible panel
Your parent list would be a listview that would have 2 table rows for each item, on the first row your parent column, on the second row use colspan and add a gridview wrapped on a collapsible panel

CHeckboxList Updating dynamically and styling

I'm adding items dynamically when a row is selected from GridView.
1. How can i make the items added are selected by default - (solved)
2. How can i avoid duplicates getting added to list
3. How can i remove them from list when user un-checks them.
And I want to change checkbox with an image and I'm using css like following but it is not working
.cbxCustom
{
...
}
.cbxCustom tr td checkbox
{
....
}
<asp:CheckBoxList ID="cbl1" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="cbl1_OnSelectedIndexChange"
CssClass="cbxCustom">
</asp:CheckBoxList>
Duplicates can be avoided by keeping your items in a List and before adding the item, check to see if lst.Contains(itemName). If it does, don't add it.
Removing them from the list is a little more problematic because it depends on how deeply you want to go into it. I'm assuming you don't want a PostBack for each click of a checkbox. If you want a PostBack, you'll need a way of serializing your list of checkboxes and information into a control in the ViewState. Since you're dynamically adding this list in the code-behind, I'd recommend an invisible Literal whose job is just to hold a serialized string holding your data.
If you DON'T want a PostBack, you'll need to work up a significant amount of JavaScript to modify the InnerHTML of the table that is output by the CheckBoxList. Again, as above, I'd use a Literal or some ViewState managed invisible control to serialize the state. In this case a literal wouldn't work, but a might.

Both Datasource and DatasourceID are defined on GridView

I get the above error in my C#/SQL/ASP.NET app because I have a datasource defined both in the ASPX file and the ASPX.CS file. But I want the Gridview to have selectable rows. So if I comment out the ASPX.CS datasource I get the above error but if I comment out the ASPX datasource I get the Gridview output but it is not selectable. How do I code this successfully?
You should only be specifying one or the other. If you want to give the data object directly to the grid, set the DataSource to be that object.
Otherwise, set the DataSourceID to the ID of the datasource that you want to be binding to, and let it do it's thing.
Selectable rows shouldn't have anything to do with where the data is coming from. You can select a row by setting the SelectedRow property. Making them selectable in the UI is a whole different topic.
Sounds like you need to just set the DataSourceID from the markup, and set AutoGenerateSelectButton to true in your markup as well. Your question makes it sound like this is the behavior you are looking for. If you'd rather generate your own select buttons, you should look into either CommandFields, or just adding your own buttons with a CommandName of "Select".

Categories

Resources