简体   繁体   中英

How to get text of a TextBox and send it from JavaScript to code-behind using QueryString

I know I'm asking 2 questions but I'm really stuck. I have a form for adding and updating records and when I click the Update button(my Add works fine with the pop-up), I want a pop-up to appear with the properties of the record I get from my script(ie every texbox/dropdownlist filled with the correct values).

This is my script:

function btnEditEP_Click() {
            var recID = document.getElementById('<%=tboxEdit.ClientID%>').textContent;
            //if (recID !=null) {
            //    alert("ok what now?");
            //}
            window.open("editPopupEP.aspx?Txt=" + recID, "_blank", "toolbar=yes", "resizable=yes", "scrollbars=yes");
        }

And this is my PageLoad in editPopupEp.aspx.cs

protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int recID = Convert.ToInt32(Request.QueryString["Txt="]);
                ...
                ..
                .

If I can get the record ID, populating the input fields is easy but I need/want to get the Text of the Textbox and receive it from codebehind with QueryString .

The pop-up window works and there are no errors but the recID has 0 in it and there is no such record.

访问QueryString集合时必须省略等号,如下所示:

Request.QueryString["Txt"]

In the below line you are using textContent, it is not available in IE8 or below. Are you by any chance working on IE8?

var recID = document.getElementById('<%=tboxEdit.ClientID%>').textContent;

You can try something like this otherwise:

var recID =  document.getElementById('<%=tboxEdit.ClientID%>').value;

In your page_load event, you can use the below code:

int recID = Convert.ToInt32(Request.QueryString["Txt"]);

Use above code if textbox will always have integer value otherwise use below code.

int recID;
if(Int32.TryParse(Request.QueryString["Txt"], out recID))
{
 //Do whatever you want to do with recID
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM