开发者

How do I maintain user entered text in a TextBox nested in an UpdatePanel between updates?

I have an ASP.Net UpdatePanel that updates on a timer. Within the UpdatePanel and nested in a GridView, I have a TextBox that the user enters a value in with a submit button. Everything works fine, except if the user does not submit the value before the timed interval, the text is removed from the TextBox.

Is there a way to persist the user entry into the TextBox between updates? Is there a better way of doing this?

All suggestions welcome.

Respectfully,

Ray K. Ragan

Code Postscript:

aspx:

<script type="text/javascript">
    var prm = Sys.WebForms.PageRequestManager.getInstance();

    prm.add_beginRequest(beginRequest);

    function beginRequest() {
        prm._scrollPosition = null;
    } 
</script> 

<asp:Timer ID="Timer1" runat="server" Interval="900" OnTick="Timer1_Tick"></asp:Timer>

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <Triggers>
    <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
    </Triggers>
    <ContentTemplate>
    <asp:DataList RepeatColumns="5" RepeatDirection="Horizontal"  ID="dlMine" runat="server" OnItemCommand="Item_Command">
        <ItemTemplate>
            <div style="border:1px solid black;margin:3px;height:300px;text-align:center;padding:5px;">
                <div style="width:150px;">
                <asp:Label ID="lblTitle" runat="server" Text='<%# left(DataBinder.Eval(Container.DataItem,"title").ToString(), 75) %>'></asp:Label>
                </div>   
                    <asp:Label ID="Label1" runat="server" Text='<%# (DateTime)DataBinder.Eval(Container.DataItem,"end_date") %>'></asp:Label>
                    <br />
                    <asp:Label ID="Label2" runat="server" Text='<%# String.Format("{0:C}",DataBinder.Eval(Container.DataItem,"current_value")) %>'></asp:Label>
                    <br />

                    <asp:TextBox ID="txtNewValue" runat="server"></asp:TextBox>

                    <asp:Button Visible='<%# (isInThePast((DateTime)DataBinder.Eval(Container.DataItem,"end_date"))) ? false : true %>' CssClass="bid_button" runat="server" CommandArgument='<%# Eval("ID") %>' CommandName="Revalue" ID="btnBid" Text="Submit New Valuation" />                  
                </div>
        </ItemTemplate>
    </asp:DataList>
    </ContentTemplate>    
    </asp:UpdatePanel>

Codebehind:

    protected void Page_Load(object sender, EventArgs e)
            {
                Timer1.Tick += new EventHandler<EventArgs>(Timer1_Tick);


                if (!IsPostBack)
                {
                    dataBindList();
                }
            }

            protected void dataBindList()
            {
                if (Request.QueryString["GroupID"] != null)//Are they coming here with a URL var? If so, build content object
                {
                    List<Item> itemList = ItemManager.getItemsByGroupID(Request.QueryString["GroupID"].ToString());

                    dlMine.DataSource = itemList.Take(15);
                    dlMine.DataBind();
                }
            }

            protected void Timer1_Tick(object sender, EventArgs e)
            {
                dataBindList();
                UpdatePanel1.Update();
            }

protected void Item_Command(Object sender, DataListCommandEventArgs e)
        {
            if (e.CommandName == "Revalue")
            {
                Person p = (Person)Session["Person"];

                foreach (DataListItem item in dlMine.Items)
                {


                    string textBoxText = ((TextBox)item.FindControl("tx开发者_如何学PythontNewValue")).Text;

                    Utilities.writeLog("txtNewValue: " + textBoxText, 1);

                }            


                dataBindList();
                UpdatePanel1.Update();
            }
        }


You're rebinding the DataList every time the Timer ticks. All changes in the ItemTemplates of the DataList will be lost on postback.

Why not use Javascript to "pause" the timer whenver one of the textboxes gains focus. This will prevent the Timer from firing and let users finish entering text. Once they leave the textbox of "onblur" then you can restart the timer.

Update

The following will take a bit of effort to make it happen but you can do something like:

  • When the Timer posts back, before rebinding, iterate over the DataList while searching for textboxes with text in them. Something like:

foreach (DataListItem item in dlMine.Items) { //find the textbox control and check for text }

  • At this point, you'll know which rows need there textboxes repopulated. Store this information in a hashtable.

In the DataList ItemDataBound event, check each rowID against the hashtable to see if its corresponding textbox exists. If so, repopulate the textbox in the DataList row.


Are you initializing the TextBbox value in your code-behind, perhaps in Page_Load or another page method?

TextBox1.Text = "";

If so, that code is executing on every timer event. You can prevent that like this:

if (! IsPostback) {
    TextBox1.Text = "";
}

The first request that hits an ASP.NET page is usually an HTTP GET request, while ASP.NET buttons and update panel events issue HTTP POST requests. So the code above will clear TextBox1 the first time you access the page, but leave the value alone when receiving requests from itself. (If you really are setting the text box's value to its default - empty - you could just remove the initialization code.)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜