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

LVL 3
joeyluAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

krunal_shahCommented:
try this,
ThisLabel.ForeColor = System.Drawing.Color.Crimson;


check this,
http://asp-net-example.blogspot.com/2009/03/how-to-set-change-label-forecolor-font.html
0
rajeeshmcaCommented:
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....
0
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
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

rajeeshmcaCommented:
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="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:FormView ID="FormView1" runat="server">
            <ItemTemplate>
                <asp:Label ID="ThisLabel" Text='<%# Bind("Column1") %>' runat="server"></asp:Label>
            </ItemTemplate>
        </asp:FormView>
        <asp:Button ID="ClkBtn" runat="server" Text="Click" OnClick="ClkBtn_Click"></asp:Button>
    </form>
</body>
</html>

.vb page
--------------

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

    Protected Sub SetFormView()
        Dim objDataTable As New DataTable
        objDataTable.Columns.Add("Column1")
        Dim objRow As DataRow
        objRow = objDataTable.NewRow()
        objRow("Column1") = "Test Label Inside formview"
        objDataTable.Rows.Add(objRow)
        FormView1.DataSource = objDataTable
        FormView1.DataBind()
    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

0
NazoUKCommented:
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.
0
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?
0
rajeeshmcaCommented:
Did u check whether it enters in to the switcch case and satisfies a condition on the first click?
0
NazoUKCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
joeyluAuthor Commented:
that solves the problem, simple and clean, excellent thought, tks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.