Replace checkboxes in Dynamic Data with gridview - c#

As you know when you have a lot of elements in a database table and you also have a relationship between this table and another (let's say many-to-many) , when you are trying to insert a new item in Dynamic Data, for the foreign key option you have a lot of checkboxes you can check.
For example, if you have Category and Products and you want to insert a new Category you will have something like this:
Product1 Product2 Product3 Product4
Product5 Product6 ...
When you have few products it looks good, but when you have over 50 000 it doesn't.
How can I replace them with a grid view ( where I load product entities ) having a checkbox before every product name. If I check that checkbox it means I want to add that product to this category (that I'm creating in this moment ).
Hope you understand what I am trying to say.
I'm thinking to make a custom field ( or a custom page with custom field ) but I don't know how to make that grid view.
Thank you

How about you make a Gridview:
<asp:GridView runat="server" ID="gvMyGrid">
<Columns>
<asp:BoundField DataField="MyField" HeaderText="HeaderForMyField />
... You have a few of these
<asp:TemplateField HeaderText="Products">
<ItemTemplate>
<asp:Repeater runat="server" id="rptListOfProducts" OnItemDataBound="ProductsDataBound">
<ItemTemplate>
<asp:HiddenField id="hfProductId" runat="server"
Value='<%# DataBinder.Eval(Container.DataItem, "ProductId") %>'/>
<asp:CheckBox id="cbProduct" runat="Server"
Text='<%# DataBinder.Eval(Container.DataItem, "ProductName") %>'
Checked='<%# DataBinder.Eval(Container.DataItem, "IsThisProductSelected") %>' /> //Or some kind of flag that is true or false
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</Columns>
</asp:GridView>
Then in your C# code you can something like this to find out what the user changed.
foreach (Control c in rptMonitors.Items)
{
var productId = int.Parse(((HiddenField)c.FindControl("hfProductId")).Value);
//And get the check box too, generate a list of ProductIds, and if they're checked or not
}
Not sure if this is what you're looking for, the description was a little vague
Edit: Oh and if you're expecting loads of entries in the Repeater, you could wrap it in an AjaxControlToolkit Collapsible Panel, so it doesn't spam up your screen.

Related

Create Dynamic ItemTemplate based on GUID column names

I have a process that pivots data to create columns based on the UniqueIdentifier key value. The resulting data table has column names such as:
> PartNum
> [DB1A6498-7CC6-4EA0-846A-9B6EAB771777]
The portion of ASP is:
<asp:TemplateField HeaderText="Part No.">
<ItemTemplate>
<asp:Label ID="lblPartNum" runat="server" Text='<%#Eval("PartNum") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Run Out">
<ItemTemplate>
<asp:Label ID="lblRunOut" runat="server" Text='<%#Eval("DB1A6498-7CC6-4EA0-846A-9B6EAB771777") %>' />
</ItemTemplate>
</asp:TemplateField>
The Part Number displays just fine, however field based on the uniqueID results in a message:
DB1A6498-7CC6-4EA0-846A-9B6EAB771777/ is neither a DataColumn nor a DataRelation for table .
I read through the data table columns to create the grid columns, which display fine, however when attemtping to dynamically create the ItemTemplate, the error occurs. Putting the brackets "[]" around the uniqueID in the label did not work either.
Any suggestions are greatly appreciated. Thanks
Kevin...your response led me down the path of the brackets. Based on that, I found information about the DataBinder with the final result being that the Text property of the asp:label reads:
Text='<%# DataBinder.GetPropertyValue(Container.DataItem,"[DB1A6498-7CC6-4EA0-846A-9B6EAB771777]") %>'
This is displaying data from the uniqueID fields with no errors.

CommandName = Insert in EditTemplate of ASP.NET ListView throws "Insert can only be called on an insert item"

I am supporting a web application. In that, there are two tables - TaxCode and TaxRate. TaxCode has 1 to many relationship with TaxRate.
The UI has a ListView with LayoutTemplate, ItemTemplate and EditTemplate to show TaxCode. When the users selects a tax code in EditTemplate it shows a CutomGridView that allows the user to create or edit tax rates for that particular tax code. This CustomGridView has 3 rows each has 4 template fields as shown below.
<asp:TemplateField HeaderStyle-CssClass="highlightTitlebar" HeaderStyle-Width="5%" HeaderStyle-Height="30px">
<HeaderTemplate>
<custom:CustomImageButton ID="imgAdd" runat="server" ImageUrl="image/add_round.gif" OnClick="imgAddTaxRateDetail_Click" CausesValidation="False"/>
</HeaderTemplate>
<ItemTemplate>
<custom:CustomImageButton ID="imgEdit" runat="server" ImageUrl="image/edit.gif" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ID="imgUpdate" runat="server" ImageUrl="image/update.gif" CommandName="Update" />
<asp:HiddenField runat="server" ID="hfId" Value='<%# Bind("Id") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="imgInsert" runat="server" ImageUrl="image/insert.gif" CommandName="Insert" OnClick="imgInsert_Click" />
</FooterTemplate>
Each row in the CustomGridView is a template field. In, below image EffectiveOn section is CustomGridView,
When I try to save TaxRate with proper EffectiveOn and Rate, it throws "Insert can only be called on an insert item. Ensure only the InsertTemplate has a button with CommandName=Insert." error as the ListView doesn't have InsertTemplate. But the record gets inserted into the DB.
Please let me know if there is any way to resolve this issue?
The error is self explanatory.
Take a look at this: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.insertitemtemplate(v=vs.110).aspx
So you can do either of these things.
Create an InsertItemplate and insert using the ItemInserted event of the listview
Change the CommandName to CommandName="InsertData" and catch that event on the ItemCommand

Show editable Gridview when value is selected from dropdown

I have created a bulk edit gridview using Itemtemplate for each column. Label for non-edit mode, Textbox for edit mode. But this works only if the structure of gridview is known(Defining templates in asp).
<asp:TemplateField HeaderText="Name" ConvertEmptyStringToNull="True">
<ItemTemplate>
<asp:Label ID="lblName" Visible='<%# !(bool) IsInEditMode %>' runat="server" Text='<%# Eval("name") %>' />
<asp:TextBox ID="txtName" ControlStyle-CssClass="wide" Visible='<%# IsInEditMode %>'
runat="server" Text='<%# Eval("name") %>' />
</ItemTemplate>
</asp:TemplateField>
Now, what I am trying to achieve is when the user selects a dropdown value, the sql query is fired and it returns result. And this result is shown in gridview. But the problem arises because the number of columns in the result for each selected value from dropdown may vary. And i want to make the gridview as editable(or read-only). This requires two templates to be defined for each column.
So i want to know how this can be done dynamically i.e defining templates depending on number of columns returned by sql.
First off, you could trigger the query and following code with this event.
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//insert code
}
For the issue of different templates, I might suggest using two GridViews. I've done this where I have two separate sets of results. If they choose a dropdownItem, it hides GridView1 and shows GridView2 (while querying GridView2.SqlDataSource).
Now are you querying different SQL Tables based on dropdown? Or are they querying from the same place?

Gridview Header customizing issue

If we add a customer header to the gridview, will it add extra row?
Currently I have a gridview with four columns and, when I add a custom header gridview coming with a five rows.
My code looks like this...
<asp:TemplateField HeaderText="" ItemStyle-Width="4%" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<HeaderTemplate>
<asp:DropDownList ID="Select" runat="server">
<asp:ListItem>Country</asp:ListItem>
<asp:ListItem>Region</asp:ListItem>
<asp:ListItem>Title</asp:ListItem>
</asp:DropDownList>
</HeaderTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="" ItemStyle-Width="4%" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:HiddenField ID="Id" Value='<%#Eval("id")%>' runat="server" />
<asp:Literal ID="ltrImage" runat="server"></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
and three other TemplateFields...
Is there any problem of adding header this way? Any other way to add customer header without having this issue?
My desired output should look like this
Search Result Search By (Dropdownlist)
Data column1 Data column2 Data column3 Data column4
The one I am getting now is
Sort By (Dropdownlist)
Data column1 Data column2 Data column3 Data column4
Could anyone help if have an idea?
Thanks in advance
You just added another column with custom header to your gridview. If you want to customize a header of the first column, just customize the header of your first template field:
<asp:TemplateField HeaderText="" ItemStyle-Width="4%" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<HeaderTemplate>
<asp:DropDownList ID="Select" runat="server">
<asp:ListItem>Country</asp:ListItem>
<asp:ListItem>Region</asp:ListItem>
<asp:ListItem>Title</asp:ListItem>
</asp:DropDownList>
</HeaderTemplate>
<ItemTemplate>
<asp:HiddenField ID="Id" Value='<%#Eval("id")%>' runat="server" />
<asp:Literal ID="ltrImage" runat="server"></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
If this dropdownlist is too big or if you want add some additional text in the header, you can always create HeaderTemplate for other TemplateFields of merge some of the column's headers in code behind (example for "merging" two first headers, Id of the gridView is gridView1):
protected void gridView1_PreRender(object sender, EventArgs e)
{
int indexOfColumnToSpan = 0;
int indexOfColumnToRemoveHeader = 1;
gridView1.HeaderRow.Cells[indexOfColumnToSpan].ColumnSpan = 2;
gridView1.HeaderRow.Cells.RemoveAt(indexOfColumnToRemoveHeader);
}
well, you have not actually added a header row, but a header column .. which is basically just another column with more controls ..
why dont you place your DropDownList before your GridView, and then you can write some code behind handling the DropDownList_SelectedIndexChanged event to sort the Data ?
I have implemented this without using the filters within the GridView. Then you can handle the events separately, and populate the GridView accordingly.
What you are actually adding is a column, instead of a row. That's why you are getting that result.
Hope this helps!

TemplateField Not Updating In GridView using LINQDataSource

Im using a LINQDataSource to populate a GridView of Universities.
Each University has an associated State, which is in another table and associated by a foreign key (StateID).
I have a TemplateField in the GridView so that when you view it normally it displays the StateName which comes from the State table and when you edit it shows a DDL populated with everything from the State table.
<asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="State" SortExpression="State">
<EditItemTemplate>
<asp:DropDownList ID="DropDownListStateEdit" runat="server"
DataSourceID="LinqDataSourceStates" DataTextField="StateName" DataValueField="StateID"
SelectedValue='<%#Eval("StateID") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("State.StateName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
If I debug, inside of RowUpdating, GridViewUpdateEventArgs.NewValues doesnt even have a key for State.
Question: How do I let my gridview know I want it to update this column? All the BoundFields just seem to work...
In EditItemTemplate you should use #Bind("StateID") instead of #Eval("StateID").

Categories

Resources