use button's onClick event to change formview's control setting

Let's say I have three button on my page, each assigned a OnClick = "ClickID"

and I have a Label control as a select parameter for the ObjectDataSource  "lb_ID"

and I have a formview bind with the ObjectDataSource, with only a Label control inside "lb_Name"

What I try to achieve is each time a button is clicked, the "lb_ID" label text will change and the formview will bind itself to the related record by using the ObjectDataSource
as well as the label "lb_Name" inside of this formview, changes its color.

My problem now is that the binding has no problem, the lb_Name changes its text, but not its ForeColor, I have to click the same button again to change its color, why?
Protected Sub ClickID(ByVal Sender As Object, ByVal e As EventArgs)
        Dim ThisLabel As Label = CType(FormView1.FindControl("lb_Name"), Label)
        Select Case Sender.ID
            Case "bn_1"
                lb_ID.Text = "1"
                ThisLabel.ForeColor = Drawing.ColorTranslator.FromHtml("#e9ba00")
            Case "bn_2"
                lb_ID.Text = "2"
                ThisLabel.ForeColor = Drawing.ColorTranslator.FromHtml("#DC14E3")
            Case "bn_3"
                lb_ID.Text = "3"
                ThisLabel.ForeColor = Drawing.ColorTranslator.FromHtml("#008fe9")
        End Select
    End Sub

Open in new window

Who is Participating?
NazoUKConnect With a Mentor Commented:
Ah, you might need to do the findcontrol that gets the ThisLabel reference after the databind too, rebinding the form will probably result in a new control being created so the reference will be outdated.
try this,
ThisLabel.ForeColor = System.Drawing.Color.Crimson;

check this,
Hi joeylu,

Is the textbox within the form view or is it outside of the formview... If it is withinn the formview u should use the formview.FindControl() method to get the control....
Never miss a deadline with

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

joeyluAuthor Commented:
to Krunal:
System.Drawing.Color.Crimson is basically the same concept of Drawing.ColorTranslator.FromHtml("#008fe9"), this won't make anything different

to rajeeshmca:
it's inside of formview, and I did use the findcontrol method (please read my code before posting), in fact, without the findcontrol method, you won't even be able to get the lb_Name text change. btw, it's label, not textbox
Hi joeylu,

Just check out whether the label is being referenced and also check out whether any of the case is being satisfied in the switch case....

I tried with a form view and a button and it worked good....

aspx page

<html xmlns="">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
        <asp:FormView ID="FormView1" runat="server">
                <asp:Label ID="ThisLabel" Text='<%# Bind("Column1") %>' runat="server"></asp:Label>
        <asp:Button ID="ClkBtn" runat="server" Text="Click" OnClick="ClkBtn_Click"></asp:Button>

.vb page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
        End If
    End Sub

    Protected Sub SetFormView()
        Dim objDataTable As New DataTable
        Dim objRow As DataRow
        objRow = objDataTable.NewRow()
        objRow("Column1") = "Test Label Inside formview"
        FormView1.DataSource = objDataTable
    End Sub

    Protected Sub ClkBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ClkBtn.Click
        Dim obj As Label = CType(FormView1.FindControl("ThisLabel"), Label)
        obj.ForeColor = Drawing.ColorTranslator.FromHtml("#e9ba00")
    End Sub

I think you'll need to set the label's colour after the data has been bound to the data. Put a FormView1.DataBind() before the line where you set the colour.
joeyluAuthor Commented:
hi rajeeshmca,
yes it works ONLY if you don't bind the formview dynamically, as in my original post, I use a label called "lb_ID" to control the formview binding with ObjectdataSource select parameter. And it does change the color, the only problem i'm having is that I have to double click the button to see the color change. The first click only make the "lb_name" text changed, in another word, the formview gets bind, but it takes another click to let the "lb_name" forecolor change.

hi NazoUK
I had exactly the same thought when the problem occurred, I did try to move the formview.databind() above the ForeColor, it turns out that the color will not even change even with the second click.

It must be the databinding cycle issue, the label forecolor cannot be changed unless the formview and its controls are rendered? but simply put the formview.databind() on top of the color changes just doesn't work.. any other thoughts?
Did u check whether it enters in to the switcch case and satisfies a condition on the first click?
joeyluAuthor Commented:
that solves the problem, simple and clean, excellent thought, tks
All Courses

From novice to tech pro — start learning today.