Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp:GridView textbox always return blank value

I added some textboxes to gridview using following code

<asp:TemplateField HeaderText="STD<br/>ID">
       <ItemStyle BackColor="LightBlue" />
          <ItemTemplate>
          <div style="font-size:xx-small; overflow:hidden;">
          <asp:TextBox ID="txtStandard" EnableViewState="true" Height="10" Font-Size="XX-Small" Width="50" Text='<%# bind("STANDARD_ID") %>' runat="server"></asp:TextBox>
       <asp:AutoCompleteExtender ID="AutoCompleteExtenderDemo" runat="server" 
            TargetControlID="txtStandard" ServiceMethod="GetCompletionList"
            MinimumPrefixLength="1" CompletionInterval="1000"
            EnableCaching="true" CompletionSetCount="20">
        </asp:AutoCompleteExtender>
                        </div>



                    </ItemTemplate>
                </asp:TemplateField>

I want to save updated values to database, But when I try to access values using

string strs = ((TextBox)TwoHeadedGridView1.Rows[0].FindControl("txtStandard")).Text;

It always returns me the blank value for all the rows, Same thing happens for dropdown list it returns me originally selected value i.e. value selected at the time of writing dropdown box, there are no duplicate ids present on my asp form , This is my first interaction with customizing gridview, I want to somehow make it run,

Can anybody suggest me any correction...

I did checked that any duplicate id is present in it or not,

like image 642
Xinus Avatar asked Oct 14 '22 05:10

Xinus


1 Answers

I found the solution to this problem here: http://www.eggheadcafe.com/software/aspnet/29602882/gridview--cant-get-text.aspx

Remember that the web page is fundamentally a stateless thing!

When your GridView fires an event, your Page_Load method executes and then the method that handles the GridView event executes. I still can't believe my eyes, but it seems that even if you manually assign the values from the GridView datasource to your TextBoxes, the value that the user typed in gets wiped out when the GridView.DataBind method executes.

In short, if your code is like mine, you have these two lines of code in your Page_Load method:

myGridView.DataSource = someDataSet;
myGridView.DataBind();

To solve this problem, change it to the following:

if (!IsPostBack)
{
myGridView.DataSource = someDataSet;
myGridView.DataBind();
}

By the way, I work in VB.net, so please do let me know that I converted to C# correctly for you. I want to hear that this works for you too!

like image 81
Vivian River Avatar answered Oct 20 '22 13:10

Vivian River