Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6010187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:04:49+00:00 2026-05-23T02:04:49+00:00

I’m having a hard time figuring out how to change values of some input

  • 0

I’m having a hard time figuring out how to change values of some input values using javascript. Can you take a look at my code and tell me why this isn’t working?

Here is the c# portion of the page:

   protected void Page_Init(object sender, EventArgs e)
    {
        if (!Page.IsPostBack) {
        String date = DateTime.Today.AddDays(1).ToString("yyyyMMdd");

        String myConnectionString = "Driver={Pervasive ODBC Client Interface};servername=192.168.1.2;dbq=@dbname;";
        String commandString = "valid SELECT statement";
        OdbcConnection myConnection = new OdbcConnection();
        myConnection.ConnectionString = myConnectionString;
        OdbcDataReader reader2;
        OdbcDataReader reader3;
        OdbcCommand command = new OdbcCommand(commandString, myConnection);

        try
        {
            myConnection.Open();
            OdbcDataReader reader = command.ExecuteReader();

            delList.Controls.Clear();

            Table tbl = new Table();
            tbl.ID = "tbl1";
            tbl.BorderWidth = 1;
            delList.Controls.Add(tbl);

            //add header row
            String[] headers = { "id", "ticket#", "address", "city", "state", "zip", "start time", "end time", "total cube", "route" };
            TableHeaderRow th = new TableHeaderRow();
            TableCell tc;
            for (int i = 0; i < headers.Length; i++)
            {
                tc = new TableCell();
                tc.BorderWidth = 1;
                tc.Text = headers[i];
                th.Cells.Add(tc);
            }
            tbl.Rows.Add(th);

            int fCount = reader.FieldCount;
            int index = 1;
            char[] rem = new char[2] { 'B', 'D' };

            HtmlInputHidden hidden1, hidden2, hidden3;

            while (reader.Read())
            {

                if(reader.GetValue(fCount -1).ToString()[0] == 'D')
                {

                    TableRow tr = new TableRow();
                    tc = new TableCell();
                    tc.Text = index.ToString();
                    tc.BorderWidth = 1;
                    tr.Cells.Add(tc);
                    //create hidden field
                    hidden1 = new HtmlInputHidden();
                    hidden2 = new HtmlInputHidden();
                    hidden1.ID = "tick" + index;
                    hidden2.ID = "rte" + index;
                    hidden1.Value = reader.GetValue(0).ToString();
                    this.Controls.Add(hidden1);
                    for (int i = 0; i < fCount-1 ; i++)
                    {
                        tc = new TableCell();
                        tc.Text = reader.GetValue(i).ToString();
                        tc.BorderWidth = 1;
                        tr.Cells.Add(tc);
                    }
                    //get cube
                    String getItems = "SELECT so_dtl_item_id, so_dtl_qty_to_ship FROM so_dtl WHERE so_dtl_no='" + reader.GetValue(0) + "';";
                    String getCubes = "SELECT item_cube FROM item WHERE item_id='";
                    command = new OdbcCommand(getItems, myConnection);
                    reader2 = command.ExecuteReader();
                    double cubes = 0;
                    while (reader2.Read())
                    {
                        command = new OdbcCommand(getCubes + reader2.GetValue(0).ToString() + "';", myConnection);
                        reader3 = command.ExecuteReader();
                        double tmp = 0;
                        while (reader3.Read())
                        {
                            tmp = Convert.ToDouble(reader3.GetValue(0));
                        }
                        cubes += tmp * Convert.ToDouble(reader2.GetValue(1));
                        reader3.Close();
                    }                        
                    reader2.Close();
                    tc = new TableCell();
                    tc.BorderWidth = 1;
                    tc.Attributes["name"] = "cubes";
                    tc.Text = cubes.ToString();
                    tr.Cells.Add(tc);

                    //get route
                    tc = new TableCell();
                    tc.BorderWidth = 1;
                    String rteNum = reader.GetValue(fCount - 1).ToString().TrimStart(rem);

                    hidden2.Value = rteNum;
                    this.Controls.Add(hidden2);

                    tc.Text = "<input name=\"route\" id=\"route\" type=\"text\" value="+ rteNum +" onblur=\"refresh()\" />";
                    tr.Cells.Add(tc);

                    tbl.Rows.Add(tr);
                    index++;
                }
            }

            hidden3 = new HtmlInputHidden();
            hidden3.ID = "total";
            hidden3.Value = index.ToString();
            this.Controls.Add(hidden3);

            reader.Close();
            command.Dispose();
            myConnection.Close();
        }
        catch (OdbcException ex)
        {
            System.Diagnostics.Trace.WriteLine(ex.Message);
        }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button_Click(object sender, EventArgs e)
    {
        HtmlInputHidden hiddenControl = (HtmlInputHidden)FindControl("total");
        try
        {
            int total = Convert.ToInt32(hiddenControl.Value);
            String ticket, route;

            for (int i = 1; i < total; i++)
            {
                hiddenControl = (HtmlInputHidden)FindControl("tick" + i);
                ticket = hiddenControl.Value;

                hiddenControl = (HtmlInputHidden)FindControl("rte" + i);
                route = hiddenControl.Value;

                System.Diagnostics.Trace.WriteLine(ticket + ": " + route);
            }
        }
        catch (FormatException ex)
        {
            Console.WriteLine("Input string is not a sequence of digits.");
            System.Diagnostics.Trace.WriteLine(ex.Message);
        }
        catch (OverflowException ex)
        {
            Console.WriteLine("The number cannot fit in an Int32.");
            System.Diagnostics.Trace.WriteLine(ex.Message);
        }
    }

And here is the javascript to update the values:

    function update() {
            var tbl = document.getElementById('MainContent_tbl1');
            var rows = tbl.getElementsByTagName('tr');
            var routes = document.getElementsByName("route");
            var mod;
            //make call to c# func to have the database updated
            for (var i = 1; i < rows.length; i++) {
                mod = document.getElementById("rte" + i);
                //for testing
                if (i == 1 || i == 10) {
                    alert("rte" + i + ", " + mod.value);
                }
                //change value
                mod.value = routes[i - 1].value;
                //for testing
                if (i == 1 || i == 10) {
                    alert("rte" + i + ", " + mod.value);
                }
         }

The alerts display the correct values, but the values themselves are never changed in the source. All help will be greatly appreciated, Thanks.

I added if (!Page.IsPostBack) to page_init so now the value doesn’t change back but it returns as null any reason that may be happening?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:04:50+00:00Added an answer on May 23, 2026 at 2:04 am

    Well this is the problem when you dynamically add and set values in the code behind. You need set the values again for every page load. I dont know the solution for this but i have workaround for this when the situation calls for it.

    When you add a HTMLHiddenInput element dynamically and you check the page source, it looks like this :-

    <input name="ctl00$MainContent$Test" type="hidden" id="MainContent_Test" value="7" />

    Check out the name and id of the hidden field with all the asp.net heirarchy and all.

    Now in your code behind in the button click function you need to read it like this.

    string sTest = Request.Form["ctl00$MainContent$Test"];

    You have to read the name parameter of the input field.

    And in javascript when you are setting the value. You use this :-

    document.getElementById("MainContent_Test").value = "5";

    You use the id parameter of the input field.

    Also, you need to wrap the code in if(!IsPostBack) in the PageLoad so that the values dont get reset and once you are done with reading the values in the ButtonClick, you will again need to dynamically add the hidden fields like how you have done in the PageLoad.

    Its a dirty trick. Hope it works for you and a better solution will always be of help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
We're building an app, our first using Rails 3, and we're having to build
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.