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 8716789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:10:17+00:00 2026-06-13T06:10:17+00:00

I have a gridview with textbox inside I want to get value of this

  • 0

I have a gridview with textbox inside

I want to get value of this textbox on button click

But I always receiving “0” (default) value

I think this problem related to viewstate but I’m not sure

Tell me please what I’m doing wrong?

Source code:

protected void Page_Load(object sender, EventArgs e)
{
    DataSet dataSet = new DataSet("MyDataSet");
    DataTable table = new DataTable();
    table.Columns.Add(new DataColumn("NameValue"));
    table.Columns.Add(new DataColumn("Number"));
    table.Columns.Add(new DataColumn("NumberValue"));
    dataSet.Tables.Add(table);

    DataRow row = dataSet.Tables[0].NewRow();
    row[0] = "Name";
    row[1] = "0";

    dataSet.Tables[0].Rows.Add(row);

    this.MyGridView.DataSource = dataSet;
    this.MyGridView.DataBind();
}

protected void Button1_Click(object sender, EventArgs e)
{
    TextBox txtsn = ((TextBox)this.MyGridView.Rows[0].FindControl("NumberTextBox"));
    string sn = txtsn.Text;
}




<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        <asp:GridView ID="MyGridView" AutoGenerateColumns="false" ShowHeader="false" runat="server" >
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label ID="NameLabel" runat="server" Text='<%#Eval("NameValue")%>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:TextBox ID="NumberTextBox" runat="server" Text='<%#Eval("NumberValue")%>' />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
  • 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-06-13T06:10:18+00:00Added an answer on June 13, 2026 at 6:10 am

    The problem is that you always bind the grid to it’s DataSource even on postbacks. That overrides all changes made by the user.

    Instead use the IsPostBack property:

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostback)
        {
            DataSet dataSet = new DataSet("MyDataSet");
            DataTable table = new DataTable();
            table.Columns.Add(new DataColumn("NameValue"));
            table.Columns.Add(new DataColumn("Number"));
            table.Columns.Add(new DataColumn("NumberValue"));
            dataSet.Tables.Add(table);
    
            DataRow row = dataSet.Tables[0].NewRow();
            row[0] = "Name";
            row[1] = "0";
    
            dataSet.Tables[0].Rows.Add(row);
    
            this.MyGridView.DataSource = dataSet;
            this.MyGridView.DataBind();
        }
    }
    

    You need to reload the DataSource only if something was changed(f.e. a record was deleted or added, the user clicked a sort-column or you have paging). But then you should do that only in the appropriate event handlers and not in page_load. So it’s best to wrap this code in a method which you can call from anywhere.

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

Sidebar

Related Questions

How to apply textbox blank validation on button click inside gridview in javascript?I have
i have create dynamic datepicker inside gridview. i want to know how to get
I have a texbox inside a templatefield in a gridview. For this textbox I
I have a gridview and I have a button on it.I want, if the
I have a textbox inside a TemplateField column for a gridview. When I set
I have a gridview that contains 2 item template 1-checkbox 2-textbox like this: <asp:GridView
How do I convert this gridview in razor syntax, I have curly brackets inside
I have a checkbox inside a gridview like this <asp:GridView ID=GridView1 runat=server BorderColor=Blue BorderWidth=1px
I have a gridview which has a textbox in an itemtemplate. I want to
I have Gridview like this. Here is my last column Gridview code; <EditItemTemplate> <asp:TextBox

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.