Solved

ASP.net VB.net change text of template button whe clicked

Posted on 2014-01-04
6
690 Views
Last Modified: 2014-01-05
Hi
    I am using the following code to change the backcolor of an ASP.net GridView cell
    What VB.net code do I use to change the text of the actual button to?
I tried
    Dim btn1 As Button = DirectCast(FindControl("MyButton1"), Button)
                btn1.Text = "btn1"
but got an error "Object not set to an instance of an object"


    If (e.CommandName = "MyButton1") Then
                ' Retrieve the row index stored in the CommandArgument property.
                Dim index As Integer = Convert.ToInt32(e.CommandArgument)

                ' Retrieve the row that contains the button
                ' from the Rows collection.
                Dim row As GridViewRow = GridView_Questions.Rows(index)

                row.Cells(0).BackColor = Drawing.Color.DarkGreen

                row.ForeColor = Drawing.Color.White



            ElseIf (e.CommandName = "MyButton2") Then
                ' Retrieve the row index stored in the CommandArgument property.
                Dim index As Integer = Convert.ToInt32(e.CommandArgument)

                ' Retrieve the row that contains the button
                ' from the Rows collection.
                Dim row As GridViewRow = GridView_Questions.Rows(index)

                'row.BackColor = Drawing.Color.LightGray


                row.Cells(0).BackColor = Drawing.Color.Green

                row.ForeColor = Drawing.Color.White



            ElseIf (e.CommandName = "MyButton3") Then
                ' Retrieve the row index stored in the CommandArgument property.
                Dim index As Integer = Convert.ToInt32(e.CommandArgument)

                ' Retrieve the row that contains the button
                ' from the Rows collection.
                Dim row As GridViewRow = GridView_Questions.Rows(index)

                'row.BackColor = Drawing.Color.LightGray


                row.Cells(2).BackColor = Drawing.Color.SlateGray

                row.ForeColor = Drawing.Color.White


            ElseIf (e.CommandName = "MyButton4") Then
                ' Retrieve the row index stored in the CommandArgument property.
                Dim index As Integer = Convert.ToInt32(e.CommandArgument)

                ' Retrieve the row that contains the button
                ' from the Rows collection.
                Dim row As GridViewRow = GridView_Questions.Rows(index)

                'row.BackColor = Drawing.Color.LightGray

                row.Cells(3).BackColor = Drawing.Color.DarkOrange

                row.ForeColor = Drawing.Color.White


            ElseIf (e.CommandName = "MyButton5") Then
                ' Retrieve the row index stored in the CommandArgument property.
                Dim index As Integer = Convert.ToInt32(e.CommandArgument)

                ' Retrieve the row that contains the button
                ' from the Rows collection.
                Dim row As GridViewRow = GridView_Questions.Rows(index)

                'row.BackColor = Drawing.Color.LightGray


                row.Cells(4).BackColor = Drawing.Color.DarkRed

                row.ForeColor = Drawing.Color.White


            End If
0
Comment
Question by:murbro
  • 3
  • 2
6 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 250 total points
ID: 39756585
Hi murbro;

Are you sure that the FindControl function returned a control? Possible reasons for retuning a null/Nothing is that the control name was mis-spelled, the TextBox is embedded in another container which you have no direct access from your current location. First check to see if you got a control back then operate on that control if is nothing.

Dim btn1 As Button = DirectCast(FindControl("MyButton1"), Button)
If ( Not btn1 Is Nothing )
    ' You have a valid control, you can use it
Else
    Response.Write("Control not found.....")
End If

Open in new window

0
 
LVL 14

Assisted Solution

by:dejaanbu
dejaanbu earned 250 total points
ID: 39756902
Try  this:

Dim btn1 As Button = DirectCast(e.row.FindControl("MyButton1"), Button)
 If ( Not btn1 Is Nothing )  then btn1.Text = "btn1"
0
 

Author Comment

by:murbro
ID: 39757010
I tried the following and the control was not found. This makes no sense because
the if statement recognizes a button called "MyButton1"

            If (e.CommandName = "MyButton1") Then
                ' Retrieve the row index stored in the CommandArgument property.
                Dim index As Integer = Convert.ToInt32(e.CommandArgument)

                ' Retrieve the row that contains the button
                ' from the Rows collection.
                Dim row As GridViewRow = GridView_Questions.Rows(index)
                If row.Cells(0).BackColor = Drawing.Color.DarkGreen Then
                    row.Cells(0).BackColor = Drawing.Color.White
                Else
                    row.Cells(0).BackColor = Drawing.Color.DarkGreen
                End If

                Dim btn1 As Button = DirectCast(FindControl("MyButton1"), Button)
                If (Not btn1 Is Nothing) Then
                    ' You have a valid control, you can use it
                Else
                    Response.Write("Control not found.....")
                End If
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:murbro
ID: 39757015
Also here is the markup code. Perhaps the error has something to do with the fact that the GridView is in an Update Panel

            <asp:UpdatePanel runat="server" id="UP1">
<contenttemplate>
            <asp:GridView ID="GridView_Questions" runat="server" BackColor="White"
                BorderColor="#3366CC" BorderStyle="Solid" BorderWidth="1px" CellPadding="4"
                Height="147px" Visible="False" Width="795px">
                <Columns>

                      <asp:TemplateField HeaderText="1" HeaderStyle-Font-Size ="Small"
                                            ControlStyle-Font-Bold ="false" ControlStyle-Forecolor ="#006600"
                                            ControlStyle-Font-Size ="Smaller">
                    <ItemTemplate>
                        <asp:Button ID="Button1" runat="server"
                      CommandName="MyButton1"
                      CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
                      Text="No" />
                    </ItemTemplate>
0
 

Author Closing Comment

by:murbro
ID: 39757253
Thanks for the help
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39757368
Not a problem, glad to be of help.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question