开发者

Getting posted-back TextBox.Text in Page_Load?

I've a asp:TextBox and a submit button on my asp.net page. Once the button was clicked, the TextBos's value is posted back. I'm going to keep the the posted-back text value into session, so that other child controls can access to the value during their Page_Load. However, I always get NOTHING ("") in the Page_Load method, and I can read the text out in the button click handler. I know that the "button click event" happens after the Page_Load. So, I'm asking how can I "pre-fetch" the TextBox.text during Page_Load?

public partial class form_staffinfo : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e){
        string s = staff_id.Text; //Reach this line first, but GET empty value. However, I want to keep it in the session during this moment.
    }

    protected void btn_submit_Click(object sender, EventArgs e) {
    开发者_JAVA百科    string s = staff_id.Text; //Reach this line afterward, value got.
    }
}

-- EDITED --

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="form_staffinfo.ascx.cs" Inherits="form_staffinfo" %>
<asp:Label ID="Label1" runat="server" Text="Staff ID: "></asp:Label>
<asp:TextBox ID="staff_id" runat="server" ></asp:TextBox>
<asp:Button ID="btn_submit" runat="server" Text="Query" OnClick="btn_submit_Click" />

Since I can't get the TextBox's text in the Page_Load, so, I didn't include any code related to session for clear presentation.

Thank you! William


None of the values of your server controls are available for consumption in the Page_Load. Those controls are assigned after the form is validated (which is after the form is loaded) and before the form's control's events fire (like button clicks, in your example). The values posted are in the Request.Form Collection. Look in the AllKeys property and you should see a key that ends in $staff_id if you use your example posted. There may be other characters in from of the key, depending upon if the control is nested in a master page or other control.

If you absolutely must have that value at page load, grab it from the Request.Form collection instead of the user control, but I would question the wisdom of capturing the value that early in the page lifecycle. You could conceivably capture the textbox's OnTextChanged Event if you needed to preserve the value in Session.

EDIT - Additional Explanation if you were going to create a custom event for your user control, there are only a couple of steps to it.

Create a delegate. This is will be the common object for inter-control messaging.

public delegate void StaffIdChangedEvent(object sender, string staffId);

Declare an event using that delegate in the user control that is going to broadcast.

public event StaffIdChangedEvent StaffIdChanged;

In your user control, when you are ready to broadcast (say from the Staff_id textbox's OnTextChanged event), you just invoke the event [Its generally a best practice to check to see if the event is null]

this.StaffIdChangedEvent(this, "staff-id-value-here");

The final step is to wire the user control event up to an event handler (this prevents the null situation I mentioned above when trying to invoke the event). You could wire a handler into the hosting page.

this.form_staffinfo.StaffIdChangedEvent += this.some_method_on_page;

Just make sure the method on the page has the same method signature as the delegate used to declare the event.

Events also could be wired into each control that needs to know about them (look up multicast delegates), so you could do something like:

this.form_staffinfo.StaffIdChangedEvent += this.some_method_on_page;
this.form_staffinfo.StaffIdChangedEvent += this.some_control_on_the_page;
this.form_staffinfo.StaffIdChangedEvent += this.some_other_control_on_the_page;

In any event, I generally preferred to do this type of wiring in the page's OnInit method.

override protected void OnInit(EventArgs e)
    {
        base.OnInit(e);
        InitializeComponent();
    }

and just write your own InitializeComponent method to centralize any of this wiring you have to do.


There is something else that is setting the textbox value. Could you please check if you are overriding other event that occurs before Page_Load and modifying the textbox text property. Even, posting the code where you update session variable would be handy. From the code you have posted, it should work.


Do you have autoeventwireup disabled? I could be mistaken, but I think if it is disabled your Page_Load will not fire. If you want to leave it disabled, you can always override the OnLoad event...

protected override void OnLoad(EventArgs e)
{
  base.OnLoad(e);
  // do stuff
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜