How to add a textbox with default value ?

Bill Johnson 11 Reputation points
2023-01-13T11:28:23.4433333+00:00

Hi,

I have this code fragment:


<div class="DetailsBoxContent">
  <asp:UpdatePanel runat="server" ID="Run Report" UpdateMode="Conditional">
    <ContentTemplate>
      <asp:Panel ID="pnlGenerateRpt" runat="server">
        <div style="padding: 10px; text-align: center; width: 100%; float: right;">
          <asp:Button ID="btnGenerateRpt" runat="server" CssClass="StdButton" Text="Generate Report" OnClick="btnGenerateRpt_Click" />
        </div>
      </asp:Panel>
    </ContentTemplate>
    <Triggers>
      <asp:PostBackTrigger ControlID="btnGenerateRpt" />
    </Triggers>
  </asp:UpdatePanel>
</div>

I would like to add a label & textbox right to the existing button. The textBox needs to have a default value of 0 but can be amended by user.

How to achieve this ?

Thank you

Developer technologies ASP.NET Other
{count} votes

2 answers

Sort by: Most helpful
  1. Albert Kallal 5,586 Reputation points
    2023-01-13T19:34:10.0433333+00:00

    Ok, so right below the button, add this:

        <asp:Label ID="Label1" runat="server" Text="Enter a number"
            style="margin-left:12px" Font-Size="Medium">
        </asp:Label>
        <asp:TextBox ID="txtNumber" runat="server"
            Text="0"
            style="margin-left:10px" Font-Size="Medium"
            TextMode="Number">
        </asp:TextBox>
    
    

    Note how we used "margin-left to space things over as much as we like.

    And I did use text mode = number. This will prevent the user from enter of non numbers

    The result is thus now this:

    User's image

    so, you can just drag + drop in the label, then the text box.

    The "bonus" part is how we space out things to the right.

    (using that simple trick of margin-left is nice, since then we don't have to add a bunch of extra messy markup or even perhapas a "table". A simple drop control, move over a bit with margin-left makes this easy. And do the same trick for the text box if you want to space it over a bit more (or less).

    Regards,

    Albert D. Kallal (Access MVP 2003-2017)

    Edmonton, Alberta, Canada

    0 comments No comments

  2. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2023-01-16T06:03:25.55+00:00

    Hi @Bill Johnson,

    This can either be done in the markup (.aspx) like:

    <asp:TextBox id="txt" runat="server" Text="0" /> Or in the code-behind (.aspx.cs):

    TextBox1.Text = 0.ToString();

    Best regards,

    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.