GridView文本框總是返回空白值。

[英]asp:GridView textbox always return blank value


I added some textboxes to gridview using following code

我使用以下代碼向gridview添加了一些文本框

<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,

它總是返回我的空白值的所有行,同樣的事情發生的下拉列表中它返回我最初選擇價值即價值選擇的時候寫的下拉框,沒有重復的id目前asp的形式,這是我第一次與定制顯示數據表格,我想以某種方式讓它運行,

Can anybody suggest me any correction...

有人能給我一些修改意見嗎?

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

我檢查過里面有沒有重復的id,

2 个解决方案

#1


4  

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

我在這里找到了這個問題的解決方案:http://www.eggheadcafe.com/software/aspnet/29602882/gridview- can -get-text.aspx

Remember that the web page is fundamentally a stateless thing!

請記住,web頁面基本上是無狀態的!

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.

當GridView觸發事件時,執行Page_Load方法,然后執行處理GridView事件的方法。我還是不敢相信我的眼睛,但似乎即使您手動地將GridView數據源中的值分配給您的文本框,用戶輸入的值在GridView時也會被刪除。DataBind方法執行。

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

簡而言之,如果您的代碼與我的類似,那么在Page_Load方法中有這兩行代碼:

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

myGridView。數據源= someDataSet;myGridView.DataBind();

To solve this problem, change it to the following:

要解決這個問題,請將其改為:

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

如果(! IsPostBack){ myGridView。數據源= 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!

順便說一下,我在VB.net工作,所以請讓我知道我為您正確地轉換了c#。我想知道這對你也有好處!

#2


0  

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.

你想在哪里訪問這些值?如果在rowupdates函數中訪問它們,那么這些值將為空。在rowupdate中訪問它們,並確保取消更新。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/05/14/720c9efc368291fe6e1c030d8d72b60b.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com