如何在Javascript刷新后获取控制值

时间:2013-12-18 11:34:38

标签: c# javascript asp.net webforms code-behind

我有一个弹出模式窗口的页面,模态返回一个值到页面,然后刷新它。我需要能够在刷新时获取页面代码隐藏中的值,但刷新后返回的文本框控件的值始终为""

如何获取使用JS返回的值的值?

当我尝试在刷新内容之前获取包含内容的文本框的值时,代码会在测试时添加。

protected void Page_Load(object sender, EventArgs e) {
    TranslatePage.ObjectsSetup(Page.Controls, 3);
    GetUserInfo();
    if (txtCustomerType.Text != "" && txtCustomerType.Text != lblTempCustType.Text) {
        SearchCustType(txtCustomerType.Text);
    }
    SetupControls();
    string test;
    test = txtCustomerType.Text;
}

1 个答案:

答案 0 :(得分:0)

我有类似的情况,但在我的情况下,我有DropDown列表框,其中充满了javascript。因此,为了在javascript被触发以填充下拉列表后获取所选值,我使用了另一个方法get值,即

public static string GetValueOfControl(WebControl ServerControl)
    {
        string IdOfControl = ServerControl.UniqueID;
        NameValueCollection PostBackFormControls = HttpContext.Current.Request.Form;
        if (PostBackFormControls.AllKeys.Length == 0)
            return null;
        string value = PostBackFormControls[IdOfControl];
        if (value == null)
        {
            int index = 0;
            for (; index < PostBackFormControls.AllKeys.Length; index++)
                if (PostBackFormControls.AllKeys[index].EndsWith(IdOfControl))
                    break;
            if (index < PostBackFormControls.AllKeys.Length)
                return PostBackFormControls[index];
            else
                return null;
        }
        else
        {
            return value;
        }
    }

并获得我使用的值

string District = Convert.ToString(GetValueOfControl(dropDownListBoxDistrict));

因此,您需要做的就是添加方法并传递文本框以获取其值,

string test = Convert.ToString(GetValueOfControl(txtCustomerType));

我相信,它适用于你