Calling a method on DataBound field 's DataField - asp.net

I want to call a method on databound field on datafield. e.g.
<asp:BoundField DataField="TitlePLang" HeaderText="Batch Title" /> This is simple bound field but I want to do something like this.
<asp:BoundField DataField='<%# Eval(getCultureSpecificTitle("TitlePLang","TitleSLang",true))%>' HeaderText="Batch Title" />
Where getCultureSpecificTitle is a method in code behind, but its giving error that bound field doesn't allow data binding.
So please help how can I do this....

Well this is not possible using the bind files but you can make use of the TempalteField avaialbe in the grid view will allows you to do this easily.
If you want to achieve same from the code behind than you make use of OnRowDataBound event available for the datagrid view will do the work for you.

Related

Publish two types of field information on same column in ASP.Net grid view

I want to publish two types of information on same column in ASP grid view.
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:hyperlinkfield text="Detail" datanavigateurlfields="name" datanavigateurlformatstring="LeadInformation.aspx?name={0}" />
The first column is a BoundField and second one is hyperlinkField. These two types of information will display separately on two columns.
or else I can do like this.
<asp:HyperLinkField DataNavigateUrlFields="name" DataNavigateUrlFormatString="LeadInformation.aspx?name={0}"
DataTextField="name" HeaderText="User Name" SortExpression="name" />
Then it will display all information on one column as a hyperlink.
I just want to display name as it is. (not as a hyperlink). And in same column,add a hyperlink like above.
Please help me.
Make it a TemplateField. Then add a Label and a Hyperlink. Then for the Text of the Label and the NavigateURL for the hyperlink use Bind("ColumnName"). (or on the design mode right click and select Databindings and enter Bind("ColumnName") for the properties.
You can refer the below MSDN link which explains how to show two values in a single column (FirstName and LastName in this case)
MSDN
The answer is absolutely correct. A template field allows you to display anything you like in a gridview, rather than the standard ASP:NET gridview column controls. The price you pay is that it then becomes a bit harder to get access to the values in the controls. You will find it's often useful to use the CommandName and CommandArgument properties to get at values of the controls.
You have a fair learning curve ahead of you, I'm afraid, and certainly the question is too broad to be answered here. Try this article, or searching for "ASP.NET gridview templatefield".

ASP.NET 4 GridView - Pulling a Hyperlink out of a Database

I have a GridView bound to a SqlDataSource.
I'm pulling hyperlinks which point to Job Descriptions stored in a separate web space, out of a database and placing them in the GridView.
These are full Hyperlinks such as "Http://stackoverflow.com/"
Originally the GridView column was a simple BoundField like this:
<asp:BoundField DataField="JobDescription" HeaderText="JobDescription"
SortExpression="JobDescription" />
So I started trying to convert it into a hyperlink field.
<asp:HyperLinkField DataNavigateUrlFields="JobDescription"
DataTextField="JobDescription"
HeaderText="JobDescription"
SortExpression="JobDescription"
Target="_blank"
NavigateUrl="{0}" />
This produced the desired result, but I can no longer edit that column in the GridView. When it was a BoundField I could edit the item, but could find no way to make it into a hyperlink.
Either way will work...
I either need the HyperLinkField to be updatable, or I need the BoundField to be formatted as a Hyperlink with what it pulls directly from the database.
I appreciate the help.
Use a Template Field. So your can define your normal view and editing view.
Grrr found the answer:
<asp:BoundField DataField="JobDescription" HeaderText="Job Description"
SortExpression="JobDescription"
DataFormatString="<a target='_blank' href='{0}'>Text</a>"
HtmlEncode="False" />
You don't need a template field. That HtmlEncode property must be set to false in order for html in DataFormatString to be rendered as html, otherwise it changes all of your characters into the equivalent of stuff like...
The Entity Numbers here: http://www.w3schools.com/tags/ref_entities.asp

How can I create an object in .ascx?

I've wrapped the asp:DropDown control so that I can create a lot of instances of it, all of which using the same additional functionality. I want to be able to create this object completely in the .ascx rather than using the code behind. I'm almost there, with the exception of the ListItems.
Here's what I have thus far, can anyone help me to figure out how to get the list items to populate?
<Control:DropDown ID="choice" runat="server" DropDownListLabel="Some Choice:"
QueryString="choice" SelectedIndex="0" ListItems='<%# new ListItemCollection(){
new ListItem("<no filter>", "-1"), new ListItem("Yes", "y"), new ListItem("No", "n")
} %>' />
Everything is working, with the exception of the ListItems, although the list items do work as expected from code behind. Any help on how I can get the ListItems accessor to call properly?
Thanks in advance,
Brett
You probably should have created your control to inherit from System.Web.UI.WebControls.DropDownList then override rendering and add properties that you want extra, this would allow you to something like the following:
<Control:DropDown id="mycontrol" runat="server" DropDownListLabel="Some Choice:" ..>
<asp:ListItem Text="<no filter" value="-1" />
...
</Control:DropDown>
Where DropDownListLabel is an added property. Of course in your case you'd add QueryString as a property as well

vb asp.net : A way to avoid repeating a code in common in ItemTemplate and EditItemTemplate

I'm a newbie in asp.net.
When using FormView, there is a big amount of code in ItemTemplate, EditItemTemplate and InsertItemTemplate which is almost identical.
For example:
<asp:ListBox ID="ListBox2" runat="server" Rows="1" CssClass="field"
DataSourceID="StatusList" DataTextField="DESCRIPTION"
DataValueField="STAT_ID" SelectedValue='<%# Bind("STAT_ID") %>'>
</asp:ListBox>
(Note: at the exception that Eval() would be used instead of Bind() in ItemTemplate)
I've been trying to avoid repeating this code but without the expecting result:
ListView allows the use of LayoutTemplate - but I didn't see any examples that insert this kind of code in LayoutTemplate. And inserting this code in LayoutTemplate would result in an error.
DetailView allows to produce code automatically but I'd like to use a specific design (for ex. using "fieldset" that encompasses some fields).
What would be the best way to avoid repeating this kind of code ?
You don't have to much choice about seperately specifying the Bind/Eval part, but you do have some control over the other pieces. You can make a custom UserControl that contains your layout.
Usually I include a property on this usercontrol called "Mode" which I either set to Edit or View, then based off of this property I change enabled/visible properties on the controls. You'll also need to include a property for each value you want bound/displayed in the usercontrol.
Put some labels, textboxes, etc... in your designer and hook them up to properties in your code behind, put the usercontrol on your page in your item/edit template and eval/bind to your data to the various properties (make sure to set the mode so it displays right).

Retrieving Row Data from a Column in DataGrid in ASP.NET

I've created a Column button in datagrid using this code
<Columns>
<asp:ButtonColumn Text="Resend" ButtonType="PushButton" CommandName="Resend"></asp:ButtonColumn> </Columns>
But I don't know where to access the event of this Button, also I would like to know how to retrive the row data of this button thanks.
Here is an msdn example of how to this
One more sample with simple code.
Try using FindControl.
See this example:http://www.jigar.net/articles/viewhtmlcontent4.aspx

Resources