开发者

Input value will not change using javascript

开发者 https://www.devze.com 2023-03-11 15:57 出处:网络
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?

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 =开发者_JAVA百科 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?


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.


This post will help you to set the value.

0

精彩评论

暂无评论...
验证码 换一张
取 消