aymane
aymane

Reputation: 39

How to get html elements value in asp.net?

I'm trying to add a new seller into database but all the values i get from the input text are null

this is the form

<div class="mb-3">
                <label for="SNameTb" class="form-label">Seller FirstName</label>
                <input type="text" class="form-control" id="Snametb" runat="server">
            </div>

And this how i'm trying to get the values

string Sname = Snametb.Value;

Upvotes: 0

Views: 3089

Answers (2)

Mohamed El-sharkawy
Mohamed El-sharkawy

Reputation: 1

add runat= 'server' to the text input

Upvotes: 0

Albert D. Kallal
Albert D. Kallal

Reputation: 48989

You missing the closing "/> in that input box.

So, this:

<div class="mb-3">
    <label for="SNameTb" class="form-label">Seller FirstName</label>
    <input type="text" class="form-control" id="Snametb" runat="server" value="" />
</div>

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

and button click code:

protected void Button1_Click(object sender, EventArgs e)
{
    Debug.Print("Text box value = " + Snametb.Value);
}

So, add the "/>" to your markup (maybe a cut+paste issue).

And not required, but good idea to add the value="" attribute also.

Upvotes: 2

Related Questions