ASP.NET - Get values from a gridview row when a button has been clicked in the same row

Asked By Annie X on 14-May-10 05:14 AM

 Hi, hope you can help me

I have a gridview which contains a button and a label

When i click the button (lnkBtnEdit) i want to redirect to another page with the button (lnkBtnEdit) and label (lblSlideUrl) values.

I can get the buttons (lnkBtnEdit) value no problem but I cannot get the label (lblSlideUrl) value. I have tried using findcontrol but i keep getting errors.

Any surgestions on how to get the labels value.

Many Thanks
AnnieX

<!--Webpage-->

<asp:GridView ID="gvSlides"
             runat="server"
             Width="100%"
             AutoGenerateColumns="False" 
             HeaderStyle-CssClass="NCT-H9-BK-B"
             HorizontalAlign="Center"
             BorderStyle="Solid"
             BorderWidth="0px"
             BorderColor="#CCCCCC"
             CellPadding="2"
             CellSpacing="2">
            <Columns>   
                
               <asp:TemplateField HeaderText="Slide Url">
                    <ItemTemplate>
                        <asp:Label ID="lblSlideUrl" runat="server" Text='<%# Eval("SlideUrl")%>' CssClass="NCT-H9-BK"></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle HorizontalAlign="Left" VerticalAlign="Bottom" Width="30%" />
                    <ItemStyle HorizontalAlign="Left" />       
                </asp:TemplateField>
                
                <asp:TemplateField HeaderText="Edit">
                    <ItemTemplate>                       
                        <asp:LinkButton ID="lnkBtnEdit" CommandName="lnkBtnEdit" CommandArgument='<%#DataBinder.Eval(Container.DataItem, "IDtblSlide")%>' runat="server" CausesValidation="false">Edit</asp:LinkButton>
                    </ItemTemplate>
                    <HeaderStyle HorizontalAlign="Right" Width="5%" />
                    <ItemStyle HorizontalAlign="Right" CssClass="NCT-H9-BK" />       
                </asp:TemplateField>
                             
            </Columns>
            <RowStyle BackColor="#E5F4FF" />
                  
                    <AlternatingRowStyle BackColor="#E0E0E0" />
            <HeaderStyle CssClass="NCT-H9-BK-B" BackColor="LightGray"></HeaderStyle>
        </asp:GridView>

<!--Code behind-->

 Protected Sub gvSlides_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvSlides.RowCommand

        If e.CommandName = "lnkBtnEdit" Then
            Dim varRowID As String = e.CommandArgument

''''''''What would i write here to get the value of the label in this row

            Response.Redirect("Index.aspx?varRowID=" & varRowID & "&Url=" & Url, False)

        End If

    End Sub















kiran k replied to Annie X on 14-May-10 05:25 AM
have u try this

Label lbl = (Label)e.Row.Cells[XXX].FindControl("label8");

it will works , try it
Annie X replied to kiran k on 14-May-10 05:41 AM
Thanks. I have convert the c# to vb which is

Dim


lbl As Label = CType(e.Row.Cells(XXX).FindControl("label8"), Label)

but i keep getting the following error msg

'Row' is not a member of 'System.Web.UI.WebControls.GridViewCommandEventArgs'.

Ramendra Kumar replied to Annie X on 14-May-10 05:50 AM
hi, this a very confussing question i think before 1.7 year i also face same problume ,you r getting this problume bcz u use eval, use bind method then you will get your value in any varirable bcz eval is read only, if want to know more search difference between eval and bind () in asp.net

For Getting much more Response From Experts always mark as HelpFul Reply If u Reallly Got help

Annie X replied to Ramendra Kumar on 14-May-10 05:58 AM
Hi. I'm quite new to this. Do you mean

<asp:Label ID="lblSlideUrl" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "SlideUrl")%>' CssClass="NCT-H9-BK"></asp:Label>



Anoop S replied to Annie X on 14-May-10 06:12 AM
for read-only values such as Labels you can use the Eval() statement, and for read-write values such as TextBoxes (also known as "two-way databinding") you can use the Bind()
DL M replied to Annie X on 14-May-10 07:17 AM
you can also do using javascript show  here article

http://www.netomatix.com/development/gridviewclientsideaccess.aspx
Ramendra Kumar replied to Annie X on 14-May-10 07:35 AM
ya , that value, and one more thing anoop told you to that u can use eval for label, he is also right but actually he don't know ur full senerio, bcz you r also getting value from that label, means after command button click u wolud like to fetch the value of that label, so here anoop wrongs, so use data bind instead of eval, and for ur kinf information i can give you the complete solution, but i told you that search difference bettwen Bind() and eval, so use this search and  improve your skill, best of luck
Annie X replied to Ramendra Kumar on 14-May-10 10:30 AM
Hi, thanks for your help.

I'm still no closer mate. I under stand the difference but i still cannot get the value from the label when i click the submit button.

What's the solution I cannot be far of

Thanks again
All the best
AnnieX
Super Man replied to Annie X on 15-May-10 04:09 AM

Dim l As Label = CType(gvSlides.Rows(gvSlides.SelectedIndex).Cells(0).FindControl("lblSlideUrl"), Label)
Dim lval As String = l.Text

Dim lb As LinkButton = CType(gvSlides.Rows(gvSlides.SelectedIndex).Cells(0).FindControl("lnkBtnEdit"), LinkButton)
Dim lbval As String = lb.Text
Super Man replied to Annie X on 15-May-10 04:09 AM

Dim l As Label = CType(gvSlides.Rows(gvSlides.SelectedIndex).Cells(0).FindControl("lblSlideUrl"), Label)
Dim lval As String = l.Text

Dim lb As LinkButton = CType(gvSlides.Rows(gvSlides.SelectedIndex).Cells(0).FindControl("lnkBtnEdit"), LinkButton)
Dim lbval As String = lb.Text
Annie X replied to Super Man on 16-May-10 06:28 AM
Hi. Thanks for the help but i keep getting this error

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Annie X replied to Ramendra Kumar on 17-May-10 10:40 AM
Hi Ramendra,

Could you show me the solution you came up with

Many thanks
AnnieX