簡體   English   中英

文本框文本在回發后消失

[英]TextBox Text disappear after postback

我想在asp.net中使用像這樣的標題列創建一個GridView

<asp:TemplateField SortExpression="Date">
    <HeaderTemplate>                                      
        <asp:LinkButton ID="headerDate" runat="server" Text="Date ↕️" CommandArgument="Date" CommandName="Sort" ></asp:LinkButton>
        <br />
        <asp:TextBox  AutoPostBack="true" ontextchanged="bindFilteredList" ReadOnly="false" CssClass="search_textbox"  runat="server" ID="fitlerDate" > 
        </asp:TextBox>
    </HeaderTemplate>
    <ItemTemplate>
        <%#Eval("Date") %>
    </ItemTemplate>
</asp:TemplateField>

我的問題是,當一個文本框被填充時,我的onTextChanged函數被觸發,我做了我的技巧(textBox中的值仍然在這里),頁面刷新自己並通過page_Load:

if (!IsPostBack && !IsAsync)
{
    this.mpePopUp.Hide();
    String defaultSortExpression;
    enuSortOrder defaultSortOrder;
    int defaultIndex;
    // On trie par date déscendant par défaut pour avoir les articles les plus récents
    defaultSortExpression = sortExpression[0];
    defaultSortOrder = enuSortOrder.soDescending;
    defaultIndex = 0;

    // bind data au gridview
    this.ViewState.Add(VS_CURRENT_SORT_EXPRESSION, defaultSortExpression);
    this.ViewState.Add(VS_CURRENT_SORT_ORDER, defaultSortOrder);
    this.ViewState.Add(VS_CURRENT_INDEX, defaultIndex);


    bindData(defaultSortExpression, defaultSortOrder, defaultIndex);
}

雖然我不適合if(使用調試器檢查),但在函數結束時,當調試器在結束括號上時,我的值將從TextBox中消失。

我該怎么做才能保持TextBox中的值?

你需要知道所有這些:

  • 我的GridView在updatePanle中
  • 我無法通過ID或編輯器在編輯器中訪問我的文本框

TextBox tb = (this.TableArticles.HeaderRow.FindControl("filterDate") as TextBox);

我不知道我的文本框ID不存在的原因。

您必須在ontextchanged事件中將fitlerDate的值存儲在ViewState中。 喜歡

ViewState["fitlerDate"] = fitlerDate.Text;

並在過濾后重新綁定網格時再次綁定該值。

您必須注冊GridView事件OnRowDataBound並且在那種情況下您必須編寫邏輯來查找標題控件

protected void gvGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                TextBox fitlerDate = (TextBox)e.Row.FindControl("fitlerDate");
                if (fitlerDate != null)
                {
                    fitlerDate.Text = ViewState["fitlerDate"].ToString();
                }
            }            
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM