开发者

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 sugg开发者_如何学运维est me any correction...

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


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!


Where are you trying to access those values? If you are accessing them in the RowUpdated function, the values will be blank. Access them during RowUpdating, and make sure to cancel the update.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜