what am I doing wrong

Hi,

I am not able to retrieve the value of a cell in a GridView that is populated without a DataSource and I am not sure what I am doing wrong. I am using the following code in the GridView1_RowCommand method that is called when a user clicks on the "Select" link in the selected row of the Gridview. 

if ( e.CommandName=="Select" )

{

GridView1.DataSource=(
DataTable ) Session ["dt"];

// Convert the row index stored in the CommandArgument

// property to an Integer.

int index=Convert.ToInt32( e.CommandArgument );

 

// Get the last name of the selected  ID  from the appropriate  cell in the GridView control.

 

GridViewRow selectedRow=GridView1.Rows [index];

// The Id is the first cell and has a string value

 

TableCell id=selectedRow.Cells [0];

 

Label2.Text="You selected "+id.Text;  

id.Text returns an empty string. The idex value returns the row selected correctly. I get a null value for id even when I changed Cells[0] to Cells[1]. Incidentally, the above code was given in http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.buttonfield.aspx.

 I also tried using the following code GridView1.DataKeys[Index].Values["id"].ToString() but here too I was not successful.

Any help would be appreciated.


If this answered your question, be sure to mark it as the answer.
0
ks2007
10/31/2007 6:33:59 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

7 Replies
820 Views

Similar Articles

[PageSpeed] 51
Get it on Google Play
Get it on Apple App Store

The index only gives you the index in the page.  For instance, you get 300 rows with your query and your page size is 10.  When you get to page 2 and click on the 2nd record, the index would be 2.  Also, how about DataBind()?

0
g2000
10/31/2007 7:32:26 PM

 GridViewRow selectedRow = GridView1.Rows[GridView1.SelectedIndex];

 

 

Otherwise, post your markup and more of the code behind so I can see where you are databinding, how your page lolad is handinling post backs etc. 


If this post helped you, Mark As Answer.
0
jpuckett
10/31/2007 7:44:10 PM

GridViewRow selectedRow=GridView1.Rows [GridView1.SelectedIndex]; does not work. It causes an ArgumentOutOfRange Exception. The code behind is as follows:

<asp:TemplateField FooterText="Hide" InsertVisible="False" SortExpression="ID" HeaderText="App. ID">

<ItemTemplate >

<asp:Label ID="lbl1" Text='<%# Bind("ID") %>' Font-Size="0.9em" runat="server"></asp:Label>

</ItemTemplate>

<HeaderTemplate>

<asp:LinkButton ID="btnLinkHeader1" Text="App. ID" runat="server" Font-Bold="True" Font-Size="0.9em" ForeColor="White" OnClick="btnLinkHeader1_Click" />

</HeaderTemplate>

<FooterTemplate>

<asp:LinkButton ID="btnLinkFooter1" runat="server" Text="Hide" Font-Bold="True" Font-Size="0.9em" ForeColor="White" OnClick="btnLinkFooter1_Click" />

</FooterTemplate>

<ItemStyle Width="9%" HorizontalAlign="Center" VerticalAlign="Top" Font-Size="0.9em" />

<FooterStyle Font-Bold="True" HorizontalAlign="Center" VerticalAlign="Middle" Font-Size="0.9em" ForeColor="White" />

<HeaderStyle BackColor="#C00000" ForeColor="White" VerticalAlign="Top" CssClass=".report" />

</asp:TemplateField>

 Probably, I need to find the text from the Label Control for the row.  I tried the following but that did not give any value either:

int index=Convert.ToInt32( e.CommandArgument );

 

index=index+1;

 

 

string lbl="lbl"+index;

 

Label lblTest= (Label)FindControl( lbl);

 

 

Label2.Text="You selected "+lblTest.Text;

 


If this answered your question, be sure to mark it as the answer.
0
ks2007
10/31/2007 8:12:22 PM

on the ASPX code you posted there is nowhere that is shown a Select Command.  I need to see your Gridview in it's entirety. 


If this post helped you, Mark As Answer.
0
jpuckett
10/31/2007 8:40:04 PM

Here is the code behind

 

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" Width="100%"  Height="289px" ShowFooter="True" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnPageIndexChanging="GridView1_PageIndexChanging" OnSorting="GridView1_Sorting" OnDataBound="GridView1_DataBound" CellPadding="2" CellSpacing="2" GridLines="None" DataKeyNames="ID" >
            <Columns>
                              
              <asp:TemplateField  FooterText="Hide"  InsertVisible="False" SortExpression="ID" HeaderText="App. ID">
                <ItemTemplate >
                 <asp:Label ID="lbl1" Text='<%# Bind("PERSON_ID") %>' Font-Size="0.9em" runat="server"></asp:Label>               
                </ItemTemplate>
                <HeaderTemplate>            
                    <asp:LinkButton ID="btnLinkHeader1" Text="App. ID" runat="server"  Font-Bold="True" Font-Size="0.9em" ForeColor="White" OnClick="btnLinkHeader1_Click" />                               
                </HeaderTemplate>               
                <FooterTemplate>                   
                    <asp:LinkButton ID="btnLinkFooter1" runat="server" Text="Hide" Font-Bold="True"         Font-Size="0.9em" ForeColor="White" OnClick="btnLinkFooter1_Click" />                   
                </FooterTemplate>
                <ItemStyle Width="9%" HorizontalAlign="Center" VerticalAlign="Top" Font-Size="0.9em" />
                    <FooterStyle Font-Bold="True" HorizontalAlign="Center" VerticalAlign="Middle"  Font-Size="0.9em" ForeColor="White" />
                  <HeaderStyle BackColor="#C00000" ForeColor="White" VerticalAlign="Top" CssClass=".report"  />
                </asp:TemplateField>

             <asp:CommandField CausesValidation="False" InsertVisible="False" ShowCancelButton="False" ShowSelectButton="True" CancelText="" DeleteText="" EditText="" InsertText="" NewText="" UpdateText="" HeaderText="View Details" ShowHeader="True" >
                    <ItemStyle Font-Size="0.9em" VerticalAlign="Top" HorizontalAlign="Center"/>
                    <ControlStyle Font-Bold="True" ForeColor="White" />
                    <HeaderStyle ForeColor="White" Font-Size="0.9em" VerticalAlign="Top" />
                </asp:CommandField>
              
              
            </Columns>
            <FooterStyle Font-Underline="True" BackColor="#C00000" Font-Bold="True" HorizontalAlign="Center" VerticalAlign="Middle"/>
            <HeaderStyle Font-Bold="True" Font-Underline="True" BackColor="#C00000" />
           
            <AlternatingRowStyle BackColor="#EDEBD5" CssClass="alt" />
        <PagerStyle BackColor="#EDEBD5" />
        </asp:GridView>


If this answered your question, be sure to mark it as the answer.
0
ks2007
11/1/2007 2:08:39 PM

if(e.Command == "Select")

{

   int selectedIndex = GridView1.SelectedIndex;
   GridViewRow row = GridView1.Rows[selectedIndex];
   string name = row.Cells[0].Text;  
}


If this post helped you, Mark As Answer.
0
jpuckett
11/1/2007 2:50:09 PM

Thanks.


If this answered your question, be sure to mark it as the answer.
0
ks2007
11/1/2007 7:16:48 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...

WRONG WRONG WRONG
Name: ROZ mandelcorn Email: thunderstaratearthlinkdotnet Product: Firefox Summary: WRONG WRONG WRONG Comments: as a long-time user, first of netscape and, mozilla firefox, i must say i DO NOT LIKE THE PLACEMENT of TABS in version 4. nor do i like the whole rearrangement of the upper/navigation portion of firefox. I NEED/WANT MY TABS DIRECTLY ABOVE MY WINDOWS AS IT IS NOW....NOT ON TOP OF THE BROWSER. I WANT MY FILE-EDIT-ETC-MENU on TOP. ETC. AT LEAST GIVE US THE CHOICE AS TO WHICH FORMAT WE WANT....CAN YOU SAY CUSTOMIZATION! I WILL NOT USE 4 IF THIS IS THE WAY IT IS. [just ...

How do the different data presentation controls equate in ADO.net?
How does Visual Studio create ado.net equivalents of the datagrid's and gridview's columns and rows?  I am assuming they make a DataRow for each of the rows.  Am I on the right track with this?Is the table created by gridview basically a dataset from ADO.net?  Can anyone offer any guidance with this?Thanks to everyone that has ever helped me with any posts. Hi newbie2C#, The row in datagrid is called DataGridItem and in gridview is called GridViewRow. They are all inherits TableRow Class. DataRow is only one kind of row in GridView control. There are many other t...

Embedded .Net control runs under wrong version (1.1 / 2.0)
We have a .Net 1.1 control that is embedded in a webpage with the <OBJECT> tag:  <OBJECT style="Z-INDEX: 101; WIDTH: 100%; HEIGHT: 100%" classid="http:./scanner/DCCScanner.dll#DCCScanner.ReplaceImage"   name="ctlScanForm">  </OBJECT>  This assembly works great under .Net 1.1. However, when the user installs .Net 2.0, the assembly is ran under the .Net 2 framework, and it fails. We are not yet ready to release a .Net 2.0 version, and we cannot control what versions of .Net are loaded on the end user machines....

Trouble controlling .net controlls with CSS
 I am trying to control the :hover css property on <asp: Button> controls. Basically the css is working correctly to set the initial look of the button, but nothing is happening when I hover over the button. Here is my code:CSS:.sidebarButton{    width: 168px;     height: 45px;     background-color: #666666;     text-align: left;    font-weight: bold;    font-family: Century Gothic;    font-size: 13pt;    color: White;    border-style: none;  ...

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...

What is the real difference between Server Controls and .NET controls?
This was a class discussion question and I'd like to know, what exactly are the differences? Is there a clear answer? .NET framework controls are server controls. The only difference that I can think of is that the .NET controls are part of the FCL (Framework Class Library). HTH,RyanRyan OlshanASPInsider | Microsoft MVP, ASP.NEThttp://ryanolshan.comHow to ask a question...

.NET Thinks variable = nothing (.NET is wrong)
the code inside the If never gets called. The runtime thinks the value is nothing. As you can see, it is not nothing. Why is this happening? Try using the IsNot operator rather than <>, i.e. If _SecondCategory IsNot Nothing Then    _____________________________________Custom, onsite .NET training and IT SupportPlease Mark this post as "ANSWER" if it's helpful.  Try this instead:If _SecondaryCategory > 0 Then Ricardo D. SanchezGDL Technologieswww.gdltec.netwww.ricardodsanchez.comPlease remember to click "Mark as Answer" on this po...

Add .Net Control to Web Custom Control
 I want to add .NET Control (like: TextBox, Button...) to Web Custom Control.How i can do it? Alphavn That's called a composite control -- "Mark As Answer" if my reply helped you -- Hello Guys What gunteman said, that is one approach and in most cases the right one.But its also possible to add child controls to a Control thats inherit from WebControl itself. Be sure to implement INamingContainer to that inherited Control too, thats the important point of that approach.--Microsoft MVP - Visual Developer ASP / ASP.NET- http://www.aspnetzone.de/blogs/peterbuch...

Architecture: .Net Controls vs. Custom Controls
I'm interested in managing users. i.e. Logins and profiles, etc.My question is, should I be using things like the Login control or should I program these from scratch? I dont like the fact that .NET creates so many things for me. This level of abstraction terrifies me and it feels like I have no control over the code .I've done a few huge sites in Classic ASP (million-hits-per-day type sites) and I'm not sure how robust certain controls are, or when to use them.Is, for example, the Login control aimed at newbies to .NET? Should it be used for the larger projects with thousands of...

DW control in VB. NET custom control
Hi Just attempting to investigate some DW.NET aspects. In regards to building a ancestor object with VB.NET, why is when I create a datawindowcontrol custom control that any reference to it is like (assuming that u_dw is the name of the DW control): custom_control.u_dw.DatawindowObject = "myDW" instead of simply: u_dw.DatawindowObject = "myDW" If I create a .NET custom control with a button on it, I can directly reference the button's methods and properties without having to qualify it with the custom control's name. When I was developing with po...

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...

Making the .NET calendar control as a floating control
Hi, I need to implement the .NET calendar control as a floating control. Is it possible to do that? If so, can you let me know how to do that?With Regards,Saravana Kumar M R  You could use the calendar extender control in the AJAX control toolkit. You can download the toolkit here, and there is a sample here.My Blog...

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.:-)...

DataWindow.Net a nominee for Best .NET Libraries / Controls
http://sys-con.com/dotnet/readerschoice2004/ Bruce Armstrong [TeamSybase] http://www.teamsybase.com TeamSybase blogs: http://www.teamsybase.net/blogs Two 3rd party books on developing with PowerBuilder http://www.pb9books.com?source=newsgroups Need code sample? Check out CodeXchange: http://www.codexchange.sybase.com Preach the gospel at all times. If necessary, use words. - Francis of Assisi http://www.needhim.org --------------------------------------------------------------------- NOTE: If you are using Google to post messages to the newsgroups, you will *no...

Web resources about - what am I doing wrong - asp.net.presentation-controls

Man Blames The Apple Watch For Burns On His Wrist
The Apple Watch is designed to be worn for extended periods of time on the wrist, which is why it doesn’t come as a surprise that in the past, ...

Stephen Colbert Gives Thanks For Donald Trump
I'd be laughing a little harder if all this fascist was doing was giving comedians like Stephen Colbert some food for fodder. That said I am ...

Jessa Duggar Seewald’s Siblings Star In ‘Ad’ For Fake Money As Critics Boycott ‘Jill & Jessa: Counting ...
Jessa Duggar Seewald’s husband recently enlisted his sisters-in-law to help him film a video that looks an awful lot like an ad for a company ...

Russia warns of 'serious consequences' after Turkey downs warplane
CNN Russia warns of 'serious consequences' after Turkey downs warplane CNN Istanbul (CNN) The conflict in Syria, a years-long maelstrom of ...

Get ready for Li-Fi, a technology 100 times faster than Wi-Fi
The world might eventually have to shift its reliance on Wi-Fi to Li-Fi, an alternative technology that scientists say can reach speeds of 1 ...

Jeff Bezos is proud of his rocket, Elon Musk not impressed
Amazon Founder and CEO Jeff Bezos saw his private spaceflight company, Blue Origin, hit a major milestone today by launching and landing its ...

Cheap Gas Fueling Expanded Thanksgiving Travel Day
Thanksgiving travel begins with gas prices low, terrorism fears high

Amazon May Not Have Requested That NYC Yank Nazi-Themed Ads
Did Amazon Studios really request that its controversial New York City subway ads for the ambitious drama “The Man In The High Castle” be removed? ...

Yet Another Dell Root Certificate Found Out
It looks like things are not all that rosy over at Dell, as yet another Dell root certificate has been discovered, which is in addition to what ...

Security tight after Tunisia bomb blast kills at least 13
At least 13 people were killed in Tuesday's apparent suicide bombing of a Tunisian presidential guard bus in central Tunis, prompting the government ...

Resources last updated: 11/25/2015 10:13:26 AM