Solved

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

Posted on 2014-01-04
6
693 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 63

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
Technology Partners: 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: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 63

Expert Comment

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

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project

733 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