Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
i am developing a web page that will create rows and columns of text boxes on button click and will save values in sql through stored procedure.
i have searched and tried different tecniques but no working.
page is in master client.
.aspx page

ASP.NET
<script type="text/javascript" >
      var index = 1;

      function addrow() {
          var row = $('tr', "#table").length; //row the number of rows in table start from 0
          // alert("rowcount="+row+"");
          if (row == 6) {
              alert("only 06 lines allowed");
          } else {
              // alert(index);
              index++;
              var td;
              //               var row = $('tr', "#table").length; //row the number of rowsin table start from 0
              //alert("row"+row+"");
              var col = $("#table").find('tr')[0].cells.length; //col= no of columns in table tells the loop 
              // alert("col"+col+"");
              for (var i = 0; i < col; i++) {
                  td = td + '<td><input type="text" name="txt_' + row + '_' + i + '" runat="server" /></td>';
              }
              $('#table tr:last').after('<tr>' + td + '</tr>');
          }
      }


      function del_row(table) {
          table.each(function () {
              //alert($('tr', this).length);
              if ($('tr', this).length > 1) {
                  $('tbody tr:last', this).remove();
              }
          });
      }

      //debugger;
      function addcol() {
          //           alert("function has been called");
          var rcnt = 0; //=index in addrow
          ShowColCount();
          var colnum = $("#table").find('tr')[0].cells.length;     // colnum=no of columns to be added
          var rowlen = $('tr', "#table").length;
          if (colnum >= 7) {
              alert("you can add maximum 7 columns");
              return false;
          }
          else {
              $("#table").find('tr').each(function () {
                  $(this).find('td').eq(colnum - 1).after('<td><input  type="text" name="txt_' + rcnt + '_' + colnum + '" runat="server" /></td>');
                  rcnt++;
              });
          }
      }

      function ShowColCount() {
          var colnum = $("#table").find('tr')[0].cells.length;
          return colnum;
      }

      function del_col() {
          // alert("in" + colm + ""); 
          var colnum = $("#table").find('tr')[0].cells.length;     // colnum=no of columns to be added
          var rowlen = $('tr', "#table").length;
          $("#table tr").each(function () {
              if (colnum == 3 || rowlen == 0) {// alert("no more to delete");
              } else {
                  $(this).find("td:last").remove();
              }
          });
      }
   </script>


ASP.NET
<div id="add_div" class="add_row_clm_div">
        <input id="btn_addrow" type="button" value="Add Row"   önclick="addrow();" /> 
        <input id="btn_del_row" type="button" value="Delete Row"   önclick="del_row($('#table'));" />
        <input id="btn_addcol" type="button" value="Add Column"   önclick="addcol();" /> 
        <input id="btn_delcol" type="button" value="Delete Column"   önclick="del_col();" /> 
        
            <input id="txt_0_0" type="text" name="txt_1_1"  runat="server" />
        
        
        
        
            <input id="txt_0_1" type="text" name="txt_1_2"  runat="server" />
        
        
        
            <input id="txt_0_2" type="text" name="txt_1_3"  runat="server" />

the method is post in master page form
and input type="text" is in table in 1 row and three cols
here is the code i am using in aspx.cs file
C#
protected void btn_insert_Click(object sender, EventArgs e)
    {
hiddenfield();
}
      protected void hiddenfield()
    {
        SqlConnection conn = GetConnected();

        SqlCommand cmd = new SqlCommand("offers_2", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        if (hf1.Value == "")
        {
            int col1 = 1;
            int row=0;
            int col;
            //for ( row = 0; row <= 5; row++)
             while(row<=5)
            {
                for ( col = 0; col <= 6; col++)
                {//hf1= id and name of hidden field
                    //hf1.Value =("txt_"+ row +"_"+ col +"");
                    hf1.Value = Request["txt_" + row + "_" + col + ""]== null?"NO VALUE":"VALUE";
                    
                    cmd.Parameters.AddWithValue("@col"+ col1+"_"+row+ "", hf1.Value);
                    col1++;
                }
                row++;
                col1 = 1;
            }
        }
        cmd.ExecuteNonQuery();
    }



now the problem is that the text boxes that i have put manually pass the value to method but not the values of textboxes created/generated dynamically. Request[] or Request.Form[] or Page.Request.Form[] are getting null=true always for dynamic textboxes values.

thanks in advance
Posted
Updated 30-Jan-14 23:42pm
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900