I need help passing a parameter to the DataSource BindCompanyCharItems() to filter records, which I bind to a CheckList called chklstCompCharItems.
The parameter I am trying to use is <asp:BoundField DataField="Id" HeaderText="CompCharID" />
My GridView:
<asp:GridView ID="grid_Data" runat="server" Width="100%" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="CompCharID" />
<asp:BoundField DataField="Name" HeaderText="Comp Char Name" />
<asp:TemplateField HeaderText="Invite Permission">
<ItemTemplate>
<asp:CheckBoxList ID="chklstCompCharItems" DataSource='<%# BindCompanyCharItems()%>' DataTextField="Name" DataValueField="Id" SelectedValue='<%# Bind ("Id") %>' runat="server">
</asp:CheckBoxList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Try following code:
DataSource='<%# BindCompanyCharItems((Eval("Id")))%>'
Your "BindCompanyCharItems()" must accept parameter and you should then use this parameter to filter.
Related
I created a table using a Sqldatasource in .net webforms. I added a "commandField" that allows you to edit and update the content of this table, but by default the input type is "text". I would like to have different input types for some columns. For example the description column should be a "textarea" and the category column should be a "select dropdown".
I tried editing the input type= text in CSS, but this was a failure. Apparently only the textarea input type allows multiple lines to be displayed for the user.
This is how I'm calling the edit column in the table:
<asp:CommandField ButtonType="Button" ControlStyle-BackColor="DarkOrange" ControlStyle-CssClass="trCBPad" ItemStyle-CssClass="flex-container2" HeaderText="Edit" ShowEditButton="True">
<ControlStyle BackColor="DarkOrange" CssClass="trCBPad"></ControlStyle>
This is the dataField I would like to edit, so that the input is a textarea:
<asp:BoundField DataField="ProofPointId" HeaderText="ProofPointId" InsertVisible="False" ReadOnly="True" SortExpression="ProofPointId" />
If yor are using a gridview
<asp:GridView ID="GridView1" DataSourceId="MyDataSource" DataKeyNames="Code"
AutoGenerateColumns="false" AutoGenerateEditButton="true"
AutoGenerateDeleteButton="true" runat="server">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<%#Eval("Name")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" Text='<%# Bind("Name")%>' runat="server" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<%#Eval("Description")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox TextMode="Multiline" ID="txtDesctiption"Text='<%# Bind("Description")%>'
runat="server" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I am new to ASP.net and trying to add dropdownlist and search text boxes under my header fields in Gridview control.
<asp:GridView ID="EmpGridView" runat="server" AutoGenerateColumns="false"
DataKeyNames="EMPLOYEEID"
AllowSorting="True" AllowPaging="true" PageSize="50"
OnPageIndexChanging="EmpGridView_PageIndexChanging" style="margin-right: 52px" OnSelectedIndexChanged="EmpGridView_SelectedIndexChanged"
>
<Columns>
<asp:BoundField DataField="EMPLOYEEID"
HeaderText="Employee ID" ReadOnly="true"
SortExpression="EMPLOYEEID" />
<asp:BoundField DataField="PERSONNAME"
HeaderText="Person Name" ReadOnly="true"
SortExpression="PERSONNAME" />
<asp:BoundField DataField="DIVISIONNAME"
HeaderText="Division Name" ReadOnly="true"
SortExpression="DIVISIONNAME" />
<asp:BoundField DataField="DESIGNATION"
HeaderText="Designation" ReadOnly="true"
SortExpression="DESIGNATION"
/>
<asp:BoundField DataField="CNIC"
HeaderText="CNIC" ReadOnly="true"
SortExpression="CNIC" />
</Columns>
</asp:GridView>
I want following
EMPLOYEEID PERSONNAME DIVISIONNAME <----HeaderText
TextBox control TextBox Control DropDownlist control <------aspcontrols
..data ..data ..data <------ rest is db
..data ..data ..data
Meaning I want my labels there too with my asp.net controls
How should I do it?
So far I tried following but could not work out where to place it? since if I add it separately then there are rows of each control which I don't want and boundfield tag does not permit it in it.
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="searchBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
You need to use HeaderTemplate of the TemplateField for placing the controls on the header
<asp:TemplateField SortExpression="PERSONNAME">
<HeaderTemplate>
<asp:Literal runat="server">Person Name</asp:Literal>
<asp:TextBox runat="server" ID="searchBox"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:Literal runat="server" Text='<%# Bind("PERSONNAME") %>'></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
Relative newbie here. Discovered that I have to write an event handler to handle sorting of my data from an objectdatasource. Found a lot of references to code like what's below, but when the code runs I get the message "Cannot find column xxx" where xxx is the name of the column I'm trying to sort on.
I've verified that e.SortExpression is returning the proper column name, but the problem appears to be in the 2nd line below, where I try to create a datatable (dt) from the GridView1.Datasource. The value of dt after this statement is 'nothing', so the rest of the code fails.
Also, I'm listing most of the gridview definition in case that's helpful.
Protected Sub GridView1_Sorting(sender As Object, e As GridViewSortEventArgs) Handles GridView1.Sorting
Dim dt As Data.DataTable = New Data.DataTable(GridView1.DataSource)
If Not dt Is Nothing Then
dt.DefaultView.Sort = e.SortExpression
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
ASPX snippet:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="ObjectDataSource1" CellPadding="4"
EmptyDataText="No records found for your search" ForeColor="#333333"
GridLines="None" Width="930px" BorderColor="#CCCCCC" Font-Size="12px" AllowSorting="True">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="CustName" HeaderText="CustName"
SortExpression="CustName" />
<asp:BoundField DataField="CustType" HeaderText="CustType"
SortExpression="CustType" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" />
<asp:TemplateField HeaderText="Address" SortExpression="Addr1">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Addr1") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Addr1") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName"
SortExpression="ContactName" />
<asp:BoundField DataField="LastContactDate" HeaderText="LastContact" SortExpression="LastContactDate" dataformatstring="{0:d}" />
<asp:BoundField DataField="CustSince" HeaderText="CustSince"
SortExpression="CustSince" dataformatstring="{0:MM/dd/yyyy}" />
<asp:BoundField DataField="Status" HeaderText="Status"
SortExpression="Status" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="Source" HeaderText="Source"
SortExpression="Source" />
<asp:BoundField DataField="RelMgr" HeaderText="RelMgr"
SortExpression="RelMgr" />
<asp:TemplateField HeaderText="RelMgrInfo">
<ItemTemplate>
<asp:HyperLink ID="RelMgrEmailLink" runat="server" NavigateUrl='<%#Eval("RelMgrEmail", "mailto:{0}")%>' Text='<%# Eval("RelMgrEmail") + "<br>" + Eval("RelMgrPhone")%> '> </asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
Thanks!
You don't have to handle the sorting event if you work with the ObjectDataSource. Instead, set the SortParameterName to a string (like "OrderBy") and augment your business method signature that ObjectDataSource uses to retrieve data with this parameter:
public IEnumerable<Whatever> Retrieve( string OrderBy, ... other arguments ) ...
The grid will pass the parameter value to the ODS and then ODS will pass it to your method.
<asp:GridView ID="grdCatgory" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:ImageButton ID="imgEdit" runat="server" CommandArgument="<%Eval("CategoryID")%>"
CommandName="Edit" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:ImageButton ID="imgDel" runat="server" CommandArgument="<%Eval("CategoryID")%>"
CommandName="Del" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
i am getting following error when build the website...
Type 'System.Web.UI.WebControls.ImageButton' does not have a public property named 'TemplateField'
I think CommandArgument="<%Eval("CategoryID")%>"
need change to
`CommandArgument="<%#Eval("CategoryID")%>"`
I have a GridView that I use to show my users the result of a search. I want to allow them to choose which columns are shown on the GridView when performing their search. Simple enough, yes? I wanted to try doing this using just databinding, no events. Unfortunately, my code fails to update the GridView using checkboxes bound to the column's Visible property. The state of the chechboxes changes, but the Visible property of the columns does not.
Snippet of Search.aspx:
<myControl:FacultyGridView ID="FacultyGridView1" runat="server" />
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Text='<%# Eval("HeaderText") %>' Checked='<%# Bind("Visible") %>' AutoPostBack=true/></ItemTemplate>
</asp:Repeater>
Code-behind snippet in Search.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
Repeater1.DataSource = FacultyGridView1.GridView.Columns;
Repeater1.DataBind();
}
To be clear, the GridView is exposed as a public property of a user control named FacultyGridView. Relevant snippet of FacultyGridView.ascx:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AllowPaging="True" AllowSorting="True" PageSize="25">
<PagerSettings Mode="NumericFirstLast" Position="TopAndBottom" />
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" SortExpression="Name" />
<asp:TemplateField HeaderText="University" SortExpression="UniversityID">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("University.Name") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Division">
<ItemTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%# Eval("DivisionMemberships") %>'>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Division.Name") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Title" HeaderText="Title" ReadOnly="True" SortExpression="Title" />
<asp:TemplateField HeaderText="Research Type">
<ItemTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%# Eval("ResearchTypeMappings") %>'>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ResearchType.Name") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Expertise" HeaderText="Expertise" ReadOnly="True" SortExpression="Expertise" />
<asp:HyperLinkField DataNavigateUrlFields="Website" DataTextField="Website" HeaderText="Website"
SortExpression="Website" />
<asp:BoundField DataField="Phone" HeaderText="Phone" ReadOnly="True" SortExpression="Phone" />
<asp:TemplateField HeaderText="Email Address" SortExpression="EmailAddress">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("EmailAddress", "mailto:{0}") %>'
Text='<%# Eval("EmailAddress") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Finally, I should mention that the GridView is bound by a Button on the page, but I am not getting updates to the Visible property whether I play with the checkboxes before or after databinding. Furthermore, I have not seen my desired behavior when binding the repeater only on the first Page_Load() using if(!IsPostBack), nor by not using Checkbox.AutoPostback true or false. Any clue as to what I'm doing wrong? I expect it to be something simple, but I'm a bit green here.
As a note: I know how to do this easily with events, but I want to do it with databinding as a learning exercise.
Probably because every time the grid is bound to the data, the column & settings are recreated (with-out your changes).