I have a query expression which I am binding to a GridView in Page_Load. The data I want to capture in the SelectedIndexChaned event is in a BoundField defined thus:
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True"
SortExpression="ID" Visible="False" />
If I set Visible="True", I have no trouble getting this data. Is there a way to hide the ID field and still get the data?
Depends on how you are trying to get the data. If this is an ID field that is unique for each row in the datasource, use DataKeyNames = "ID" in the GridView declaration. Then, in the code behind, whenever you need the ID, you can use the following line:
string ID = GridView1.Rows[GridRowIndex].DataKeys[0].Value.ToString();
You could also convert one of your BoundFields to a TemplateField, and place a HiddenField in it to store the ID. Like so:
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="someOtherDataLabel" runat="server" />
<asp:HiddenField ID="IDHiddenField" runat=server />
</ItemTemplate>
</asp:TemplateField>
Then you could use FindControl() in the RowDataBound event of the GridView to store the ID value.
Related
I have a gridview with a dropdownlist for the products description in the footer template.
There is no way to create a SelectedIndexChanged in the IDE and writing it out manually produces an error? How to create code to handle the Selection change? I need to populate the product ids when the product description is selected.
"EDITED"
I tried using the gridview rowediting event assuming that if a row item was changed (ie, a new selection in the dropdown, it would fire, but it doesn't) It appears a gridview event has to be fired when that dropdown list changes, that's where i need the code to go. Any ides on what event?
Here is what the template field markup is:
<asp:TemplateField HeaderText="description" SortExpression="description">
<FooterTemplate>
<asp:DropDownList ID="ddlProductDesc" runat="server" DataSourceID="edsProductDesc" DataTextField="description"
OnSelectedIndexChanged="ddlProductDesc_SelectedIndexChanged">
</asp:DropDownList>
<%--<asp:TextBox ID="tbInsertdescriptiton" Width="350" runat="server"></asp:TextBox>--%>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblProdDesc" runat="server" Text='<%# Bind("description")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Take a look at this:
http://www.c-sharpcorner.com/blogs/7228/asp-net-gridview-dropdown-with-related-records-in-textbox.aspx
I have a asp DetailsView control that I auto generate the fields.
There is one column that has an ID that is mapped to another table (foreign key). It shows up in a textbox. I want that column to be displayed as a dropdownlist as indicated in my code example below. This works fine, but the other column still shows the textbox with the ID in it.
My question is:
is it possible to use the auto generate and still hide columns you don't need or want to modify?
I hate to have to write code for every single column just because one column needs to use a TemplateField.
DetailsView
<asp:DetailsView ID="DetailsView1" runat="server"
DefaultMode="Edit" DataSourceID="EntityDataSource1"
AutoGenerateEditButton="True" AutoGenerateInsertButton="True">
<Fields>
<asp:TemplateField HeaderText="Authorization">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" Runat="server" DataSourceID="EntityDataSource2" CssClass="DropDown"
DataTextField="Name" DataValueField="AuthenticationId" SelectedValue='<%# bind("AuthenticationId") %>'>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp
</Fields>
</asp:DetailsView>
DetailsView DataSource:
<asp:EntityDataSource ID="EntityDataSource1" runat="server"
ContextTypeName="EntityNamespace.MyEntity" EnableFlattening="False"
EntitySetName="Routes" Include="Authentication" Where="it.RouteId = #RouteId">
<WhereParameters>
<asp:RouteParameter Type="Int32" RouteKey="RouteId" Name="RouteId" />
</WhereParameters>
</asp:EntityDataSource>
Dropdownlist DataSource
<asp:EntityDataSource ID="EntityDataSource2" runat="server"
ContextTypeName="EntityNamespace.MyEntity" EnableFlattening="False"
EntitySetName="Authentications">
</asp:EntityDataSource>
I think you can. Take a look at AutoGenerateRows. It says like this on msdn:
Explicitly declared row fields can be used in combination with
automatically generated row fields. When both are used, explicitly
declared row fields are rendered first, followed by the automatically
generated row fields.
But you also have to consider that the rows are not in the field collection
Automatically generated bound row fields are not added to the Fields
collection.
Reference here
I have a paginated GridView on an ASP.NET page. In the DataBound event of the GridView control, I am attempting to update the OnClick event of each cell in the row to window.open() a new page using a field in the underlying DataTable that is not displayed in the GridView. However, the GridView DataSource propety is null, as is the GridViewRow DataItem property. The GridView's DataKeys has a Count of 0. The paginated DataSet has 20 rows and is being rendered properly, I just can't find the underlying data to pull the ID needed for the window.open() call.
I have followed asp.net's guide when constructing my page:
Creating a data access layer
Creating a business access layer
Displaying Data with ObjectDataSource
So I have a DAL -> BLL -> ObjectDataSource -> GridView. The ID column is being hidden in the GridView:
<asp:GridView ID="pdfdocuments" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataSourceID="pdfods" PageSize="20"
ondatabound="pdfdocuments_DataBound">
<PagerSettings Mode="NumericFirstLast" />
<Columns>
<asp:BoundField DataField="pdf_id" HeaderText="pdf_id" InsertVisible="False"
ReadOnly="True" SortExpression="pdf_id" Visible="False" />
...
</Columns>
<HeaderStyle BackColor="#FFCC99" />
<AlternatingRowStyle BackColor="#FFFFCC" />
</asp:GridView>
How do I access the pdf_id value in the underlying DataTable?
Try accessing GridView.DataSourceObject in the RowDataBound event.
Unless there is some requirement to use javascript window.open it is probably better to use an anchor tag with a target like the following code:
<asp:GridView ID="GridView1" runat="server" DataSourceID="ODS1"
OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="False">
<Columns>
<asp:HyperLinkField DataTextField="FieldName" DataNavigateUrlFields="FieldID" DataNavigateUrlFormatString="somePage.aspx?id={0}" Target="_blank" />
</Columns>
</asp:GridView>
If you have to execute JavaScript you can still do it without accessing the underlying DataSourceObject directly using a TemplateField:
<asp:GridView ID="GridView1" runat="server" DataSourceID="ODS1"
OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href='javascript:window.open(<%# Eval("FieldID") %>);'><%#Eval("FieldName")%></a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I agree with D-Mac to use RowDataBound event. But instead of using GridView.DataSourceObject try to access the row's DataItem property.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView rowView = e.Row.DataItem as DataRowView;
if (rowView != null)
{
//Check if the execution reach this pont
object pdf_id = rowView["pdf_id"];
}
}
You can find an example here
I have a GridView with 2 BoundFields like this:
<asp:BoundField DataField="UnitPrice" HeaderText="Unit Price">
</asp:BoundField>
<asp:BoundField DataField="Quantity" HeaderText="Quantity">
</asp:BoundField>
How do I add a "Total" field which is calculated by multiplying the other 2 fields to the GridView declaratively? These 2 existing fields are from an SQL database and I cannot add new fields to this database. In a way, I must create the Total field dynamically.
If this is not possible, how to do this programmatically?
Thanks.
Easiest way would be to add a property to the underlying class:
Decimal Total { get { return UnitPrice*Quantity; } }
You can then reference the field Total in a BoundField
If modifying the underlying class isn't an option, try this:
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" Text='<%# String.Format("{0:c}", ((Decimal)Eval("UnitPrice"))*((int)Eval("Quantity"))) %>' />
</ItemTemplate>
</asp:TemplateField>
Note that you will want to make sure the casts for each Eval match your data structure.
I have a gridview that is being populated from a datasouce.
The Stored procedure that is populating the datasource, has a field "Client" and a field "Client WebSite".
I want to populate the field "Client" in the gridview column called "Client" which would be a hyperlink field and the hyperlink field would be the "Client WebSite" value from the dataset. The client website is an external site (not within my asp project)
Below is my html code. How can I get the "Client WebSite" appear as the DataNavigatrURL value?
<asp:HyperLinkField DataTextField="Client" HeaderText="Client" DataNavigateUrlFields="Client"
DataNavigateUrlFormatString="Client WebSite">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Left" />
</asp:HyperLinkField>
Use databinding on the NavigateUrl attribute, like this:
NavigateUrl = '<%# Bind("ClientWebSite") %>'
Or more fully:
<asp:HyperLinkField DataTextField='<%# Bind("Client" %>' HeaderText="Client" NavigateUrl='<%# Bind("ClientWebSite") %>'>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizantalAlign="Left" />
</asp:HyperLinkField>
DataNavigateUrlFields is used to gets or set the names of the fields from the data source used to construct the URLs for the hyperlinks in the HyperLinkField object.
'DataNavigateUrlFormatString` is used to gets or sets the string that specifies the format in which the URLs for the hyperlinks in a HyperLinkField object are rendered.