Solved

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

Posted on 2014-01-04
6
683 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:murbro
Comment Utility
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
Comment Utility
Thanks for the help
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Not a problem, glad to be of help.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now