On my page there are textboxes which I use for input. How can I wire those textboxes to an EntityDataSource control?
Instead of using plain text boxes with your EntityDataSource control, I would recommend using a GridView control with a corresponding DetailsView control. The EntityDataSource control was designed to work nicely with these two controls. The DetailsView control will be the one which is actually responsible for the inserting and updating of your data.
When you are configuring the EntityDataSource controls (you will need two, one for the GridView and one for the DetailsView), make sure to check the 'Enable automatic inserts,' 'Enable automatic updates,' and 'Enable automatic deletes' check boxes in the 'Configure Data Selection' screen of the setup wizard. In your case, since you are only interested in inserting the data, you can just check the 'Enable automatic inserts' checkbox.
You can also just use the DetailsView by itself, and leave the GridView out. To do this, you'll need to configure the DetailsView's EntityDataSoruce 'Expression Editor' for the 'Where' clause a bit differently (found in the properties list of the EntityDataSource control).
It's important to point out that the GridView and DetailsView are pretty heavy controls. Unless you are building a small site (with few users) or prototyping a planned site project, I would recommend revising your development strategy and possibly going with raw ADO.Net instead.
Here is a simple example:
<asp:EntityDataSource ID="EntityDataSource1" runat="server"
ConnectionString="name=SomeDatabaseEntities"
DefaultContainerName="SomeDatabaseEntities"
EnableFlattening="False"
EntitySetName="Links" EnableDelete="True" EnableInsert="True"
EnableUpdate="True">
</asp:EntityDataSource>
<asp:GridView ID="GridView1" runat="server" CellPadding="4"
ForeColor="#333333"
GridLines="None" AllowPaging="True" AllowSorting="True"
EnableViewState="false"
DataSourceID="EntityDataSource1"
AutoGenerateColumns="False" PageSize="20"
DataKeyNames="LinkId">
<Columns>
<asp:CommandField ShowDeleteButton="True"
ShowSelectButton="True" />
<asp:BoundField DataField="LinkId" HeaderText="LinkId"
SortExpression="LinkId" ReadOnly="True" />
<asp:BoundField DataField="Title" HeaderText="Title"
SortExpression="Title" />
<asp:BoundField DataField="URL" HeaderText="URL"
SortExpression="URL" />
<asp:BoundField DataField="ImagePath" HeaderText="ImagePath"
SortExpression="ImagePath" />
</Columns>
</asp:GridView>
<br />
<asp:EntityDataSource ID="EntityDataSource2" runat="server"
ConnectionString="name=SomeDatabaseEntities"
DefaultContainerName="SomeDatabaseEntities"
EnableFlattening="False"
EntitySetName="Links" Where="it.LinkId == #LinkId"
EnableInsert="True"
EnableUpdate="True">
<WhereParameters>
<asp:ControlParameter ControlID="GridView1"
DbType="Int32" Name="LinkId"
PropertyName="SelectedValue" />
</WhereParameters>
</asp:EntityDataSource>
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px"
Width="125px"
CellPadding="4" ForeColor="#333333"
GridLines="None" DataKeyNames="LinkId"
AutoGenerateRows="False"
DataSourceID="EntityDataSource2">
<Fields>
<asp:BoundField DataField="LinkId"
HeaderText="LinkId"
ReadOnly="true" InsertVisible="false"
SortExpression="LinkId" />
<asp:BoundField DataField="Title"
HeaderText="Title"
SortExpression="Title" />
<asp:BoundField DataField="URL" HeaderText="URL"
SortExpression="URL" />
<asp:BoundField DataField="ImagePath"
HeaderText="ImagePath"
SortExpression="ImagePath" />
<asp:CommandField ShowEditButton="True"
ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
Related
I have a gridview with some values. If the user clicks on a link or button (here I took a Buttonfield with the text 'Copy') on one of the rows, it should copy a cell value of that row to the clipboard.
Is something like this possible in asp.net c# ?
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource1" Visible="False" OnRowDataBound="GridView1_RowDataBound"
onselectedindexchanged="GridView1_SelectedIndexChanged1">
<Columns>
<asp:BoundField DataField="Username" HeaderText="Username"
SortExpression="Username" />
<asp:BoundField DataField="Password" HeaderText="Password"
SortExpression="Password" />
<asp:CommandField ButtonType="Image" EditImageUrl="/images/edit.png" CancelImageUrl="/images/undo.png" UpdateImageUrl="/images/save.png" ShowEditButton="True" />
<asp:CommandField ButtonType="Image" ShowDeleteButton="True" DeleteImageUrl="/images/deletered.png" />
<asp:ButtonField Text="Copy" />
</Columns>
<EmptyDataTemplate>
No data available
</EmptyDataTemplate>
</asp:GridView>
There is no Onclick event available for a ButtonField. Otherwise I would think of using some javascript (I have seen that in other solutions)
Thanks for any input.
You can do that with windows form only, but since you are working on Web application you should write client side code which is javascript to copy data on client side not server side. checkout this link
I am setting up HealthMonitoring and I am writing a couple of pages to manage the event logs.
I have one page which has a GridView and another that has a DetailsView. The GridView has a template button on each row with an onClick event that loads the second page with the DetailsView on it.
What I would like to do is; when I click the template button on the GridView it then loads the second page with the DetailsView and inserts that particular record from the GridView into the DetailsView table.
The DetailsView must have paging enabled. I am stuck trying to find out which page index to load with the right record from the GridView. Currently it will just load up the first page index, then I have to click next to the record I need.
Without the paging enabled, I can read my 2 global variables and then load the correct record into the DetailsView table, but with paging enabled I'm not sure how to do it.
On the GridView page:
protected void Details1_ButtonClick(object sender, EventArgs e)
{
//I set 2 global variables here of the selected EventId and
//Details to read when the next page loads
Response.Redirect("ErrorDetails.aspx");
}
On the DetailsView page.
protected void Page_Load(object sender, EventArgs e)
{
//without paging I can set the SqlDataSource1.SelectCommand to select
//the correct record using one of the global variables
}
I have tried to use DetailsView1_PageIndexChanged, DetailsView1_Load and Page_Load to get the value in the first row, but for some reason it is allways a page behind. The variable always shows the ID of the previous page when it loads. I was going to try and keep track of the page for deleting records, but it will not work either.
This is the DataSource and DetailsView in ErrorDetails.aspx:
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:DefaultConnection %>"
ProviderName="<%$ ConnectionStrings:DefaultConnection.ProviderName %>">
<DeleteParameters>
<asp:Parameter Name="EventId" />
</DeleteParameters>
</asp:SqlDataSource>
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" DataSourceID="SqlDataSource1" DataKeyNames="EventId" AutoGenerateRows="False" OnItemDeleted="DetailsView1_ItemDeleted" OnItemDeleting="DetailsView1_ItemDeleting" AllowPaging="True" OnLoad="DetailsView1_Load" OnPageIndexChanged="DetailsView1_PageIndexChanged" OnPageIndexChanging="DetailsView1_PageIndexChanging">
<AlternatingRowStyle CssClass="alt" />
<Fields>
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="EventId" HeaderText="EventId" />
<asp:BoundField DataField="EventTimeUtc" HeaderText="EventTimeUtc" />
<asp:BoundField DataField="EventTime" HeaderText="EventTime" />
<asp:BoundField DataField="EventType" HeaderText="EventType" />
<asp:BoundField DataField="EventSequence" HeaderText="EventSequence" />
<asp:BoundField DataField="EventOccurrence" HeaderText="EventOccurrence" />
<asp:BoundField DataField="EventCode" HeaderText="EventCode" />
<asp:BoundField DataField="EventDetailCode" HeaderText="EventDetailCode" />
<asp:BoundField DataField="Message" HeaderText="Message" />
<asp:BoundField DataField="ApplicationPath" HeaderText="ApplicationPath" />
<asp:BoundField DataField="ApplicationVirtualPath" HeaderText="ApplicationVirtualPath" />
<asp:BoundField DataField="MachineName" HeaderText="MachineName" />
<asp:BoundField DataField="RequestUrl" HeaderText="RequestUrl" />
<asp:BoundField DataField="ExceptionType" HeaderText="ExceptionType" />
<asp:TemplateField HeaderText="Details">
<ItemTemplate>
<asp:Label ID="Label1" runat="server">
<%= EventVariables.EventDetails %>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" />
</Fields>
<PagerSettings Mode="NextPreviousFirstLast" />
<PagerStyle CssClass="pager" />
</asp:DetailsView>
This is the DataSource and GirdView from the parent form:
<asp:GridView
ID="ErrorGrid"
runat="server"
AutoGenerateColumns="False"
DataSourceID="SqlDataSource1"
DataKeyNames="EventId"
OnRowDeleting="ErrorGrid_RowDeleting"
AllowPaging="True"
AllowSorting="True"
Font-Size="Small"
CellPadding="10"
CellSpacing="1" >
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete this record?');" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="Details1" Text="Details" runat="server" AutoPostBack="true" OnClick="Details1_ButtonClick" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="EventId" HeaderText="EventId" SortExpression="EventId" />
<asp:BoundField DataField="EventTime" HeaderText="EventTime" SortExpression="EventTime" />
<asp:BoundField DataField="RequestUrl" HeaderText="RequestUrl" SortExpression="RequestUrl" />
<asp:BoundField DataField="ExceptionType" HeaderText="ExceptionType" SortExpression="ExceptionType" />
</Columns>
</asp:GridView>
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:DefaultConnection %>"
SelectCommand="SELECT * FROM [aspnet_WebEvent_Events]"
DeleteCommand="DELETE FROM [aspnet_WebEvent_Events] WHERE [EventId]=#EventId">
<DeleteParameters>
<asp:Parameter Name="EventId" />
</DeleteParameters>
</asp:SqlDataSource>
I managed to solve this by doing a calculation. As there is 10 rows per page in the GridView I'm able to calculate the page by doing this:
EventVariables.EventPage = ((((ErrorGrid.PageIndex + 1) * 10) - 10) + Row.RowIndex);
And then loading the page in the DetailsView from the variable when it loads.
Well it so happens that I am kind of a novice to asp.net and trying to create a Grid whose source is declared programmatically.
In the process, I came across 2 tags Fields and Columns. Can anyone please tell me how they are different?
EDIT: I went through some sample MSDN examples, and for all I can tell it seems to me they can be used interchangeably(though I have a feeling thats not true!).
Check this out:
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="EmployeeID" DataSourceID="ObjectDataSource1">
<Columns>
<asp:BoundField DataField="LastName" HeaderText="LastName"
SortExpression="LastName" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName"
SortExpression="FirstName" />
<asp:BoundField DataField="Title" HeaderText="Title"
SortExpression="Title" />
<asp:BoundField DataField="HireDate" HeaderText="HireDate"
SortExpression="HireDate" />
</Columns>
</asp:GridView>
And then there is:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateRows="False" DataKeyNames="ProductID"
DataSourceID="ObjectDataSource1" EnableViewState="False">
<Fields>
<asp:BoundField DataField="ProductName" HeaderText="Product"
SortExpression="ProductName" />
<asp:BoundField DataField="CategoryName"
HeaderText="Category"
ReadOnly="True" SortExpression="CategoryName" />
<asp:BoundField DataField="SupplierName"
HeaderText="Supplier"
ReadOnly="True" SortExpression="SupplierName" />
<asp:BoundField DataField="QuantityPerUnit"
HeaderText="Qty/Unit" SortExpression="QuantityPerUnit" />
<asp:BoundField DataField="UnitPrice"
DataFormatString="{0:c}"
HeaderText="Price"
HtmlEncode="False" SortExpression="UnitPrice" />
</Fields>
</asp:GridView>
Seem similar or is it just me??!
Thanks for helping.
Columns is just the surrounding tag for the fields which are
TemplateFields with any controls you want or
BoundFields which are created automatically
So Columns enclose the list of fields in the GridView.
<Columns>
<asp:Boundfield datafield="StudentID"
readonly="true"
headertext="Student ID"/>
<asp:TemplateField HeaderText="Student" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:label runat="server" Font-Bold="true" ID="LblStudent" Text='<%# Bind("Student") %>'></asp:label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Inner Grid">
<ItemTemplate>
<asp:GridView ID="Grid2" AutoGenerateColumns="false" runat="server" GridLines="None" Width="300">
<RowStyle CssClass="GridViewRowStyle" />
<AlternatingRowStyle CssClass="GridViewAlternatingRowStyle" />
<HeaderStyle CssClass="GridViewHeaderStyle" />
<SelectedRowStyle BackColor="Aqua" />
<Columns>
<asp:TemplateField HeaderText="Student" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:label runat="server" Font-Bold="true" ID="LblStudent" Text='<%# Bind("Student") %>'></asp:label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
As you can see, a TemplateField could also contain another nested GridView.
Am I blind or wat!
After I posted this question I went back to my drawingboard and as it turns out, there is no Fields tag in asp:GridView, right?!
Please do let me know if this is true people(and prove me silly!)
Putting aside this particular control for a moment, it might be helpful to look at this from a general computer science point of view.
In classic programming (ANY LANGUAGE), a FIELD would be the INTERSECTION of a row and a column -- a discrete piece of data. For example, if a table has 20 rows of data containing first and last names, if you went to the 19th row and looked in the "first name" column, you've got a FIELD. Perhaps it contains the discrete data "JOHN".
COLUMNS then would be collections of like data -- in this example, you have the two columns "first name" and "last name". Columns would have attributes such as a data type, maximum length, constraints (are nulls OK?, etc.) and so forth.
Some may quibble with my definitions and say that individual cells in a COLUMN would be called a FIELD. It's not uncommon to hear that. I would reply that for a table with a single column, it'd be especially true :-) But the takeaway point is this: COLUMNS are generally containers for smaller, more discrete items such as FIELDS. FIELDS typically refer to a single piece of data, such as you'd find at the intersection of a row and column in a database table.
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.
I want the headers of my gridview to be hyperlinks, without the "SortExpression"...
I searched the net, but I've been not very succesful.
Anyone has the solution?
For example: when clicking on the header of a simple gridview, the site navigates to a webpage. Is it possible?
Thanks in advance!
Have you tried Gridview Header template like...
<asp:GridView runat="server" ID="grd">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:HyperLink runat="server" NavigateUrl="YourURL"> </asp:HyperLink>
</HeaderTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I think a HeaderTemplate is needed here...
Ref.: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.templatefield.headertemplate.aspx
HTH.
Now I got this gridview, And I need the headers to be clickable, whereafter an event starts (something like OnClickHeader="header_ClickEvent"?) Ofcourse there is a SortExpression element, which enables to sort the grid, but I want to be able to start any event, like when clicking a button.
I could not find any solution within the asp:BoundField nor asp:TemplateField...
I thought a hyperlink could solve the problem, but that was a bit premature.
The Gridview:
<asp:GridView CssClass="gridview" ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Student_key" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" PagerSettings-Visible="false" PageSize="14">
<HeaderStyle CssClass="headerstyle" />
<RowStyle CssClass="rowstyle"/>
<AlternatingRowStyle CssClass="altrowstyle" />
<Columns>
<asp:BoundField DataField="Studentnumber" HeaderText="Studentnummer" >
<HeaderStyle CssClass="header100" />
</asp:BoundField>
<asp:BoundField DataField="Prefix" HeaderText="Voorletters" >
<HeaderStyle CssClass="header75" />
</asp:BoundField>
<asp:BoundField DataField="prename" HeaderText="Voornaam" SortExpression="Voornaam">
<HeaderStyle CssClass="header75" />
</asp:BoundField>
<asp:BoundField DataField="nickname" HeaderText="Roepnaam" >
<HeaderStyle CssClass="header100" />
</asp:BoundField>
<asp:BoundField DataField="insertion" HeaderText="Tussenvoegsel" >
<HeaderStyle CssClass="header100" />
</asp:BoundField>
<asp:BoundField DataField="surname" HeaderText="Achternaam">
<HeaderStyle CssClass="header100" />
</asp:BoundField>
<asp:CommandField SelectText="show results" ShowSelectButton="True" >
<HeaderStyle CssClass="header100" />
</asp:CommandField>
</Columns>
<EmptyDataTemplate >There are no results shown, please try again.</EmptyDataTemplate>
</asp:GridView>
I used a method that might be a little unconventional but it works. In my case I wanted to use the standard BoundField controls in my gridview as opposed to using a template field with both a HeaderTemplate and ItemTemplate. I used a simple gridview based on a SQL datasource that looks like this.
<asp:GridView
ID="gvTopXByContest"
runat="server"
AutoGenerateColumns="False"
DataSourceID="dsTopXByContest"
AllowSorting="true"
OnSorting="gvTopXByContest_OnSorting" >
<Columns>
<asp:BoundField DataField="txtOnlineUserName" HeaderText="Fan Name & Rank" SortExpression="txtOnlineUserName" ItemStyle-Width="155px"></asp:BoundField>
<asp:BoundField DataField="fltTotalPoints" HeaderText="Points" SortExpression="fltTotalPoints" ItemStyle-Width="40px"></asp:BoundField>
<asp:BoundField DataField="curWon" HeaderText="Won" SortExpression="curWon" ItemStyle-Width="40px"></asp:BoundField>
</Columns>
</asp:GridView>
I then used code that fires on the OnSorting event of the gridview to do my redirects
Protected Sub gvTopXByContest_OnSorting(sender As Object, e As GridViewSortEventArgs)
If e.SortExpression <> DirectCast(sender, GridView).SortExpression Then
If e.SortExpression = "txtOnlineUserName" Then
Response.Redirect(URL to redirect to goes here)
ElseIf e.SortExpression = "fltTotalPoints" Then
Response.Redirect(URL to redirect to goes here)
Else
'I could have used another ElseIf here but since there are only 3 columns Else works
Response.Redirect(URL to redirect to goes here)
End If
End Sub