Getting Information from the Controls (TemplateField or BoundField ) from any of the Data Presentation Controls

Hello Everybody,

I have a very important question for me.

For example, I have a GridView.
It has three BoundFields and one TemplateField

I want to get information from any of the record (row,line etc ) using any of the BoundFields or TemplateFields.
And also I want to accomplish this not only with GridView but also other Data Presentation Controls, DetailsView FormView and Repeater.

Shortly, Can anybody help me how to refer Controls in GridView, FormView, DetailsView, Repeater

PS: I have searched in Google and many sites,forums. But I couldn't see any solution and see that many people ask the same question but few answers which is no use mostly.

Thanks for your replies

 

 

 

0
antoniomoreno
12/19/2006 3:43:54 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

2 Replies
923 Views

Similar Articles

[PageSpeed] 54

Here's a simple on how to retrieve a value from a BoundField and a TemplateField for a GridView control. The same basic procedure would apply to all presentation controls.

ASPX

<asp:gridview id="GridView1" runat="server" autogeneratecolumns="False" datakeynames="ProductID"
 datasourceid="ObjectDataSource1" onrowdatabound="GridView1_RowDataBound">
 <columns>
  <asp:boundfield datafield="ProductID" headertext="ProductID" insertvisible="False"
   readonly="True" sortexpression="ProductID" />
  <asp:templatefield headertext="ProductName" sortexpression="ProductName">
   <itemtemplate>
    <asp:literal id="litProductName" runat="server" text='<%# Bind("ProductName") %>' />
   </itemtemplate>  
  </asp:templatefield>
 </columns>
</asp:gridview>
<asp:objectdatasource id="ObjectDataSource1" runat="server" selectmethod="GetData"
 typename="NorthwindTableAdapters.ProductsTableAdapter">
</asp:objectdatasource>


CODE-BEHIND

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
 GridViewRow gvr = e.Row;

 switch (gvr.RowType)
 {
  case DataControlRowType.DataRow:
  {
   // Retrieve ProductID
   int productId = Int32.Parse(gvr.Cells[0].Text);

   // Retrieve ProductName
   Literal litProductName = gvr.FindControl("litProductName") as Literal;

   if (litProductName != null)
   {
    string productName = litProductName.Text;
   }

   break;
  }
 }
}


Thanks, Ed

Microsoft MVP - ASP/ASP.NET

0
ecbruck
12/19/2006 8:41:02 PM

Thank you for "ecbruck" for his informative answer.

I will continue this subject under this thread which is related to this topic that I started. 

http://forums.asp.net/thread/1502098.aspx


0
antoniomoreno
12/21/2006 1:43:07 PM
Reply:

Similar Artilces:

Data presentation control in another Data presentation control
 Hello,I want to make a menu with datalist or gridview control.Basically,the menu will be like this,Categories     Sub Categories.For example, for COMPUTING category there will be NOTEBOOKS DESKTOPS etc.There will be a image for CATEGORIES and linkbuttons with Subcategories.If I put a datalist inside a datalist and a link button inside the second datalist visually it satisfies my need.However,CommandArguments come from database to linkbutton.Andwhen one of these command buttons is cliked it must go to proper page.I dont know how to wrie an event_handler ,specifically, for...

Theming the ControlStyle for BoundField controls within data presentation controls.
Hi all, I was wondering if you can set a default width for template fields and/or BoundField controls within a GridView or  DetailsView / FormView controls? I can change the control widths of bound data control fields, including template fields, by setting the ControlStyle. But if I have a bunch of fields in a DetailsView control for example, it's a pain to have to set the ControlStyle property for each field. I would like to do it in one fell swoop. If I add the following to my skin file, then I get server control rendering errors on my pages. The controls can't be rendered becuase I...

Bind Data to an Image Control not in a Data Presentation Control
I've been searching for two days but I don't really know what to search for.I'm making a site for a photographer.  The Default.aspx page displays Albums by showing a title, image and description from the database table Albums. Click an album to view the contents and it passes the QueryString which is AlbumName.  The ViewAlbum.aspx page uses the QueryString to display an UpdatePanel holding a Large Image and a Repeater full of dynamically filled ImageButtons for thumbs.  Both are from the database table Photos and are retrieved using an ObjectDataSource. Click the ...

ajax control in data presentation controls
can i add ajax controls (MaskedEdit) to presentation controls (dataGrid, dataForm)?? Hi,of course. But keep in mind that those controls are extenders, so you append them to an existing control. Just convert your GridView / DetailsView-fields into templated fields and add the extender you'd like to use to the controls.RegardsMarco Buerckel Don't forget to mark posts that were helpful as answers. Actually, if you looked at the source code for the Masked Edit Extender, you'd see  [TargetControlType(typeof(TextBox))]Meaning, targeting any control type other than textboxes...

data presentation controls (how to call the individual controls)
Hi please help me.,i have this question., example i create a site with one control, like textbox. when i double click on the white page, the page load event, in the intellisense, the textbox is visible, but when a formview with a textbox inside, in the same page load event, intellisense, the textbox is not available. how can i do the simple intellisense calling where the textbox is just outside a data presentation control like formview. i mean by this intellisense calling is in VB code view., thanks JFRBPH12™  The textbox is within the formview then you can find it like this...

How to apply ID to each control & find that specific control again to get data from the control?
Hi. i have problem with my codes & need someone help to fix my codes. my case is in addRow function, when i click the button, i will add textbox & dropdownlist control dynamically. but after click Save button, i got error msg  " File saved failed. Reason is as follows System.NullReferenceException: Object reference not set to an instance of an object. at Default2.Save_Click(Object sender, EventArgs e) in C:\Documents and Settings\My Documents\Visual Studio 2008\WebSites\WebSite2\Default2.aspx.vb:line 290  (objStreamWriter.WriteLine(CStr(MapTable.Rows.Count...

Run an event whose control is located in Data Presentation Controls
Hey, How can we fire an event which belongs to such as a button which is located in Data Presentation Controls such as a gridview.For example : Assume that my page contains a gridview control which is contains a button and the button name comes from database by gridview control but i wanna fire button control's own event independently of the GridView. Thanks and Regards.I am not a perfect programmer, but i have perfect programmers' habits [ i think so :)] Hi:   You can use CommandArgument to distinguish different buttons in the same column: <asp:Button ID="Button2&...

How to add the data present in the controls to a table control without any database involvement.
Hi All,i am four controls where i will write the text and once i click the add button those should get added to the table .i want 6 columns fixed in the table and rows as many as required.i tried doing that taking a table server control and able to load the table.but the problem is whenver i am adding a new row the old one is gfetting overwritten by this and at the end i am getting only one row.  System.Web.UI.WebControls.Table t1 = new System.Web.UI.WebControls.Table();  i have declared the below one global TableRow tr; TableCell tc1; TableCell tc2; TableCell tc...

Getting the control id of a control inside gridview control
 uhmm.. a little bit confusing regarding my subject because i dont know how to say it...by the way my problem is how can i get the id of a control (e.g. textbox) inside the gridview... and the textbox is in the templatefield... is there any way i can get its id??  hoping i could get a reply as soon as possible... Regards,MhaeyPlease remember to click “Mark as Answer” on the post that helps you.. =) What do you want's to do by getting the id of the Text box? Do you want's to find particular Textbox with certain id ? Kamran ShahidSr. Software Engineer(MCP,MCAD.net,MC...

Visibility control of Panel control and its affect on data controls
Thought I would toss this out here this morning and perhaps someone else had run across the same issue and had found a good way to accomplish what I want. First let me state what I'm attempting to do and the problem that I've run into.  GridView1 has a collection of rows in a SQL table that I want to present to the user. I've provided them the options of sorting and selecting only from the GridView control.  Orgionally, I had created a detail view that was being presented in Edit mode and then customized the layout of the edit item template to suit my needs.&nbs...

using javascript getting value from textbox when edit button is clicked and both controls are present in gridview control
Hi All using javascript I want to get value  from textbox when edit button is clicked and both controls that is texbox and button are present in gridview control .Alok Hi aloksinha83, Please see this post: http://forums.asp.net/t/1069245.aspx Thanks,Qin Dian TangMicrosoft Online Community SupportPlease remember to mark the replies as answers if they help and unmark them if they provide no help....

Is there any way to control the width of the Gridcolumn when the data is present and not present
I would like to know if there is any way you can control the gridview column width. Thanks in advance. Digish Hi,   try these link http://www.eggheadcafe.com/community/aspnet/17/10004157/setting-gridview-column-w.aspx http://msdn.microsoft.com/en-us/library/ms178296(VS.80).aspxBabaIf this post was useful to you, please Don't forget to click "Mark as Answer". Thanks You.:-)...

How do I control one control from another control?
I have a page page1.aspx which is within a master page. On page I have a table with two cells. Within each cell I have placed two controls (ascx) that I have developed. Now I'd like to be able to get these two controls to interact. One needs to update the other. One has some search criteria that the other one needs to update its gridview. One needs to make the other visible/not visible at certain times. Out of the box neither control can see each other at all. Is there a good article or someplace I can find some information on how to do this?  AllAboutFocus.com. Information Technolo...

Custom Control within a Control Control
I am developing a custom controls that will render headers for different web sites. Within these controls, I want to place another custom control that will change the language. I created the language control and had it implement IPostBackDataHandler. This control works fine if I drag it on a new web form, but if I tell a header custom control to render it problems occur. The Language Selector custom control does not have a Page member anymore and the event handlers do not work. Does anyone have any experience with doing something similiar? Anyone have any suggestions? Thanks, ...

Web resources about - Getting Information from the Controls (TemplateField or BoundField ) from any of the Data Presentation Controls - asp.net.presentation-controls

ERROR: “Index was out of range” when Accessing the DataKeys Collection in a Paged GridView
... While working on a legacy ASP.NET WebForms application I stumbled into the following error: I had a paged GridView with a LinkButton in a TemplateField ...

Scott On Writing.NET
... While working on a legacy ASP.NET WebForms application I stumbled into the following error: I had a paged GridView with a LinkButton in a TemplateField ...

Resources last updated: 12/7/2015 1:40:22 AM