?
Solved

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

Posted on 2014-01-04
6
Medium Priority
?
697 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:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 1000 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 1000 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:Murray Brown
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Murray Brown
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:Murray Brown
ID: 39757253
Thanks for the help
0
 
LVL 63

Expert Comment

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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses
Course of the Month12 days, 9 hours left to enroll

777 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