I have this:
<uc:MyCustomGridView runat="server">
</uc:MyCustomGridView>
This is an UserControl which is not only a GridView. It has some other controls, like a menu bar on top, and a title.
I would like to create a GridView inside my MyCustomGridView like this:
<uc:MyCustomGridView runat="server">
<uc:MyCustomGridView.GridView>
<Columns>
<asp:BoundField HeaderText="Code" DataField="Code" />
<asp:BoundField HeaderText="Description" DataField="Description" />
</Columns>
</uc:MyCustomGridView.GridView>
</uc:MyCustomGridView>
Is something like this possible? Is there any keyword that I should search for?
I solved by reading the following tutorial:
http://www.tomot.de/en-us/article/2/asp.net/how-to-create-an-asp.net-control-that-behaves-as-a-template-container-to-nest-content-via-markup
Related
I would like to apologize if this has been asked in a different way before. I couldn't find any links specific to this question.
I'm currently learning how to implement GridViews on an ASP.Net web forms application, changing to grid views from a repeater. The repeaters had been binding to some columns retrieved by a stored procedure on our SQL Server and then displaying to the page. Due to the large amount of data, we decided that using the paging feature associated with grid views would improve our performance. However, when I have the grid view bind to the data source, all of the columns from the stored procedure are displayed. I believe I have the syntax correct:
<asp:GridView ID="CECreditGridView" runat="server" DataSourceID="testSqlSource" AllowPaging="true" PageSize="100">
<Columns>
<asp:HyperLinkField DataNavigateUrlFormatString="CECredit_Detail.aspx?CECredit_ID={0}" DataNavigateUrlFields="CECredit_ID" ItemStyle-CssClass="actionLinkView" />
<asp:HyperLinkField DataNavigateUrlFormatString="CECredit_Update.aspx?CECredit_ID={0}" DataNavigateUrlFields="CECredit_ID" ItemStyle-CssClass="actionLinkEdit" />
<asp:HyperLinkField DataNavigateUrlFormatString="CECredit_Detail.aspx?CECredit_ID={0}" DataNavigateUrlFields="CECredit_ID" ItemStyle-CssClass="actionLinkDelete" />
<asp:BoundField HeaderText="ID" DataField="CECredit_ID"/>
<asp:HyperLinkField HeaderText="Session Title" DataNavigateUrlFormatString="CECredit_Detail.aspx?CECredit_ID={0}" DataNavigateUrlFields="CECredit_ID" DataTextField="CECredit_Name" />
<asp:HyperLinkField HeaderText="Person" DataNavigateUrlFormatString="Person_Update.aspx?Person_ID={0}" DataNavigateUrlFields="Person_ID" DataTextField="FullName" />
<asp:HyperLinkField HeaderText="Type" DataNavigateUrlFormatString=“CECreditType_Update.aspx?CECreditType_ID={0}" DataNavigateUrlFields="CECreditType_ID" DataTextField="CECreditType_Name" />
<asp:HyperLinkField HeaderText="Status" DataNavigateUrlFormatString="CECreditStatus_Update.aspx?CECreditStatus_ID={0}" DataNavigateUrlFields="CECreditStatus_ID" DataTextField="CECreditStatus_Name" />
<asp:BoundField HeaderText="Expiration Date" DataFormatString = "{0:MM/dd/yyyy}" DataField="CECredit_ExpirationDate" />
<asp:BoundField HeaderText="Last Updated" DataFormatString = "{0:MM/dd/yyyy}" DataField="CECredit_RecordUpdateDate" />
</Columns>
<PagerSettings Mode="NextPreviousFirstLast" FirstPageText="First" LastPageText="Last" NextPageText="Next" PreviousPageText="Previous" />
</asp:GridView>
So my question is, is there a way to only have the called columns display? Or will I need to change the stored procedure to only return the columns I want displayed? We were trying to have the stored procedure stay the same because the code behind used a few of the other columns for validation, etc.
I am trying to do something like the code below:
<%= if(ddlChoice.SelectedItem.Value ==1) { %>
<asp:BoundField DataField="FirstName" HeaderText="First Name">
<HeaderStyle HorizontalAlign="Left" /></asp:BoundField>
<asp:BoundField DataField="LastName" HeaderText="Last Name">
<HeaderStyle HorizontalAlign="Left" /></asp:BoundField>
<%= } else { %>>
<asp:BoundField DataField="Name" HeaderText="Name">
<HeaderStyle HorizontalAlign="Left" /></asp:BoundField>
<%= } %>
I have a GridView, now I want to add if-else condition to change available BoundFields according to selected item in DropDownList... Please Guide me !!!
In short: You cannot place your if statement between BoundFields as you are trying to do.
As an alternative solution, you could change the Visible property of each BoundField either from code behind or by setting a boolean value to that attribute in your .aspx file.
Another alternative solution is to have more than one GridView and change their visibility upon user selection.
I have the following GridView:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="SysInvoiceID" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="InvoiceID" HeaderText="SysInvoiceID" ReadOnly="True" SortExpression="SysInvoiceID" />
<asp:BoundField DataField="BillMonth" HeaderText="BillMonth" SortExpression="BillMonth" />
<asp:BoundField DataField="InvoiceDate" HeaderText="InvoiceDate" ReadOnly="True" SortExpression="InvoiceDate" />
<asp:BoundField DataField="InvoiceNumber" HeaderText="InvoiceNumber" SortExpression="InvoiceNumber" />
<asp:BoundField DataField="Net" HeaderText="Net" SortExpression="Net" />
<asp:BoundField DataField="VAT" HeaderText="VAT" SortExpression="VAT" />
<asp:BoundField DataField="Gross" HeaderText="Gross" SortExpression="Gross" />
<asp:ButtonField CommandName="ViewInvoice" HeaderText=" " ShowHeader="True" Text="View" />
</Columns>
</asp:GridView>
The very last column (ButtonField) is one I created myself just to include the text 'View' on each row, which when clicked, will bring up a PDF invoice.
I'm not sure if this is even possible, but I was wondering if it was possible to add some sort of validation for that column or something, so that if the 'InvoiceID' column is blank, the 'View' link on the corresponding row won't show up.
I felt close to doing this by going on split view in Visual Studio and then the 'Edit Columns' button on GridView tasks, but like I said I'm not sure if it's possible to do it this way and may have to resort to simply coding it.
Thanks for any help!
Use a <TemplateField> instead of a <ButtonField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" Text="View"
Visible='<%# Eval("IsEmpty(InvoiceID)") %>' CommandName="ViewInvoice" />
</ItemTemplate>
</asp:TemplateField>
And add a method to your page that is IsEmpty(string id) or whatever type your id is, and just check to see if it's empty first.
You can also add a CommandArgument attribute to the Button that will let you specify what the argument to it will be.
Use a placeholder...
<asp:Placeholder runat="server" ID="plView" Visible="<%# Convert.ToBoolean(InvoiceID == null) ? false : true %>">
<asp:ButtonField CommandName="ViewInvoice" HeaderText=" " ShowHeader="True" Text="View" />
</asp:Placeholder>
I have the following GridView
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
OnRowCommand="GridView1_RowCommand" DataKeyNames="Chart_Id"
AutoGenerateColumns="False" EnableModelValidation="True" >
<Columns>
<asp:CommandField ShowEditButton="False" ShowDeleteButton="False" ShowInsertButton="False" />
<asp:BoundField DataField="Week" HeaderText="Week" SortExpression="Week" />
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" ItemStyle-Wrap="False" />
<asp:BoundField DataField="Host" HeaderText="Host" SortExpression="Host" />
<asp:BoundField DataField="Topic_1" HeaderText="Topic 1" SortExpression="Topic_1" />
<asp:BoundField DataField="Topic_2" HeaderText="Topic 2" SortExpression="Topic_2"
HeaderStyle-Wrap="False" />
<asp:BoundField DataField="Topic_3" HeaderText="Topic 3" SortExpression="Topic_3" />
<asp:BoundField DataField="Topic_4" HeaderText="Topic 4" SortExpression="Topic_4" />
</Columns>
</asp:GridView>
By default, I have the edit/insert/cancel buttons set to false.
Then in the code behind, I want to be able to set these to true during certain conditions.
string theUser = Helpers.GetUser();
string admin = "adminName";
if (theUser == admin) {
// Set the buttons to true
}
I've been looking for ways to do this, and someone suggested to use the AutoGenerate properties, and then enable them like so:
GridView1.AutoGenerateEditButton = true;
GridView1.AutoGenerateDeleteButton = true;
GridView1.AutoGenerateInsertButton = true; // This one throws an error
Only problem is, AutogenerateInsertButton does not seem to exist, in the main ASPX page or in the code behind.
Can anyone suggest some ways for me to access these properties and set them to true?
Thank you.
Why do you think that a GridView should have an AutoGenerateInsertButton property?
A GridView is a list of GridViewRows, where each row represents a record/element/item which can be edited or deleted. But it doesn't make sense to have a insert-button for each record because it already exists.
You could follow this tutorial which shows how to use the footer-row of the GridView to insert a new record.
The property AutoGenerateInsertButton exists on the DetailsView control. Whoever designed the control probably figured that you don't need an insert button for each row in the grid, since each would essentially do the same thing.
So, maybe you could display an empty DetailsView at the bottom of the grid, or just create your own insert command using a regular Button.
Can I build a control that basically acts "like a MasterPage"?
What I mean to do is, say I have a grid like this in a number of pages:
<asp:UpdatePanel ID="AnnouncementsPanel" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:GridView ID="AnnoucementsGrid" runat="server" AutoGenerateColumns="false" DataKeyNames="Id" >
<Columns>
<asp:BoundField DataField="Title" HeaderText="Title" />
<asp:BoundField DataField="Created" HeaderText="Date" />
<asp:BoundField DataField="Modified" HeaderText="Last Modified" />
<asp:ButtonField ButtonType="Button" Text="Process" CommandName="Process" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
I'd like to build a control that handles most of the codebehind, but I need to declare the columns for the grid declaratively in each case.
Is there a way to create a control like this?
<uc:CrudGrid ID="AnnouncementsCrud" runat="server">
<Columns>
<asp:BoundField DataField="Title" HeaderText="Title" />
<asp:BoundField DataField="Created" HeaderText="Date" />
<asp:BoundField DataField="Modified" HeaderText="Last Modified" />
<asp:ButtonField ButtonType="Button" Text="Process" CommandName="Process" />
</Columns>
</uc:CrudGrid>
or event better:
<uc:CrudGrid ID="AnnouncementsCrud" runat="server">
<Columns>
<asp:BoundField DataField="Title" HeaderText="Title" />
<asp:BoundField DataField="Created" HeaderText="Date" />
<asp:BoundField DataField="Modified" HeaderText="Last Modified" />
</Columns>
</uc:CrudGrid>
Maybe having to name the tag "Fields", but being able to drop the button, so it can be used in the UC's code-behind?
A lot of love to whoever has a positive answer on this <3
Found the exact solution I was looking for:
[DefaultValue((string)null)]
[Editor(typeof(System.Web.UI.Design.WebControls.DataControlFieldTypeEditor), typeof(UITypeEditor))]
[PersistenceMode(PersistenceMode.InnerProperty)]
public DataControlFieldCollection Columns
{
get { return Grid.Columns; }
}
this way I can expose the Columns from the grid in my own user control and edit the fields from the markup, keeping general functionalities within my UC
Depends on how positive you want it to be :-) You can always build your own control to do this. That is one option.
Another option is to create a helper object that attaches to the grid, and keep each grid separate.
Third option is to create a user control with the common code, and programmably add columns to the grid through the user control.
HTH.