Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3338016
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:19:25+00:00 2026-05-18T00:19:25+00:00

What is the best way to pass a variable from an OnClick event. To

  • 0

What is the best way to pass a variable from an OnClick event. To elaborate I would like to bind some data to the button and then when that button is pressed I would like to pass that data back to the code behind and do something cool with it.

I have found several ways of doing this, passing a commandParamter, add a button tag and use that. I wonder what is considered the best practice for this problem? Also am i looking at the problem wrong? Should I not try to pass a variable on click but do something else?

markup

<asp:button text="test" id="btnTest" runat="server"/>

codebehind

Protected Sub btnTest_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnTest.Click
    'do something cool
End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T00:19:26+00:00Added an answer on May 18, 2026 at 12:19 am

    It doesn’t sound like you’re really “passing” a variable to the button’s click event. You’re simply (and correctly) referencing the current value in the textbox by accessing test.Text. For example, assuming you have a label on your form named resultLabel:

    Protected Sub btnTest_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnTest.Click
        If test.Text.Trim() = String.Empty Then
            resultLabel.Text = "Please enter a value!"
        Else
            resultLabel.Text = test.Text
        End If
    End Sub
    

    Of course realistically you would use a RequiredFieldValidator or have a better way of displaying errors to the user instead of re-using the same label.

    This approach is fine. Once you’re familiar with this style you may choose to implement the Model-View-Presenter (MVP) pattern. A good project to look at is ASP.NET Web Forms MVP. Alternately, Microsoft offers a different flavor of ASP.NET, namely ASP.NET MVC (Model-View-Controller).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's the best way to pass data from one Windows Forms app (an office
What's the best way to pass in the value held in a javascript variable
What will the best way to pass a datatable data to unmanaged environments? (c++)
What is the best way to pass data between a C server to a
What is the best way to pass JSON to and from a Flash movie?
What's the best way to pass an optional argument that is nearly always the
I'm wondering the opinion of what is the best way to pass a lot
I'd like to pass content back and forth from PHP to jQuery and vice
I was wondering what is best way to create XML output from MVC2 application
What is the best / good way to implement method calls. For eg: From

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.