skip to Main Content

i am trying to set the value of a textbox control in my aspx page as the value of a label text. I am using the following code but nothing happens. I have tried from the code behind file using c# and then I want to assign the textbox value to a session variable. If I just assign a string value like "Hello"it works, but otherwise nothing works.

    protected void btnBook_Click(object sender, EventArgs e)
       
     {

        txtmtcid.Text = blbtest.Text;
        Session["mtcid"] = txtmtcid.Text;
        //Response.Redirect("booknow.aspx");
    }
    }

I also tried with Javascript, but no use:

    var mtcid = parsedData.employeeCode;

   document.getElementById('txtmtcid').value = mtcid;

The above js code works fine if I am assigning the value of mtcid to a label text, but not for the text box. please help.

2

Answers


  1. Chosen as BEST ANSWER

    So this is the markup:

             <asp:Label ID="blbtest" runat="server" ClientIDMode="Static"> 
             </asp:Label>
             <asp:Button ID="btnBook" runat="server" Text="Book Now" 
              CssClass="spaces-info__button" OnClick="btnBook_Click"/>
        
             <asp:TextBox ID="txtmtcid" runat="server"> 
             </asp:TextBox>
    

    Code behind:

       protected void Page_Load(object sender, EventArgs e)
        {
            
    
            if (!(Session["username"] == null))
            {
                string usn = Session["username"].ToString();
                lblusn.Text = usn;      
            }
    
        }
     protected void btnBook_Click(object sender, EventArgs e)
        {
            txtmtcid.Text = blbtest.Text;
            Session["mtcid"] = txtmtcid.Text;
            Response.Redirect("booknow.aspx");
        }
    

    updated js:

        $(function () {
        //dom is ready
    var request = new XMLHttpRequest;
    var usn = document.getElementById('lblusn').innerHTML;
    //var usn = "juhaina.ahmed";
    console.log(usn);
    
    request.open('GET', "URL" + usn + ""); //of course I have replaced 
        the URL here
        request.onload = function () {
        var response = request.response;
        var parsedData = JSON.parse(response);
        console.log(parsedData);
        var nm = parsedData.fullName;
        document.getElementById('lblfullnm').innerHTML = nm;
        var mtcid = parsedData.employeeCode;
        document.getElementById('blbtest').innerHTML = mtcid;
        document.getElementById('txtmtcid').value = 
     document.getElementById('blbtest').innerHTML
    };
    request.send();
        });
    

    I am new to js, and asp.net, so trying to browse whatever possible and work things out here. The session variable value is just not getting passed to next page. Honestly, i dont need the textbox, I dont know if label text can be stored in session variables. If thats possible, then all I want to do is assign the blbtest label text to the session variable, but that also didnt work,but if I am hard coding it like:

        Session["mtcid"]="D-11234" 
    

    this works and the value of session variable is passed.

    hence I am trying now with textbox. Please let me know if theres a better approach to this.

    If there is a way to avoid the use of the label and textbox and simply pass the session variable, Session["username"] from the code behind to the javascript, that would be great. how can I do it?


  2. You don’t show things that could be messing this up.

    and we can’t see your markup used.

    What does your page load event look like. Keep in mind that EVERY button click, post-back or ANY control on the page with a event code stub WILL RUN the page load event EVERY time. the page load event runs every time, and BEFORE your button click code runs.

    So, if I have this markup:

            <asp:Label ID="blbtest" runat="server" Text="zoo"></asp:Label>
            <br />
            <asp:TextBox ID="txtmtcid" runat="server"></asp:TextBox>
            <br />
            <br />
            <asp:Button ID="Button1" runat="server" Text="Button"
                onclick="Button1_Click" />
            <br />
    

    And then code behind of this:

        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        protected void Button1_Click(object sender, EventArgs e)
        {
            txtmtcid.Text = blbtest.Text;
        }
    

    I then get this when I click on the button

    enter image description here

    So, we can see how the lable text is now copy to the text box.

    You have to post more markup, and give futher detilas (such as some grid view, repeater or any other boatload of features that well explain your issue).

    Note that you can double click on the button in the web forms designer to create a event click for the button. Its possible that no event code for the button exists, and your button click code and code behind never runs.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search