2013-12-18 16 views
0

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

如何获得我使用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

当你的模态返回值,你可以使用location.href =“YourPage.aspx?myvalue = test”,你可以从代码后面用Request.QueryString [“myvalue”] – Mehmet

+0

但它不是一个链接。该模型将值放入文本框,然后刷新文本框页面。然后我想要捕获文本框内的内容并运行搜索。 – connersz

+0

它不一定是一个链接。您可以在javascript代码的任意位置使用location.href ...例如;关闭你的模态后。你可以像这样使用这个技巧; ** location.href = YourPage.aspx?myvalue = yourtexboxvalue **您可以使用document.getElementById(“textboxID”)获取您的texbox值。值 – Mehmet

回答

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)); 

值所以,所有的u需要做的就是添加的方法,并通过您的文本框中获取其作为价值,

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

我肯定,它会为ü

工作