[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2014-01-04
6
Medium Priority
?
700 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 64

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 64

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

656 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